interfaces: type mismatch with int

Joined
Jun 13, 2010
Messages
1
Reaction score
0
i'm trying to work through an example in my textbook, but it doesn't show the complete source code for the example, just snippets that are updates to a code from a previous chapter.

the issue here is the previous chapter (on iteration) doesn't involve the "Measurable" interface and just uses double's. the original constructor works because "maximum" was a double and can just be set to 0. but in this chapter the author introduces the Measurable interface. getMaximum() has return-type Measurable, so the instance field "maximum" also has to be "Measurable".

One solution i have is adding a setMeasure() method to the Measurable interface. I'm pretty sure it would work, but it is inappropriate because it would only work for this DataSet class, which defeats the purpose of the interface, I think. I also tried cast the "0" to a Measurable, but that also does not work.

So, how can I edit this DataSet class to work with the Measurable interface.

the specific error is:
Type mismatch: cannot convert from int to Measurable

--the DataSet class---
Code:
public class DataSet {
	public DataSet() {
		sum = 0;
		count = 0;
		maximum = 0;  //-------------ERROR
	}
	public void add(Measurable x) {
		sum += x.getMeasure();
		if (count == 0 || maximum.getMeasure() < x.getMeasure() )
			maximum = x;
		count++;
	}
	public double getAverage() {
		if (count == 0)
			return 0;
		else return sum / count;
	}
	public Measurable getMaximum() {
		return maximum;
	}
	private double sum;
	private Measurable maximum;
	private int count;
--the Measurable interface---
Code:
public interface Measurable {
	double getMeasure();
}

thanks, if any one can help, i've been stuck on this for a while.
PS: the book is Big Java by Cay Horstman 3rd Edition 2007.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top