java programming: enter size (1-16), what character to use, enter figure type ie
ID: 3625924 • Letter: J
Question
java programming: enter size (1-16), what character to use, enter figure type ie square, rt. triangle, or isoscles triangle,if invalid size program will print invalid entry please try again,program should work regardless of the case input of figure type,also check for incorrect figure type, good input is repeated until a valid size or figure type is entered,write internal documentation which describes your program logic and add comments.NOTE: modify the program so that the input for the figure size is done with a METHOD ONLY AND DO NOT USE METHOD ANY WHERE ELSE OR ARRAYS.BE SURE TO WRITE BOTH JAVADOC (INCLUDING PRE AND POST CONDITION DESCRIPTIONS)Explanation / Answer
please rate - thanks
you werent specific so hope this is good
import java.util.*;
public class drawtriangle
{public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int n;char s,c;
int i,j;
do
{n=getSize(in);
System.out.println("What character should be used? ");
c=in.next().charAt(0);
System.out.println("Enter shape");
System.out.println("s - square");
System.out.println("r - right triangle");
System.out.println("i - isoceles triangle");
System.out.println("e - exit");
s=in.next().charAt(0);
switch(Character.toLowerCase(s)) //accept upper or lower case
{case 's': for(i=0;i<n;i++) //square
{for(j=0;j<n;j++)
System.out.print(c);
System.out.println();
}
break;
case 'r': for(i=0;i<n;i++) //right triangle
{for(j=0;j<=i;j++)
System.out.print(c);
System.out.println();
}
break;
case 'i': for(i=1;i<=n;i++) //isosceles
{ for(j=1;j<=(n-i);j++) //print leading blanks
System.out.print(" ");
for(j=1;j<=(i*2-1);j++)
System.out.print(c); //print character
System.out.println();
}
case 'e': break; //exit do nothing
default: System.out.println("Invalid Entry");
break;
}
}while(s!='e');
}
public static int getSize(Scanner in)
{int i;
System.out.print("Enter size(1-16): ");
i=in.nextInt();
while(i<1||i>16) //check input validity
{System.out.println("Invalid entry");
System.out.print("Enter triangle size(1-16): ");
i=in.nextInt();
}
return i;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.