1. Write a class that prompts the user to enter the edge of a square, then use a
ID: 3543885 • 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
import java.util.*;
public class Conversion {
public static void main(String ard[]){
int edge, i, j;
System.out.print("Enter the square edge: ");
Scanner in = new Scanner(System.in);
edge = in.nextInt();
while(edge < 1 || edge > 30){
System.out.print("Invalid edge. Please re-enter the edge: ");
edge = in.nextInt();
}
for(i = 1; i <= edge; i++){
for(j = 1; j <= edge; j++){
if(i == 1 || i == edge || j == 1 || j == edge){
System.out.print("* ");
}
else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.