You are to extend the program you wrote in 5A to work for non filled-in rectangl
ID: 3630911 • Letter: Y
Question
You are to extend the program you wrote in 5A to work for non filled-in rectangles in addition to the filled rectangle. If either of the input values for the length or width or both are negative an unfilled rectangle is to be printed. As in part A above, the request for good input is repeated until a valid number or figure type is entered. Finally, you are to write an overall description of what the program is designed to do as well as internal documentation which describes internal program logic.Here's my code from The first part
Scanner keyboard = new Scanner(System.in);
int length = 0;
int width = 0;
String chara;
String quit = " ";
while (!quit.equals("no")) {
System.out.print("Enter the length of the rectangle (+1 to +10): ");
length = keyboard.nextInt();
while (length < 1 || length > 10) {
System.out.println("Input Error -----");
System.out.print("Enter the length of the rectangle (+1 to +10): ");
length = keyboard.nextInt();
}
System.out.print("Enter the width of the rectangle (+1 to +10): ");
width = keyboard.nextInt();
while (width < 1 || width > 10) {
System.out.println("Input Error -----");
System.out.print("Enter the width of the rectangle (+1 to +10): ");
width = keyboard.nextInt();
}
System.out.print("Enter the symbol to be used: ");
chara = keyboard.next();
for (int county = 0; county < width; county++) {
for (int countx = 0; countx < length; countx++) {
System.out.print(chara);
}
System.out.println();
}
System.out.println("Do you want another rectangle, Yes or No: ");
quit = keyboard.next();
quit = quit.toLowerCase();
These are some sample outputs
Enter + values for a filled rectangle and - values for a rectangle outline.
Enter the non zero length of the rectangle (-10 to +10): -10
Enter the non zero width of the rectangle (-10 to +10): 0
Input Error -----
Enter the non zero width of the rectangle (-10 to +10): 3
Enter the symbol to be used: ^
^^^^^^^^^^
^ ^
^^^^^^^^^^
Do you want another rectangle, Yes or No: yES
Enter the non zero length of the rectangle (-10 to +10): 12
Input Error -----
Enter the non zero length of the rectangle (-10 to +10): 8
Enter the non zero width of the rectangle (-10 to +10): 2
Enter the symbol to be used: &
&&&&&&&&
&&&&&&&&
Do you want another rectangle, Yes or No: nO Enter + values for a filled rectangle and - values for a rectangle outline.
Enter the non zero length of the rectangle (-10 to +10): -8
Enter the non zero width of the rectangle (-10 to +10): -8
Enter the symbol to be used: A
AAAAAAAA
A A
A A
A A
A A
A A
A A
AAAAAAAA
Do you want another rectangle, Yes or No: yEs
Enter the non zero length of the rectangle (-10 to +10): 0
Input Error -----
Enter the non zero length of the rectangle (-10 to +10): 2
Enter the non zero width of the rectangle (-10 to +10): -2
Enter the symbol to be used: *
**
**
Do you want another rectangle, Yes or No: NO
Written in Java. Thank you
Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{public static void main(String[] args)
{Scanner keyboard = new Scanner(System.in);
int length = 0;
int width = 0;
char chara;
char charb;
String quit = " ";
while (!quit.equals("no")) {
System.out.print("Enter the length of the rectangle (-10 to +10): ");
length = keyboard.nextInt();
while (Math.abs(length) < -10 || Math.abs(length) > 10||length==0) {
System.out.println("Input Error -----");
System.out.print("Enter the length of the rectangle (-10 to +10): ");
length = keyboard.nextInt();
}
System.out.print("Enter the non zero length of the rectangle (-10 to +10): ");
width = keyboard.nextInt();
while (Math.abs(width) < -10 || Math.abs(width) > 10||width==0) {
System.out.println("Input Error -----");
System.out.print("Enter the non zero length of the rectangle (-10 to +10): ");
width = keyboard.nextInt();
}
System.out.print("Enter the symbol to be used: ");
chara =keyboard.next().charAt(0);
if(length<0||width<0)
charb=' ';
else
charb=chara;
for (int countx = 0; countx < Math.abs(length); countx++) {//top line
System.out.print(chara);
}
System.out.println();
for (int county = 0; county < Math.abs(width)-2; county++) { // middle lines
System.out.print(chara);
for (int countx = 0; countx < Math.abs(length)-2; countx++) {
System.out.print(charb);
}
System.out.print(chara);
System.out.println();
}
for (int countx = 0; countx < Math.abs(length); countx++) {//bottom line
System.out.print(chara);
}
System.out.println();
System.out.println("Do you want another rectangle, Yes or No: ");
quit = keyboard.next();
quit = quit.toLowerCase();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.