Question
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.
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(); } }}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started