I really need help with this assignment! For this assignment, you are to create
ID: 3708091 • Letter: I
Question
I really need help with this assignment!
For this assignment, you are to create a program in Java that asks the user to enter the size of a figure (must be odd), then displays a menu of 4 options. Options 1-3 will print various figures: box, diamond, X. Option 4 will allow the user to quit the program. You are to work on this assignment independently Program Design . You should have a single class called Figures . You will have 4 static methods: main, printBox, printDiamond, and printX. . The printBox,printDiamond, and printX methods should take as input an int parameter called size and print a box, diamond, or X shape respectively, of that given size. (see sample output at the end) 1. Ask the user to enter a number for the size of the figure. This number needs to be an ODD integer. If 2. Enter a loop in which you will display a menu of choices (1-4) and wait for user input (see example at . The main method should do the following the user enters a number that is not odd, ask him/her to reenter the number until it is odd. the end). The choices are: "1. Print box", "2. Print diamond", "3. Print X", and "4. Quit program" If options 1-3 are selected, print the corresponding figure shape with dimensions-size × size, by calling the appropriate method. The exception is the diamond figure, which will have size rows and size+1 columns. See example for sample output. If option 4 is selected, quit the program and print "Good bye!" 3. 4. Additional Requirements The name of your Java Class that contains the main method should be Figures. All your code should be within a single file Your code should follow good coding practices, including good use of whitespace (indents and line breaks) and use of both inline and block comments You need to use meaningful identifier names that conform to standard Java naming conventions At the top of the file, you need to put in a block comment with the following information: your name, date, course name, semester, and assignment name. Your program needs to handle invalid inputs gracefully. For example, entering a number outside of a valid range should not crash the program. Instead, the user should be prompted to enter the number again. The output of your program should exactly match the sample program output given at the end 1. 2. 3. 4. 5. 6.Explanation / Answer
Figures.java
import java.util.Scanner;
public class Figures {
public static void main(String args[]) {
//Declaring variables
int choice;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
/* This while loop continues to execute
* until the user enters a valid choice or choice 4
*/
while (true) {
//displaying the menu
System.out.println(" :: Menu ::");
System.out.println("1.Box");
System.out.println("2.Diamond");
System.out.println("3.X");
System.out.println("4.Quit");
//getting the choice entered by the user
System.out.print(" Enter choice :");
choice=sc.nextInt();
//Based on the user choice the corresponding case will be executed
switch (choice) {
case 1: {
printBox();
continue;
}
case 2: {
printDiamond();
continue;
}
case 3: {
printX();
continue;
}
case 4: {
System.out.println("Good Bye!");
break;
}
default:{
System.out.println("** Invalid Choice **");
continue;
}
}
break;
}
}
private static void printX() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if ((i == j) || (j == 4 - i)) {
System.out.print("X");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
private static void printDiamond() {
int n=5;
int space = n - 1;
for (int k = 1; k<=n; k++)
{
for (int c = 1; c<=space; c++)
System.out.print(" ");
space--;
for (int c = 1; c<= 2*k-1; c++)
System.out.print("*");
System.out.println();
}
space = 1;
for (int k = 1; k<= n - 1; k++)
{
for (int c = 1; c<= space; c++)
System.out.print(" ");
space++;
for (int c = 1 ; c<= 2*(n-k)-1; c++)
System.out.print("*");
System.out.println();
}
}
private static void printBox() {
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
System.out.print("X");
}
System.out.println();
}
}
}
_______________
Output:
:: Menu ::
1.Box
2.Diamond
3.X
4.Quit
Enter choice :1
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
:: Menu ::
1.Box
2.Diamond
3.X
4.Quit
Enter choice :2
*
***
*****
*******
*********
*******
*****
***
*
:: Menu ::
1.Box
2.Diamond
3.X
4.Quit
Enter choice :3
X X
X X
X
X X
X X
:: Menu ::
1.Box
2.Diamond
3.X
4.Quit
Enter choice :4
Good Bye!
_______________Thank You