First, launch NetBeans and close any previous projects that may be open (at the
ID: 3705480 • Letter: F
Question
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows.
Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line.
Use a method containing a nested for loop to compute the average of the doubles in each row.
Use a method to output these three row averages to the command line.
Explanation / Answer
Scorer.java
public class Scorer {
public static void main(String[] args) {
int n = 3;
double scores[][] = new double[n][n];
java.util.Scanner in = new java.util.Scanner(System.in);
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.println("Please Enter row ["+(i)+"] col ["+(j)+"] Value ...");
scores[i][j] = in.nextDouble();
}
}
System.out.println("Two Dimensional Input : ");
String str = "";
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
str = str + scores[i][j] +" ";
}
str = str + " ";
}
System.out.println(str);
double sum = 0.0;
for(int i=0; i<n; i++){
sum = 0.0;
for(int j=0; j<n; j++){
sum = sum + scores[i][j];
}
System.out.printf("The "+i+" Row Average is : %.2f ",((double)sum/(double)n));
}
}
}
Output:
Please Enter row [0] col [0] Value ...
1
Please Enter row [0] col [1] Value ...
2
Please Enter row [0] col [2] Value ...
3
Please Enter row [1] col [0] Value ...
4
Please Enter row [1] col [1] Value ...
5
Please Enter row [1] col [2] Value ...
6
Please Enter row [2] col [0] Value ...
7
Please Enter row [2] col [1] Value ...
8
Please Enter row [2] col [2] Value ...
9
Two Dimensional Input :
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
The 0 Row Average is : 2.00
The 1 Row Average is : 5.00
The 2 Row Average is : 8.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.