Assistance with this being written in JAVA...19. Squares. Write a program class
ID: 3782600 • Letter: A
Question
Assistance with this being written in JAVA...19. Squares. 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
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number between 1-15: ");
int number = keyboard.nextInt();
validateNumber(keyboard, number);
outputMatrix("X", number);
keyboard.close();
}
static void validateNumber(Scanner keyboard, int number) {
while (number < 1 || number > 15) {
System.out.println("Sorry, that's an invalid number.");
System.out.print("Enter an integer in the range of 1-15: ");
number = keyboard.nextInt();
}
}
static void outputMatrix(String charToOutput, int number) {
for (int row = 0; row < number; row++) {
for (int column = 0; column < number; column++) {
System.out.print(charToOutput);
}
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.