So here's my assignment: Define a class Quiz that implements the Measurable interface. A quiz has a score and a letter grade (such as B+). Use the implementation of the DataSet class in Section 9.1 to process a collection of quizzes. Display the average score and the quiz with the highest score (both letter grade and score).
I think I've almost got it, but I get a type mismatch error in the tester class on line 18: Quiz max = quizData.getMaximum();
What am I missing here? I don't understand... any help is GREATLY appreciated.
public class Quiz implements Measurable {
private double score;
private String grade;
public Quiz(double aScore, String aGrade)
{
score = aScore;
grade = aGrade;
}
@Override
public double getMeasure() {
return score;
}
public void setMeasure(double s) {
score = s; }
public String getGrade() {
return grade;
}
public double getScore() {
return score;
}
}
public class DataSet {
private double sum;
private Measurable maximum;
private int count;
public DataSet() {
sum = 0;
count = 0;
maximum = null;
}
/**
* Adds a data value to the data set
*
* @param x
* a data value
*/
public void add(Measurable x) {
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
maximum = x;
count++;
}
/**
* Gets the largest of the added data.
*
* @return the maximum or 0 if no data has been added
*/
public Measurable getMaximum() {
return maximum;
}
public double getAverage() {
if (count == 0)
return 0;
else
return sum / count;
}
}
This is the tester class that was provided by the Instructor.
/**
This program tests the Quiz and DataSet classes.
*/
public class QuizTester
{
public static void main(String[] args)
{
DataSet quizData = new DataSet();
Quiz q1 = new Quiz(89, "B+");
Quiz q2 = new Quiz(90, "A-");
Quiz q3 = new Quiz(73, "C");
quizData.add(q1);
quizData.add(q2);
quizData.add(q3);
double avg = quizData.getAverage();
Quiz max = quizData.getMaximum();
System.out.println("Average score: " + avg);
System.out.println("Expected: 84");
System.out.println("Highest score: " + max.getScore());
System.out.println("Expected: 90");
System.out.println("Highest grade: " + max.getGrade());
System.out.println("Expected: A-");
}
}