Write a complete Java program called Scorer that declares a two-dimensional arra
ID: 3816738 • Letter: W
Question
Write a complete Java program called Scorer 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 doubles in each row. Use a method to output these three row averages to the command line. public class Scorer { public static void main(String[ ] args) { final int MAX_SIZE = 3; double[][] scores = new double[MAX_SIZE][MAX_SIZE]; scores = getNumbersFromUser(MAX_SIZE); double[] rowAverages = computeRowAverages(scores, MAX_SIZE); printRowAverages(scores, rowAverages); } public static double[][] getNumbersFromUser(int size) { double[][] numbers = new double[size][size]; Scanner in = new Scanner(System.in); System.out.println("Enter a line of 9 numbers, each followed by a space; enter 'q' to quit data entry: "); for (int = 0; i < size; i++) { for (int = 0; j < size; j++) { if (in.hasNextDouble()) { numbers[i][j] = in.hasNextDouble(); } } } An example output: 1 2 3 4 5 6 7 8 9, q The average of the first row [1.0 2.0 3.0] is 2.0. The average of the second row [4.0 5.0 6.0] is 5.0. The average of the third row [7.0 8.0 9.0] is 8.0. A while loop will replace the last for loop.
Explanation / Answer
package practise;
import java.util.*;
import java.lang.*;
import java.io.*;
public class scorer
{
public static void main(String[ ] args)
{
final int MAX_SIZE = 3;
double[][] scores = new double[MAX_SIZE][MAX_SIZE];
scores = getNumbersFromUser(MAX_SIZE);
double[] rowAverages = computeRowAverages(scores, MAX_SIZE);
printRowAverages(scores, rowAverages);
}
public static double[][] getNumbersFromUser(int size)
{
double[][] numbers = new double[size][size];
Scanner in = new Scanner(System.in);
System.out.println("Enter a line of 9 numbers, each followed by a space; enter 'q' to quit data entry: ");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (in.hasNextDouble())
{
numbers[i][j] = in.nextDouble();
}
}
}
return numbers;
}
public static double[] computeRowAverages(double[][] scores, int maxSize)
{
double[] rowAverages = new double[maxSize];
for(int i=0; i<maxSize; i++)
{
rowAverages[i] = 0;
for(int j=0; j<maxSize; j++)
{
rowAverages[i] += scores[i][j];
}
}
return rowAverages;
}
public static void printRowAverages(double[][] scores, double[] rowAverages)
{
System.out.println("The average of the first row [" + scores[0][0] + " " + scores[0][1] + " " + scores[0][2] + "] is " + rowAverages[0]);
System.out.println("The average of the second row [" + scores[1][0] + " " + scores[1][1] + " " + scores[1][2] + "] is " + rowAverages[1]);
System.out.println("The average of the third row [" + scores[2][0] + " " + scores[2][1] + " " + scores[2][2] + "] is " + rowAverages[2]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.