Question

4.29 (Square of Asterisks) Write an application that prompts the user to enter the size of...

4.29 (Square of Asterisks) Write an application that prompts the user to enter the size of the side of a square, then displays a hollow square of that size made of asterisks. Your program should work for squares of all side lengths between 1 and 20.

I keep getting the error: "AsteriskSquare.java:2: error: class Main is public, should be declared in a file named Main.java public class Main ^ 1 error"

Homework Answers

Answer #1
import java.util.Scanner;

public class PrintSquare{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int sides = 0;

      while(sides == 0 || sides > 20){
      System.out.println("Enter a number between 1 and 20: ");
      int input = sc.nextInt();

       if(input > 0 && input <= 20)
         sides = input;
        }

        for(int i=0; i<sides; i++){
            
         if(i == 0 || i == sides-1){
          for(int j=0; j<sides; j++){
           System.out.print("*");
           }
            }else{
         System.out.print("*");
              
           for(int j=0; j<sides-2; j++){
           System.out.print(" ");
             }
              System.out.print("*");
            }
            System.out.println();
        }
    }
}
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
Write an application that prompts a user for two integers and displays every integer between them....
Write an application that prompts a user for two integers and displays every integer between them. Display There are no integers between X and Y if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger. ------------------------------------------------------------------------------------------------- import java.util.Scanner; public class Inbetween {     public static void main (String args[]) {         // Write your code here     } }
Write a program (just console application, no GUI) in C# which prompts the user for an...
Write a program (just console application, no GUI) in C# which prompts the user for an integer, increments the integer by 1 and displays the result in the following format: "The given number n has been incremented to n+1" Make sure your application fails to a secure mode. That is, no matter what is the user input, the application does not terminate with an error
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
Complete the following program so it prompts the user for a line of text and then...
Complete the following program so it prompts the user for a line of text and then displays the line in reverse. For instance if the input provided by the user is Good Day then the program would display yaD dooG import java.util.Scanner; public class Program2 { public static void main(String[] args) { Scanner kb = new Scanner(System.in);
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 an application that prompts a user for two integers and displays every integer between them....
Write an application that prompts a user for two integers and displays every integer between them. Display There are no integers between X and Y if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger. This is a java assignment. Pls help
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...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
Write a program that prompts the user to enter the weight of a person in kilograms...
Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. (Note that 1 kilogram 52.2 pounds.) Add your name and comments to the .cpp page    Print your name on screen    Submit .cpp file
Write a program that prompts the user to enter time in 12-hour notation. The program then...
Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your program must contain three exception classes: invalidHr, invalidMin, and invalidSec. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object. Follow similar conventions for the invalid values of minutes and seconds. This needs to be done in C++ There needs to be a separate header file for each...