Question

Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...

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();
    }
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Write an application that asks a user to enter an integer. Display a statement that indicates...
Write an application that asks a user to enter an integer. Display a statement that indicates whether the integer is even or odd. ------------------------------------------ import java.util.Scanner; class EvenOdd {     public static void main(String[] args) {         // Write your code here     }     public static boolean isEven(int number) {     } }
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
Write a program that prompts the user to enter a string and displays the number of...
Write a program that prompts the user to enter a string and displays the number of characters it contains, fourth character, and last character. Note: The string may contain blanks. For example: “C++ programming is fun”. You should write a complete program.
Write a program that reads three integer values from the keyboard using the Scanner class representing,...
Write a program that reads three integer values from the keyboard using the Scanner class representing, respectively, a number of quarters, dimes, and nickels. Convert the total coin amount to dollars and output the result.Write a program that reads three integer values from the keyboard using the Scanner class representing, respectively, a number of quarters, dimes, and nickels. Convert the total coin amount to dollars and output the result.