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
/* 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 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) {     } }
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...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Write a program that takes two numbers from the Java console representing, respectively, an investment and...
Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * (1 + interest rate)year We will assume that the interest...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly. starter code : import java.util.Scanner; /* * Counts down to a blastoff, starting from a given number. */ public class Countdown {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int countdown = 0;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT