Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The top picture is the direction for the program. The second picture is what the program should look like once executed. Right below that is

The top picture is the direction for the program. The second picture is what the program should look like once executed. Right below that is my program. I got it to print the Box, X and the diamond. I need to code to make it look like the example.

image text in transcribedimage text in transcribed

import java.util.Scanner;

public class newwpp

{

//declare and initialize instance variable

public int size = 0;

//method printBox

public void printBox()

{

//loop that represents rows

for(int i = 0; i

{

//loop that represents columns

for(int j = 0; j

{

//print *

System.out.print("*");

}

//go to next line

System.out.println();

}

}

public void printX()

{

for (int i = 1; i

for (int j = 1; j

if (j == i) {

System.out.print("*");

} else if (j == size - (i - 1)) {

System.out.print("*");

} else {

System.out.print(" ");

}

}

System.out.println(); }

}

public void printDiamond()

{

Scanner in = new Scanner (System.in);

//System.out.print("Enter Number: ");

int inp = in.nextInt();

int wan = 1;

int space = inp ;

for(int i = 0; i

{

for(int j = 0; j

{

System.out.print(" ");

}

for(int j = 0; j

{

if(j == 0 || j == wan -1)

{

System.out.print("*");

}

else

{

System.out.print(" ");

}

}

if(i == inp-1)

{

break;

}

else{

System.out.println();

wan=wan+2;

space--;

}

}

//second

int x = (inp * 2) - 1;

int y = 1;

for(int l = 0; l

{

for(int j = y; j >= 1; j--)

{

System.out.print(" ");

}

for(int j = 0; j

{

if(l >= 1)

{

if(j == 0 || j == x - 1)

{

System.out.print("*");

}

else

{

System.out.print(" ");

}

}

}

System.out.println();

x-=2;

y++; }

}

public static void main(String[] args)

{ int num = 9;

Scanner scan = new Scanner(System.in);

while(num > 0 && num

{System.out.print("Enter a odd number between (0 to 9): ");

num= scan.nextInt();

if( num % 2 == 0)

{

System.out.println("Error. Please enter an odd number");

System.out.print("Enter a number between (1 to 9): ");

num= scan.nextInt();

}

else

{

System.out.println("You Enter: " + num);

}

//create an object for Box class

newwpp myBox = new newwpp();

newwpp myX = new newwpp();

newwpp myDiamond = new newwpp();

//change the size to 5 using '.' operator

myBox.size = num;

myX.size = num;

myDiamond.size = num;

//invoke the printBox method

myBox.printBox();

myX.printX();

myDiamond.printDiamond();

}

}}

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Market Research Cost Effective Searching Of The Internet And Online Databases

Authors: John F. Lescher

1st Edition

0201489295, 978-0201489293

More Books

Students also viewed these Databases questions

Question

6. How would you design your ideal position?

Answered: 1 week ago

Question

2. How do they influence my actions?

Answered: 1 week ago