package grades; //D.Lopes //9.3.18 //Grade HW Assignment public class Grade { //
ID: 3745614 • Letter: P
Question
package grades;
//D.Lopes
//9.3.18
//Grade HW Assignment
public class Grade {
//data variables
private String year;
private String course;
private char grade;
//default constructor
public Grade(){
year = "2010-11";
course = "Art-02";
grade = 'A';
}
//constructor
public Grade(String year, String course, char grade){
this.year = year;
this.course = course;
this.grade = grade;
}
//getters and setters for course
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
//getters and setters for grade
public char getGrade() {
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
//setters and getters for year
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
//toString method
@Override
public String toString() {
return "Grade [Year=" + year + ", Course=" + course + ", Grade=" + grade + "]";
}
//equals method
public boolean equals(Grade obj) {
return (this.year.equals(obj.getYear()) && this.course.equals(obj.getCourse()) && grade == obj.getGrade());
}
}//end class
package grades;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
public class GradeDemo {
public static void studentsPerYear(Grade[] g, int n, String s) {
for(int i = 0; i < n; i++) {
}
}
public static char averageGrade(Grade[] g, int n, String s) {
for(int i = 0; i < n; i++) {
}
return
}
public static void main(String args[]) {
//array of grade objects
Grade grades[] = new Grade[413940];
//reads file of grades
Scanner input = null;
try{
input = new Scanner(new File("grades.csv"));
} catch(FileNotFoundException e) {
e.printStackTrace();
}
//to get rid of header
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++;
}//end while
//Finds the average grade for a course
System.out.println("Averae grade in CSPC-39: " + averageGrade(grades, n, "CSPC-39"));
//Finds the number of students per year for a specific course
studentsPerYear(grades, n, "CSPC-39");
}
}
This is what the first few rows look like, I can't upload a file. It goes from 2010-11 to 2015-16, the file has 413,940 rows. Grades are A, B, C, D and F. The classes are all different.
Attached is a csv file that contains all of the grades for all of the students at Merced College from 2010-2016. Your task is to create a class to hold a record for this file. Then make a Test program to read in the data from the file into an Array of Objects. For your Grade object class make the following data variables and methods: .String year String course char grade . make 2 constructors; the default constructor and another constructor that take in 3 parameters to set your data variables make the setters, getters, toString, and equals methods. 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? Just count the letter grades A, B, C, D, F What is the average number of students in a CPSC-39 class? Which computer Science Class has the highest amount of A's? (Computer Science classes have a course that contains the letters CPSC you can hard code the names of the classes in your code)Explanation / Answer
Screenshot
----------------------------------------------------------
Program
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Grade {
//data variables
private String year;
private String course;
private char grade;
//default constructor
public Grade(){
year = "2010-11";
course = "Art-02";
grade = 'A';
}
//constructor
public Grade(String year, String course, char grade){
this.year = year;
this.course = course;
this.grade = grade;
}
//getters and setters for course
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
//getters and setters for grade
public char getGrade() {
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
//setters and getters for year
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
//toString method
@Override
public String toString() {
return "Grade [Year=" + year + ", Course=" + course + ", Grade=" + grade + "]";
}
//equals method
public boolean equals(Grade obj) {
return (this.year.equals(obj.getYear()) && this.course.equals(obj.getCourse()) && grade == obj.getGrade());
}
}//end class
public class GradeDemo {
//Display each year number of students of a particular class
public static void studentsPerYear(Grade[] g, int n, String s) {
//Variables to take count of each year
int count1=0,count2=0,count3=0,count4=0,count5=0,count6=0;
//Loop through the array
for(int i = 0; i < n; i++) {
//Compare year then compare course,accordingly increment count
if(g[i].getYear().equals("2010-11")) {
if(g[i].getCourse().equals(s)) {
count1++;
}
}
else if(g[i].getYear().equals("2011-12")) {
if(g[i].getCourse().equals(s)) {
count2++;
}
}
else if(g[i].getYear().equals("2012-13")) {
if(g[i].getCourse().equals(s)) {
count3++;
}
}
else if(g[i].getYear().equals("2013-14")) {
if(g[i].getCourse().equals(s)) {
count4++;
}
}
else if(g[i].getYear().equals("2014-15")) {
if(g[i].getCourse().equals(s)) {
count5++;
}
}
else if(g[i].getYear().equals("2015-16")) {
if(g[i].getCourse().equals(s)) {
count6++;
}
}
}
//Display each year count
System.out.println("2010-11 "+s+"has "+count1+" number of students");
System.out.println("2011-12 "+s+"has "+count2+" number of students");
System.out.println("2012-13 "+s+"has "+count3+" number of students");
System.out.println("2013-14 "+s+"has "+count4+" number of students");
System.out.println("2014-15 "+s+"has "+count5+" number of students");
System.out.println("2015-16 "+s+"has "+count6+" number of students");
}
//Function to return letter grade
public static char averageGrade(Grade[] g, int n, String s) {
//Variable to take count of each grade
int countA=0,countB=0,countC=0,countD=0,countF=0,count=0;
//Loop through the array
for(int i = 0; i < n; i++) {
{
//If equal course then check grade and increment count and find total count
if(g[i].getCourse().equals(s)) {
if(g[i].getGrade()=='A') {
countA++;
count++;
}
else if(g[i].getGrade()=='B') {
countB++;
count++;
}
else if(g[i].getGrade()=='C') {
countC++;
count++;
}
else if(g[i].getGrade()=='D') {
countD++;
count++;
}
else if(g[i].getGrade()=='F') {
countF++;
count++;
}
}
}
}
//Find each average
double diffA=0,diffB=0,diffC=0,diffD=0,diffF=0;
diffA=((double)countA)/count;
diffB=((double)countB)/count;
diffC=((double)countC)/count;
diffD=((double)countD)/count;
diffF=((double)countF)/count;
double large=diffA;
if(large<diffB)
large=diffB;
if(large<diffC)
large=diffC;
if(large<diffD)
large=diffD;
if(large<diffF)
large=diffF;
if(large==diffA)
return 'A';
else if(large==diffB)
return 'B';
else if(large==diffC)
return 'C';
else if(large==diffD)
return 'D';
else
return 'F';
}
//Function to return average of students in particular course
public static double averageStudentsNumber(Grade[] g, int n, String s) {
//Variable for count
int countCourse=0,count=0;
double avg = 0;
for(int i = 0; i < n; i++) {
{
if(g[i].getCourse().equals(s)) {
countCourse++;
}
count++;
}
}
avg=(double)(countCourse)/count;
return avg;
}
public static void main(String args[]) {
//array of grade objects
Grade grades[] = new Grade[413940];
//reads file of grades
Scanner input = null;
try{
input = new Scanner(new File("C:/Users/deept/Desktop/grades.csv"));
} catch(FileNotFoundException e) {
e.printStackTrace();
}
//to get rid of header
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++;
}//end while
//Finds the average grade for a course
System.out.println("Averae grade in CSPC-39: " + averageGrade(grades, n, "CSPC-39"));
//Finds the number of students per year for a specific course
studentsPerYear(grades, n, "CSPC-39");
//Finds the average number of the students for a course
System.out.println("Averae grade in CSPC-39: " + averageStudentsNumber(grades, n, "CSPC-39"));
}
}
-------------------------------------------
Ouput(based on my csv)
Grade [Year=2010-11, Course=CSPC-39, Grade=A]
Grade [Year=2010-11, Course=CSPC-39, Grade=A]
Averae grade in CSPC-39: A
2010-11 CSPC-39has 2 number of students
2011-12 CSPC-39has 0 number of students
2012-13 CSPC-39has 0 number of students
2013-14 CSPC-39has 0 number of students
2014-15 CSPC-39has 0 number of students
2015-16 CSPC-39has 0 number of students
Averae grade in CSPC-39: 1.0
----------------------------------------------------
Note
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.