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---
--the Measurable interface---
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.
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;
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.