Write an interactive program where two distinct “rectangular” shapes using the c
ID: 3603939 • Letter: W
Question
Write an interactive program where two distinct “rectangular” shapes using the character 'X' maybe displayed on the screen given user input of (a) two positive integers between 5 and 12 and (b) the users choice of "rectangular" display type.
The two integer values (between 5 and 12) should be used to represent no# of columns (or width) and no# of rows (or height) to generate the respective shapes on the screen. If the numbers are different, the larger of the two numbers should be used for the height (ie, rows) and the smaller for the width (ie, columns). The user should have an opportunity to choose between the two shapes to be displayed and when to “quit” the program.
Using appropriate control structures (HINT! nested while loops or for statements)write the necessary program statements that will display the desired shapes.
For example, if the user enters two positive integers 7 and 5 and selects 1:RECTANGLE - OUTLINE FORMAT
the program should display the following:
XXXXX
X X
X X
X X
X X
X X
XXXXX
On the other hand if perhaps the user chooses 2: RECTANGLE - FILLED-IN FORMAT
the program should display the following:
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
No setw implementation is allowed here but you must use nested loop or for statements with proper indentation to generate these patterns. Use meaningful variable identifier names, good prompting messages and appropriate comments for each loop segment as well as other sections in the body of your program.
Explanation / Answer
RectangleFormat.java
import java.util.Scanner;
public class RectangleFormat {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the two numbers: ");
int height = scan.nextInt();
int width = scan.nextInt();
if(height < width) {
int t = width;
width = height;
height = t;
}
System.out.println("Enter the choice: 1. RECTANGLE - OUTLINE FORMAT 2. RECTANGLE - FILLED-IN FORMAT 3.Quit");
int choice = scan.nextInt();
while(choice != 3) {
if(choice == 1) {
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
if(i==0 || j==0 || i==height-1 || j==width-1)
System.out.print("X");
else
System.out.print(" ");
}
System.out.println();
}
} else {
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
System.out.print("X");
}
System.out.println();
}
}
System.out.println("Enter the choice: 1. RECTANGLE - OUTLINE FORMAT 2. RECTANGLE - FILLED-IN FORMAT 3.Quit");
choice = scan.nextInt();
}
}
}
Output:
Enter the two numbers:
5
8
Enter the choice:
1. RECTANGLE - OUTLINE FORMAT
2. RECTANGLE - FILLED-IN FORMAT
3.Quit
1
XXXXX
X X
X X
X X
X X
X X
X X
XXXXX
Enter the choice:
1. RECTANGLE - OUTLINE FORMAT
2. RECTANGLE - FILLED-IN FORMAT
3.Quit
2
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
Enter the choice:
1. RECTANGLE - OUTLINE FORMAT
2. RECTANGLE - FILLED-IN FORMAT
3.Quit
3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.