Question: 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
What is the problem with this code....error messages received: all the way up to java 23 with error message.
SquareDisplay.java:1: error: class, interface, or enum
expected
public static void main(String[] args) {
^
SquareDisplay.java:2: error: illegal character: '\u00a0'
Scanner keyboard = new Scanner(System.in);
^
SquareDisplay.java:2: error: illegal character: '\u00a0'
Scanner keyboard = new Scanner(System.in);
^
SquareDisplay.java:2: error: illegal character: '\u00a0'
Scanner keyboard = new Scanner(System.in);
^
SquareDisplay.java:3: error: illegal character: '\u00a0'
System.out.print("Enter a number between 1-15:
");
^
SquareDisplay.java:3: error: illegal character: '\u00a0'
System.out.print("Enter a number between 1-15:
");
^
SquareDisplay.java:3: error: illegal character: '\u00a0'
System.out.print("Enter a number between 1-15:
");
^
SquareDisplay.java:3: error: class, interface, or enum
expected
System.out.print("Enter a number between 1-15:
");
^
SquareDisplay.java:4: error: illegal character: '\u00a0'
int number = keyboard.nextInt();
^
SquareDisplay.java:4: error: illegal character: '\u00a0'
int number = keyboard.nextInt();
^
SquareDisplay.java:4: error: illegal character: '\u00a0'
int number = keyboard.nextInt();
^
SquareDisplay.java:4: error: class, interface, or enum expected
Program below:
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();
}
}
SquareDisplay.java
import java.util.Scanner;
public class SquareDisplay {
public static void main(String[] args) {
//Declaring variable
int number;
//Scanner class object is used to
read the inputs entered by the user
Scanner keyboard=new Scanner(System.in);
/* This while loop continues to execute
* until the user enters a valid number as input.
*/
while(true)
{
//Getting the number entered by the
user
System.out.print("Enter a number between 1-15
:");
number = keyboard.nextInt();
//Checking the number is within the range or not
if(number<1 || number>15)
{
/* If the user entered number is
not with
* in range(between 1-15) displaying
error message
*/
System.out.println("** Invalid
Input **");
continue;
}
else
break;
}
//Calling the method by passing the user entered
number as argument
displayMatrix(number);
}
//This method will display the matrix based in the
user entered number
private static void displayMatrix(int number) {
//Displaying the matrix
for(int i=1;i<=number;i++)
{
for(int
j=1;j<=number;j++)
{
System.out.print('X');
}
System.out.println();
}
}
}
__________________
Output:
Enter a number between 1-15 :-1
** Invalid Input **
Enter a number between 1-15 :18
** Invalid Input **
Enter a number between 1-15 :5
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
__________Thank You
Get Answers For Free
Most questions answered within 1 hours.