19. 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

Respuesta :

Answer:

// program in java.

// package

import java.util.*;

// class definition

class SquareDisplay

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

    // variable

       int num;

       // scanner object to read input

Scanner scr=new Scanner(System.in);

System.out.print("Enter an integer in the range of 1-15:");

 // read integer from user

num=scr.nextInt();

 // read until input is not in range 1-15

while(num<0 || num>15)

{

    System.out.print("wrong input!!enter again:");

     // read again

    num=scr.nextInt();

}

// print square of X

for(int a=1;a<=num;a++)

{

    for(int b=1;b<=num;b++)

    {

        System.out.print("X");

    }

    System.out.println();

}  

   }catch(Exception ex){

       return;}

}

}

Explanation:

Read an integer from user and assign it to variable "num".Check if input is not  in range 1-15 then ask again until user enter a number between 1-15 only.Then  print a square of character "X" with the help of two nested for loops.

Output:

Enter an integer in the range of 1-15:-4                                                                                  

wrong input!!enter again:20                                                                                                

wrong input!!enter again:4                                                                                                

XXXX                                                                                                                      

XXXX                                                                                                                      

XXXX                                                                                                                      

XXXX