In: Computer Science
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number of assignments (number of columns), respectively.
For example, if the file contains:
3
4
9 6 10 5
2 2 0 0
10 10 9 10
then it represents the scores for 3 students on 4 assignments as follows:
Assignment 1 | Assignment 2 | Assignment 3 | Assignment 4 | |
Student 1 | 9 | 6 | 10 | 5 |
Student 2 | 2 | 2 | 0 | 0 |
Student 3 | 10 | 10 | 9 | 10 |
The program should:
1) Read in the first two values to get the number of students and number of assignments. Create a two-dimensional array of integers to store all the scores using these dimensions.
2) Read in all the scores into the 2-D array.
3) Print the whole array, row by row on separate lines, using Arrays.toString() on each row of the array
3) Print the average of the scores on each assignment. For example, if the data is given in the above example, the output would be:
Array of scores:
[9, 6, 10, 5]
[2, 2, 0, 0]
[10, 10, 9, 10]
Average score of each assignment:
Assignment #1 Average = 7.0
Assignment #2 Average = 6.0
Assignment #3 Average = 6.333333333333333
Assignment #4 Average = 5.0
Starter Code:
import java.util.Scanner;
import java.io.File; //needed to open the file
import java.io.FileNotFoundException; //needed to declare possible
error when opening file
import java.util.Arrays; //for the Arrays.toString() method
public class Scores {
public static void main(String[] args) throws FileNotFoundException
{
//create a Scanner to get input from the keyboard to ask the user
for the file name
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the name of the file containing the
scores?");
//create another Scanner to read from the file
String fileName = keyboard.nextLine();
Scanner fileScan = new Scanner(new File(fileName));
//TODO: read in the values for the number of students and number of
assignments using the Scanner on the file
//TODO: create a 2-D to store all the scores and read them all in
using the Scanner on the file
System.out.println("Array of scores:");
//TODO: print the entire array, row by row, using
Arrays.toString()
System.out.println("Average score of each assignment:");
//TODO: compute and print the average on each assignment
Scores.java :
import java.util.Scanner;
import java.io.File; //needed to open the file
import java.io.FileNotFoundException; //needed to declare possible
error when opening file
import java.util.Arrays; //for the Arrays.toString() method
public class Scores {
public static void main(String[] args) throws
FileNotFoundException {
//create a Scanner to get input from the keyboard to ask the user
for the file name
Scanner keyboard = new
Scanner(System.in);
System.out.println("What is the
name of the file containing the scores?");
//create another Scanner to read from the file
String fileName =
keyboard.nextLine();
Scanner fileScan = new Scanner(new
File(fileName));
//TODO: read in the values for the number of students and number
of assignments using the Scanner on the file
int rows =
Integer.parseInt(fileScan.nextLine());// reading number of
rows
int cols =
Integer.parseInt(fileScan.nextLine());// reading number of
columns
//TODO: create a 2-D to store all the scores and read them all in
using the Scanner on the file
int[][] scores = new
int[rows][cols];
int i = 0;
while (fileScan.hasNext()) {
String line =
fileScan.nextLine();
String[]
strArray = line.split(" ");
for (int k = 0;
k < strArray.length; k++) {
scores[i][k] =
Integer.parseInt(strArray[k]);
}
i++;// increment
i
}
System.out.println("Array of
scores:");
// TODO: print the entire array,
row by row, using Arrays.toString()
for (int k = 0; k < rows; k++)
{
for (int j = 0;
j < cols; j++) {
System.out.print(scores[k][j] + " ");// print
each element
}
System.out.println();// used for new line
}
System.out.println("Average score
of each assignment:");
// TODO: compute and print the
average on each assignment
for (int k = 0; k < cols; k++)
{
int sum =
0;
for (int j = 0;
j < rows; j++) {
sum = sum + scores[j][k];// add each assignment
score in the sum
}
// print average
of each assignment
System.out.println("Assignment #" + (k + 1) + " Average = " +
((double) sum / 3));
}
}
}
==========================
Output :
Screen 1:Scores.txt
Screen 2: