Make a test class that will read in the data from the following file into an arr
ID: 3742440 • Letter: M
Question
Make a test class that will read in the data from the following file into an array of Grade objects. You are then to make methods in your test class to answer 2 of the following questions: What is the average grade in CPSC-39 for each of the years, for all of the students in that year? What is the average number of students in a CPSC-39 class for every separate year? Which computer Science Class has the highest amount of A's? (Computer Science classes have a course that contains the letters CPSC)Explanation / Answer
/**************************GradeDemo.java*******************************
**********************use the Grade.java file without changes*********/
package grades;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import java.util.Map.Entry;
public class GradeDemo {
private static void highestGrade(Grade[] g, int n, String s, char c) {
HashMap<String, Integer> freq = new HashMap<String, Integer>();
for(int i = 0; i < n; i++) {
if(g[i].getGrade() == 'A' && g[i].getCourse().startsWith(s)) {
Integer count = freq.get(g[i].getCourse());
if (count == null) {
freq.put(g[i].getCourse(), 1);
}
else {
freq.put(g[i].getCourse(), count + 1);
}
}
}
System.out.println("Course with the highest number of " + c + " and beginning with " + s);
int maxValueInMap=(Collections.max(freq.values())); // This will return max value in the Hashmap
for (Entry<String, Integer> entry : freq.entrySet()) { // Itrate through hashmap
if (entry.getValue()==maxValueInMap) {
System.out.println(entry.getKey()); // Print the key with max value
}
}
}
private static void studentsPerYear(Grade[] g, int n, String s) {
HashMap<String, Integer> freq = new HashMap<String, Integer>();
for(int i = 0; i < n; i++) {
if(s.equals(g[i].getCourse())) {
Integer count = freq.get(g[i].getYear());
if (count == null) {
freq.put(g[i].getYear(), 1);
}
else {
freq.put(g[i].getYear(), count + 1);
}
}
}
System.out.println("Students per year for the course: " + s);
System.out.println(freq);
}
private static char averageGrade(Grade[] g, int n, String s) {
int count = 0;
int gradeScore = 0;
char c = 'E';
//Default grade
for(int i = 0; i < n; i++) {
if(s.equals(g[i].getCourse())){
c = g[i].getGrade();
if(c == 'A') gradeScore += 5;
else if(c == 'B') gradeScore += 4;
else if(c == 'C') gradeScore += 3;
else if(c == 'D') gradeScore += 2;
else if(c == 'E') gradeScore += 1;
count++;
}
}
if(count != 0) {
gradeScore /= count;
}
switch(gradeScore) {
case 5: c = 'A';
break;
case 4: c = 'B';
break;
case 3: c = 'C';
break;
case 2: c = 'D';
break;
case 1: c = 'E';
break;
default: c = 'E';
}
return c;
}
public static void main(String args[]){
Grade grades[] = new Grade[413940];
Scanner input = null;
try{
input = new Scanner(new File("grades.csv"));
} catch(FileNotFoundException e) {
e.printStackTrace();
}
input.nextLine();
int n = 0;
while(input.hasNextLine()) {
String line = input.nextLine();
String fields[] = line.split(",");
grades[n] = new Grade(fields[0], fields[1], fields[2].charAt(0));
System.out.println(grades[n]);
n++;
}
System.out.println("Averae grade in CSPC-39: " + averageGrade(grades, n, "CSPC-39"));
//Finds the average grade for a course
studentsPerYear(grades, n, "CSPC-39");
//Finds the number of students per year for a specific course
highestGrade(grades, n, "CSPC", 'A');
//Finds the class with the highest number of grade passed and the course prefix given
//CSPC is the course prefix
}
}
/***************Grade.java**********************************/
package grades;
public class Grade {
private String year;
private String course;
private char grade;
public Grade(){
year = "2010-11";
course = "Art-02";
grade = 'A';
}
public Grade(String year, String course, char grade){
this.year = year;
this.course = course;
this.grade = grade;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public char getGrade() {
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String toString() {
return "Grade [Year=" + year + ", Course=" + course + ", Grade=" + grade + "]";
}
public boolean equals(Grade obj) {
return (this.year.equals(obj.getYear()) && this.course.equals(obj.getCourse()) && grade == obj.getGrade());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.