Need java code: Write a program class named SquareDisplay that asks the user for
ID: 3857216 • Letter: N
Question
Need java code:
Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following: XXXXX XXXXX XXXXX XXXXX XXXXX
INPUT and PROMPTS. The program prompts for an integer as follows: "Enter an integer in the range of 1-15: ".
OUTPUT. The output should be a square of X characters as described above.
CLASS NAMES. Your program class should be called SquareDisplay
Explanation / Answer
Hi Below is your code: -
I have added exception handling also like number not in correct format or it has value less than 1 and greater than 15.
Note that in question your output is as follows
XXXXX XXXXX XXXXX XXXXX XXXXX
for this kind of output, the code is : -
import java.util.Scanner;
//Class declaration
public class SquareDisplay {
// Main function
public static void main(String[] args) {
// Scanner object to get the input
Scanner sc = new Scanner(System.in);
// flag which will set true on valid input
boolean done = false;
// loop till a valid input is obtained
while (!done) {
try { // try catch to check if only number is inserted.
System.out.print("Enter an integer in the range of 1-15: ");
int index = Integer.parseInt(sc.next());
if (index < 1 || index > 15) { // checking if input is in valid range
System.out.println("Please enter number between 1 and 15 only.");
} else {
//printing the square
for (int i = 0; i < index; i++) {
for (int j = 0; j < index; j++) {
System.out.print("X");
}
System.out.print(" ");
}
done = true;
sc.close();
}
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number.");
}
}
}
}
Sample run: -
Enter an integer in the range of 1-15: 0
Please enter number between 1 and 15 only.
Enter an integer in the range of 1-15: kjcndj
Please enter a valid number.
Enter an integer in the range of 1-15: 5
XXXXX XXXXX XXXXX XXXXX XXXXX
If you want output as a square i.e.
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
Then code is
import java.util.Scanner;
//Class declaration
public class SquareDisplay {
// Main function
public static void main(String[] args) {
// Scanner object to get the input
Scanner sc = new Scanner(System.in);
// flag which will set true on valid input
boolean done = false;
// loop till a valid input is obtained
while (!done) {
try { // try catch to check if only number is inserted.
System.out.print("Enter an integer in the range of 1-15: ");
int index = Integer.parseInt(sc.next());
if (index < 1 || index > 15) { // checking if input is in valid range
System.out.println("Please enter number between 1 and 15 only.");
} else {
//printing the square
for (int i = 0; i < index; i++) {
for (int j = 0; j < index; j++) {
System.out.print("X");
}
System.out.println();
}
done = true;
sc.close();
}
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number.");
}
}
}
}
Sample Run: -
Enter an integer in the range of 1-15: 5
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
Let me know if you have any concern in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.