Write a program that takes two lists of 5 positive integers each and places them
ID: 3695887 • Letter: W
Question
Write a program that takes two lists of 5 positive integers each and places them in two arrays called "rowList" and "columnList". Afterwards, generate 5 rectangles of asterisks, each with dimensions taken from the previous two arrays provided. The 1st elements of "rowList" and "columnList" would generate the first rectangle, the 2nd elements of "rowList" and "columnList" would generate the second rectangle, and so on. The input values for "rowList" and "columnList" must only be between 2 and 7. The program is to implement and use a function called "printRectangle", which takes two input parameters (one for row numbers, and one for column numbers), and generates a rectangle with the dimension specified by the parameters (row and column). When run, 3, 7, -2, 5, 4, 4, 9, 7, 6, 2, 4, 2 must be used as input with the output matching the sampe run below.
SAMPLE RUN:
Enter 5 positive integers for rowList (2 to 7)
0: 3
1: 7
2: -2 Rejected!
2: 5
3: 4
4: 4
Enter 5 positive integers for columnList (2 to 7)
0: 9 Rejected!
0: 7
1: 6
2: 2
3: 4
4: 2
Rectangles generated
3 rows 7 columns
*******
*******
*******
7 rows 6 columns
******
******
******
******
******
******
******
5 rows 2 columns
**
**
**
**
**
4 rows 4 columns
****
****
****
****
4 rows 2 columns
**
**
**
**
Explanation / Answer
RectangleArray.java
package org.students;
import java.util.Scanner;
public class RectangleArray {
public static void main(String[] args) {
// Creating the one-dimensional array of size 5
int rowList[] = new int[5];
int columnList[] = new int[5];
// Scanner object is used to get the inputs from the user.
Scanner sc = new Scanner(System.in);
System.out.println("Enter 5 positive integers for rowList (2 to 7):");
// Filling Row Array by taking the values from the user.
for (int i = 0; i < 5; i++) {
while (true) {
System.out.print(i + " : ");
int row = sc.nextInt();
if (row >= 2 && row <= 7) {
rowList[i] = row;
break;
} else {
System.out.println("Rejected.");
continue;
}
}
}
// Filling Row Array by taking the values from the user.
System.out
.println("Enter 5 positive integers for columnList (2 to 7):");
for (int j = 0; j < 5; j++) {
while (true) {
System.out.print(j + " : ");
int row = sc.nextInt();
if (row >= 2 && row <= 7) {
columnList[j] = row;
break;
} else {
System.out.println("Rejected.");
continue;
}
}
}
// Displaying the rectangles
System.out.println(":: Rectangles Generated ::");
for (int k = 0; k < 5; k++) {
System.out.println(rowList[k] + " Rows " + columnList[k]
+ " Columns");
for (int i = 0; i < rowList[k]; i++) {
for (int j = 0; j < columnList[k]; j++) {
System.out.print("*" + " ");
}
System.out.println(" ");
}
System.out.println(" ");
}
}
}
_____________________________________________________________________________
output:
Enter 5 positive integers for rowList (2 to 7):
0 : 7
1 : 6
2 : 5
3 : 4
4 : 3
Enter 5 positive integers for columnList (2 to 7):
0 : 4
1 : 5
2 : 6
3 : 7
4 : 3
:: Rectangles Generated ::
7 Rows 4 Columns
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
6 Rows 5 Columns
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
5 Rows 6 Columns
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
4 Rows 7 Columns
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
3 Rows 3 Columns
* * *
* * *
* * *
_______________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.