There is a CSV file Grades.txt that has grades for different courses as shown in Fig.1 below.
Question:
There is a CSV file Grades.txt that has grades for different courses as shown in Fig.1 below. The first element in each row is the course-code, followed by grades scored by students in that course. For example, the first row is for course 95712. It has eight students who have scored different grades from A to C. Different courses have different number of students, and some courses have some special grade-letters such as D and F.
You need to program that processes this grade file and produces a report as shown in Fig.2.
95712, A, B, A, B, C, A, A, B 95706, B, A, D, A, B, C, F, A 95643, C, A, B, A, D 96546, A, C, B, A, A, B, A Fig.1. Grades.txt | *** File data *** 95712, A, B, A, B, C, A, A, B 95706, B, A, D, A, B, C, F, A 95643, C, A, B, A, D 96546, A, C, B, A, A, B, A *** Course grades *** 95712 A B A B C A A B 95706 B A D A B C F A 95643 C A B A D 96546 A C B A A B A *** Grade letters *** [A, B, C, D, F] *** Grade Table *** A B C D F 95712 4 3 1 0 0 95706 3 2 1 1 1 95643 2 1 1 1 0 96546 4 2 1 0 0 Fig. 2. Console output |
Solution Design
You are given a class Grader.java shown in Fig. 3 that has five member variables as arrays, four methods to load these arrays, and four methods to print the outputs as shown in Fig.2. The main() and print methods are fully coded. You need to write the remaining methods. Please refer to comments in the starter code for more details.
Fig. 3: Class diagram
Hint:
You may consider using a StringBuilder object to read the content of the file. A StringBuilder is a dynamic string which can grow in size and its content could be modified.
When you are reading/writing files, you need to add code to handle exceptions.
In your code, you may have something like below
try { input = new Scanner(new File(filename)); } catch (FileNotFoundException e) { e.printStackTrace(); } StringBuilder stringData = new StringBuilder(); while (input.hasNextLine()) { stringData.append(input.nextLine() + " "); } fileData = stringData.toString().split(" ");
This will load the content of the file into string builder object and then split it into individual lines.
Design constraint:
- Use of collection classes is not allowed in this lab. Using collection classes (e.g. ArrayList) may cost you up to 3 points.
- Do not hard code the array length. Your program will be tested with a different data file which will have different number of entries. Hard coding may cost you up to 3 points.
Instructions:
- Grader.javapackage lab2;
import java.util.Arrays;
import java.util.Scanner;
public class Grader {
String[] fileData; //stores each row from file as an array element
String[] courses; //will have all course codes
String[][] courseGrades; //each i'th row will have grades for course in courses[i]
String[] gradeLetters; //will have all possible grade letters across all courses
int[][] gradeTable; //gradeTable[i][j] will have count of gradeLetters[j] in courses[i]
Scanner input;
//do not change this method
public static void main(String[] args) {
Grader grader = new Grader();
grader.loadData("Grades.txt");
grader.printData();
grader.loadGrades();
grader.printGrades();
grader.loadGradeLetters();
grader.printGradeLetters();
grader.buildGradeTable();
grader.printGradeTable();
}
/** loadData() takes filename as input,
* reads its content, and loads each row
* as a string in fileData array
* @param filename
*/
void loadData(String filename) {
//code here
}
/** loadGrades() reads data from fileData[] array
* populates courses[] and courseGrades[][] arrays.
* Each row in courses[] and courseGrades[][] represents one course
* Note that each row in courseGrades[][] has
* a different number of elements
*/
void loadGrades() {
//code here
}
/** loadGradeLetters() populates the gradeLetters[] array
* by scanning the data in courseGrades[][].
* Each grade letter must appear only once in
* gradeLetters[]
*/
void loadGradeLetters() {
//code here
}
/*** buildGradeTable() scans the data in courseGrades[][]
* and populates gradeTable[][]. Each row in grdeTable
* represents one course and each column represents a grade letter.
* Each cell contains the number of students who scored that grade
* in that course
*/
void buildGradeTable() {
//code here
}
/************************ print methods ********************************/
/** printData() prints all the rows
* as shown in the handout under the heading
* *** File data ***
*/
void printData() {
System.out.println("*** File data ***");
for (int i = 0; i < fileData.length; i++) {
System.out.println(fileData[i]);
}
System.out.println();
}
/** printGrades() prints data from courses[] and courseGrades[][]
* as shown in the handout under the
* heading *** Course grades ***
*/
void printGrades() {
System.out.println("*** Course grades ***");
for (int i = 0; i < courses.length; i++) {
System.out.print(courses[i] + " ");
for (int j = 0; j < courseGrades[i].length; j++) {
System.out.print(courseGrades[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
/** printGradeLetters prints letter grades stored in gradeLetters
* as shown in the handout under
* the heading *** Grade letters ***
*/
void printGradeLetters() {
System.out.println("*** Grade letters ***");
System.out.println(Arrays.toString(gradeLetters));
System.out.println();
}
/** printGradeTable() prints the gradeTable
* as shown in the handout
* under the heading *** Grade Table ***
*/
void printGradeTable() {
System.out.println("*** Grade Table ***");
for (int i = 0; i < gradeLetters.length; i++) {
System.out.print("t" + gradeLetters [i]);
}
System.out.println();
for (int i = 0; i < courses.length; i++) {
System.out.print(courses[i] + "t");
for (int j = 0; j < gradeTable[i].length; j++) {
System.out.print(gradeTable[i][j] + "t");
}
System.out.println();
}
}
}
- Grades.txt
95712, A, B, A, B, C, A, A, B 95706, B, A, D, A, B, C, F, A 95643, C, A, B, A, D 96546, A, C, B, A, A, B, A
- Create package named lab2 and copy/import java files in it. Store Grades.txt in project folder
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates