. Write a java program that specifies three parallel one dimensional arrays name length, width, and area. Each array should be capable of holding a number elements provided by user input. Using a for loop input values for length and width arrays. The entries in the area arrays should be the corresponding values in the length and width arrays (thus, area[i] = length [i]* width [i]) after data has been entered display the following output:
Length Width Area
-------- -------- -------
25 2.6 65.00
18.2 4.9 89.18
Sample Run1 Enter the array size: 5
Enter the Length and Width for Rectangle 1: 25 2.6
Enter the Length and Width for Rectangle 2: 18 4.9
Enter the Length and Width for Rectangle 3: 100 3.27
Enter the Length and Width for Rectangle 4: 1.84 7.4
Enter the Length and Width for Rectangle 5: 56 9.5 Output1:
Length Width Area
-------- -------- -------
25 2.6 65.00
18.2 4.9 89.18
100 3.27 327.00
1.84 7.4 13.62
56 9.5 532.00
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LengthWidthArea {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sizeOfArray = 0;
System.out.print("Sample Run1 Enter the array size: ");
sizeOfArray = input.nextInt();
double area[][] = new double[sizeOfArray][3];
for (int i = 0; i < area.length; i++) {
System.out.print("Enter the Length and Width for Rectangle " + (i + 1) + " : ");
for (int n = 0; n < area[i].length; n++) {
area[i][n] = input.nextDouble();
}
}
System.out.println("Length Width Area");
System.out.println("-------- -------- -------");
System.out.println("\t\t\t"+sum(area));
for (int i = 0; i < area.length; i++) {
for (int n = 0; n < area.length; n++) {
System.out.print(area[i][n] + " ");
}
System.out.println();
}
input.close();
}
public static List<Double> sum(double[][] array) {
List<Double> total = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
double mult = 1;
for (int k = 0; k < array[i].length; k++) {
mult = mult * array[i][k];
}
total.add(mult);
}
return total;
}
}
Comments
Leave a comment