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...
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...
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)...
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...
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...
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;...
Write an interactive version of the InchesToFeet class that accepts the inches value from a user....
Write an interactive version of the InchesToFeet class that accepts the inches value from a user. class InchesToFeetInteractive {       public static void main(String[] args) {       // Modify the code below      final int INCHES_IN_FOOT = 12;      int inches = 86;      int feet;      int inchesLeft;      feet = inches / INCHES_IN_FOOT;      inchesLeft = inches % INCHES_IN_FOOT;      System.out.println(inches + " inches is " +         feet + " feet and " + inchesLeft + " inches");   } }
We may expand the general strategy established in the prior question to determine whether a given...
We may expand the general strategy established in the prior question to determine whether a given number is divisible by another. Modify the program from the prior question or write one from scratch which determines whether 12345 is divisible by 15. public class Main {   public static void main( String[] args ) {     int myNumber = 12345; //15     int divisor = 15; //4     System.out.print( "(" + myNumber + ") is " );     if( myNumber%divisor==0) {       // Do nothing     }     else...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT