1. Write a class that prompts the user to enter the edge of a square, then use a
ID: 3543531 • Letter: 1
Question
1. Write a class that prompts the user to enter the edge of a square, then use asterisks to displays it (hollow with an empty body). The edge must be a number from 1 to 30. If edge is out of range, the program should display an error message and allow the user to enter a new value of edge.
Sample Run 1:
Enter the square edge: -4
Invalid edge. Please re-enter the edge: -10
Invalid edge. Please re-enter the edge: -40
Invalid edge. Please re-enter the edge: 5
* * * * *
* *
* *
* *
* * * * *
Explanation / Answer
please rate - thanks
any questions ask
import java.util.*;
public class main
{public static void main(String args[])
{Scanner in=new Scanner(System.in);
int edge,i,j;
System.out.print("enter the square edge:");
edge=in.nextInt();
while(edge<1||edge>30)
{System.out.print("Invalid edge. Please-enter the edge:");
edge=in.nextInt();
}
for(i=0;i<edge;i++)
System.out.print("*");
System.out.println();
for(i=1;i<edge-1;i++)
{System.out.print("*");
for(j=1;j<edge-1;j++)
System.out.print(" ");
System.out.println("*");
}
for(i=0;i<edge;i++)
System.out.print("*");
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.