Design Utility class that has no instance data and no constructor. It provides f
ID: 3601272 • Letter: D
Question
Design Utility class that has no instance data and no constructor. It provides four methods that draw the following four triangular shapes as shown:
* ******* ******* *
** ****** ****** **
*** ***** ***** ***
**** **** **** ****
***** *** *** *****
****** ** ** ******
******* * * *******
The methods are named lowerLeft, upperLeft, upperRight, and lowerRight, respectively. The methods name indicates the position of right angle (90 degrees) corner in each triangular shape. Each method accepts an integer parameter n which is the number of lines with stars in the triangle. Some shapes require printing some number of " " (spaces) before printing the specified number of stars.
The Driver class will be used to invoke the four methods. Design the flow where user is asked to type
LL ,UL, UR, or LR (either lower caseor upper case ) to select method lowerLeft, upperLeft, upperRight, and lowerRight, respectively or XX to stop. Any other input from user should be followed by prompt to input one of the four given options or XX to stop, and accepting input again. After any legal input from user, program should ask for input for n which is triangle size.
For each user pair of inputs program should print the type of triangle that user selected and input size provided and than prints it. Run program by making the above four different legal choices and sizes of your choice ( between 3 and 9), and then select XX to stop.
Explanation / Answer
Implemented code as per the requirement. Please comment if modification required.
Code:
=======
import java.util.Scanner;
public class Triangles {
static int size;
static String choice = "";
static Scanner scan_size = new Scanner(System.in); //Scanner to read input size
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//Scanner to read choice ll/lr/ul/ur
while(!choice.equalsIgnoreCase("XX")){ //Infinite loop to read choice
System.out.println("LL ,UL, UR, or LR (either lower caseor upper case ) to select method"+
" lowerLeft, upperLeft, upperRight, and lowerRight, respectively or XX to stop");
choice = sc.nextLine(); //Reading choice
//LL ,UL, UR, or LR
if(choice.equalsIgnoreCase("LL")){ //Condition for LL
lowerLeft();
}
else if(choice.equalsIgnoreCase("LR")){//Condition for LR
lowerRight();
}
else if(choice.equalsIgnoreCase("UL")){//Condition for UL
upperLeft();
}
else if(choice.equalsIgnoreCase("UR")){//Condition for UR
upperRight();
}
else if(choice.equalsIgnoreCase("XX")){//Condition for stop
break;
}
else{//Condition for invalid input
System.out.println("Select one of the below options..");
}
}
}
public static void lowerLeft(){
int size = 0;
while(!(size>=3 && size<=9)){ //Loop to read size till valid input receive
System.out.println("Enter size of the triangle between 3 and 9: ");
size = scan_size.nextInt(); //Reading size
}
for(int i=0; i<=size;i++){ //Printing triangle
for(int j=0; j<i; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void upperLeft(){
int size = 0;
while(!(size>=3 && size<=9)){ //Loop to read size till valid input receive
System.out.println("Enter size of the triangle between 3 and 9: ");
size = scan_size.nextInt(); //Reading size
}
//Printing triangle
for(int i=size; i>0;i--){
for(int j=i; j>0; j--){
System.out.print("*");
}
System.out.println();
}
}
public static void upperRight(){
int size = 0;
while(!(size>=3 && size<=9)){ //Loop to read size till valid input receive
System.out.println("Enter size of the triangle between 3 and 9: ");
size = scan_size.nextInt();//Reading size
}
for(int i=0; i<=size;i++){//Printing triangle
for(int k=0; k<i;k++){
System.out.print(" ");
}
for(int j=i; j<size; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void lowerRight(){
int size = 0;
while(!(size>=3 && size<=9)){//Loop to read size till valid input receive
System.out.println("Enter size of the triangle between 3 and 9: ");
size = scan_size.nextInt();//Reading size
}
//Printing triangle
for(int i=1; i<=size;i++){
for(int k=size-i; k>0;k--){
System.out.print(" ");
}
for(int j=1; j<=i; j++){
System.out.print("*");
}
System.out.println();
}
}
}