Problem Statement:
Draw a triangle for a given size, using given character and pointing toward given direction.
Language:
Java
Platform:
Command Prompt
Command:
[Program] -size [Size] -char [Char] -direction [Direction]
Expected Input:
[Size]: Any positive integer. Error should be printed if non-positive integer or string is given.
[Char]: Any single char. Error should be printed if more then one char is given.
[Direction]: R/L/U/D for Right / Left / Up / Down. Error should be printed if any other input is given.
Note: The order of parameters may change.
Hint: First make programme with fix order of parameter to get a working code. Then attempt to process the command line input for parameter reordering. This approach is known as modular approach for programming.
Expected Output:
Print the triangle of given size in given direction using given character. See example for better understanding.
Example:
Interface:
$ java DrawTriangle -size 5 -char A -direction R
Output:
A
AA
AAA
AAAA
AAAAA
AAAA
AAA
AA
A
import java.io.*;
ReplyDeleteclass Triangle{
public static void main(String[] args) {
try{
System.out.println("******************** right ***************");
//************************* Right ******************************//
for (int i=1; i<=5;i++ ){
for (int j=1; j<=i;j++ ){
System.out.print("*");
}
System.out.println("");
}
for (int i=1; i<5;i++ ){
for (int j=4; j>=i;j-- ){
System.out.print("*");
}
System.out.println("");
}
System.out.println("******************** Left ***************");
//************************** Left ******************************//
for(int i = 1; i<=5; i++){
for(int j=5-i; j>=1;j--){
System.out.print(" ");
}
for(int j=1; j<=i;j++){
System.out.print("*");
}
System.out.println("");
}
for(int i = 1; i<5; i++){
for(int j=1; j<=i;j++){
System.out.print(" ");
}
for(int j=4; j>=i;j--){
System.out.print("*");
}
System.out.println("");
}
//************************ Top ***********************************//
System.out.println("******************** Top ***************");
for(int i = 1; i<=5; i++){
for(int j=4; j>=i;j--){
System.out.print(" ");
}
for(int j=1; j<=i-1;j++){
System.out.print("*");
}
System.out.print("*");
for(int j=0; j<=i-2;j++){
System.out.print("*");
}
for(int j=4; j>=i;j--){
System.out.print(" ");
}
System.out.println();
}
//************************ bottom ***********************************//
System.out.println("######################### Bottom ############################################");
for(int i = 1; i<=5; i++){
for(int j=1; j<=i-1;j++){
System.out.print(" ");
}
for(int j=5; j>=i;j--){
System.out.print("*");
}
for (int j=4; j>=i;j-- ){
System.out.print("*");
}
System.out.println("");
}
}
catch(Exception e){}
}
}
the basic code is working... if you can remove extra empty lines from the code that it will be easy to read :) ...
ReplyDeleteimport java.io.*;
ReplyDeleteclass Triangle{
public static void main(String[] args) {
try{
//************************* Right ******************************//
int loopSize = Integer.parseInt(args[0]);
String displayChar= args[1];
String direction = args[2];
if(direction.equalsIgnoreCase("R")){
for (int i=1; i'<'=loopSize;i++ ){
for (int j=1; j'<'=i;j++ ){
System.out.print(displayChar);
}
System.out.println("");
}
for (int i=1; i'<'loopSize;i++ ){
for (int j=loopSize-1; j'>'=i;j-- ){
System.out.print(displayChar);
}
System.out.println("");
}
}
//************************** Left ******************************//
if(direction.equalsIgnoreCase("L")){
for(int i = 1; i'<'=loopSize; i++){
for(int j=loopSize-i; j'>'=1;j--){
System.out.print(" ");
}
for(int j=1; j'<'=i;j++){
System.out.print(displayChar);
}
System.out.println("");
}
for(int i = 1; i'<'loopSize; i++){
for(int j=1; j'<'=i;j++){
System.out.print(" ");
}
for(int j=loopSize-1; j'>'=i;j--){
System.out.print(displayChar);
}
System.out.println("");
}
}
//************************ Top ***********************************//
if(direction.equalsIgnoreCase("U")){
for(int i = 1; i'<'=loopSize; i++){
for(int j=loopSize-1; j'>'=i;j--){
System.out.print(" ");
}
for(int j=1; j'<'=i-1;j++){
System.out.print(displayChar);
}
System.out.print(displayChar);
for(int j=0; j'<'=i-2;j++){
System.out.print(displayChar);
}
for(int j=loopSize-1; j'>'=i;j--){
System.out.print(" ");
}
System.out.println();
}
}
//************************ bottom ***********************************//
if(direction.equalsIgnoreCase("D")){
for(int i = 1; i'<'=loopSize; i++){
for(int j=1; j'<'=i-1;j++){
System.out.print(" ");
}
for(int j=loopSize; j'>'=i;j--){
System.out.print(displayChar);
}
for (int j=loopSize; j'>'=i;j-- ){
System.out.print(displayChar);
}
System.out.println("");
}
}
}
catch(Exception e){}
}
}
program is good, however I can not give more then 6/10. reasons are given below:
ReplyDelete1. Down arrow is not working correctly.
2. Input method is also not as per the problem statement.
Down Arrow:
C:\Java>java Triangle 5 D D
DDDDDDDDDD
DDDDDDDD
DDDDDD
DDDD
DD
C:\Java>
Input Methods:
As per the problem statement, I needs to give any of the following command imputes and I should get the same output which is obvious from the command.
java DrawTriangle -size 5 -char A -direction R
java DrawTriangle -char A -size 5 -direction R
java DrawTriangle -size 5 -direction R -char A