JAVA Programming Implement a class Student A student has a n
JAVA Programming
Implement a class Student. A student has a name and a total quiz score. Supply an appropriate constructor, and methods getName(), addQuiz(int score), getTotalScore(), and getAverageScore(). To compute the latter, also need to store the number of quizzes that the student took.
Then, modify the student class to computer grade point averages. Methods are needed to add a grade and get the current GPA. Specify the grades as elements of a class Grade. Supply a constructor that constructs a grade from a string, such as “B+”. You will also need a method that translates grades into their numeric values (for example, “B+” becomes 3.3).
Solution
public class Student {
String name;
int quizScore;
int noOfQuizes;
/**
* @param name
* @param quizScore
* @param noOfQuizes
*/
public Student(String name, int quizScore) {
this.name = name;
this.quizScore = quizScore;
this.noOfQuizes = 1;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the quizScore
*/
public int getTotalQuizScore() {
return quizScore;
}
public void addQuiz(int score) {
quizScore += score;
noOfQuizes++;
}
public double getAverageScore() {
return getTotalQuizScore() / (double) noOfQuizes;
}
}
public class Grade implements Comparable<Grade> {
private String grade;
public Grade(String grade) {
this.grade = grade;
if (getGPA() < 0.0)
throw new IllegalArgumentException(\"Illegal grade\");
}
public double getGPA() {
if (grade.equals(\"A\"))
return 4.00;
if (grade.equals(\"B\"))
return 3.00;
if (grade.equals(\"C\"))
return 2.00;
if (grade.equals(\"D\"))
return 1.00;
if (grade.equals(\"F\"))
return 0.00;
if (grade.equals(\"B+\"))
return 3.33;
if (grade.equals(\"C+\"))
return 2.33;
if (grade.equals(\"A-\"))
return 3.67;
if (grade.equals(\"B-\"))
return 2.67;
if (grade.equals(\"C-\"))
return 1.67;
return -1.0;
}
public int compareTo(Grade that) {
return Double.compare(this.getGPA(), that.getGPA());
}
// use %-2s flag to left justify
public String toString() {
return String.format(\"%-2s %3.2f\", grade, getGPA());
}
}

