Write an application that prompts the user to enter a Diamond\'s axis size and t
ID: 3722503 • Letter: W
Question
Write an application that prompts the user to enter a Diamond's axis size and then use asterisks to displays it (hollow with an empty body or filled with asterisks). The size must be an odd number from 3 to 27. If the size is out of range, the program should y an error message and allow the user to enter a new value of the size. 3. Enter the Diamond axis size: 2 Invalid edge. Please re-enter the edge: 30 Invalid edge. Please re-enter the edge: 5 1. Empty 2. Filled Choose an option: 1 Enter the Diamond axis size: 9 1. Empty 2. Filled Choose an option: 2Explanation / Answer
import java.util.*;
class Diamond {
public static void main(String[] args) {
int size, option;
Scanner in = new Scanner(System.in);
System.out.print("Enter the Diamong axis size: ");
while (true) {
size = in.nextInt();
if (size >= 3 && size <= 27 && size % 2 == 1)
break;
System.out.print("Invalid edge. Please re-enter the edge: ");
}
System.out.println("1. Empty 2. Filled");
System.out.print("Choose an option: ");
option = in.nextInt();
switch (option) {
case 1:
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i + j == size / 2 || i - j == size / 2 || j - i == size / 2
|| i + j == size + (((size - 1) / 2) - 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
break;
case 2:
for (int i = 0; i <= size / 2; i++) {
for (int j = 0; j < (size / 2) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i * 2; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; i <= size / 2; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= 2 * ((size / 2) - i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
break;
default:
System.out.println("Invalid Option");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.