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-");
   }
}

Recommended Answers

All 6 Replies

Can you post the imports you used in each class? Given that Quiz implements Measureable there should not be an error unless you've got 2 Measureable classes in your project.

I have no imports... here's the error message.

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Measurable to Quiz

at QuizTester.main(QuizTester.java:18)

You should change the declaration to use Measurable instead of Quiz.

> Given that Quiz implements Measureable there should not be an error
Well, in this case he could explicitly cast the Measurable back to Quiz because he knows that is what he supplied to DataSet, but it wouldn't be a good idea. The entire point of using the interface is to deal with a certain contract of behavior without regard for the class that is implementing it.

It's more appropriate to use the interface type here.

Well, in this case he could explicitly cast the Measurable back to Quiz because he knows that is what he supplied to DataSet, but it wouldn't be a good idea.

Yes. I somehow had the inheritance the other way round in my mind (Measurable implements Quiz).

The entire point of using the interface is to deal with a certain contract of behavior without regard for the class that is implementing it.
It's more appropriate to use the interface type here.

Indeed. @Author: Do this not casting, although both work.

Cast is what the instructor was looking for. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.