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) {     } }
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
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...
Write a program that asks the user to type in ages. They will type a negative...
Write a program that asks the user to type in ages. They will type a negative age when they finish entering the ages. The program will print out the average of all of the ages and the oldest age entered. It should end with a newline. Sample output #1 Type a negative for age to exit Enter your age: 21 Enter your age: 22 Enter your age: 21 Enter your age: -8 The average age is: 21.33 The oldest person...
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...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT