compareTo Help
Collapse
X
-
By implementing the Comparable interface, you have to override its compareTo() function. The signature of this function is as follows:
[CODE=java]
public int compareTo(Objec t)
[/CODE]
Due to the way inheritance works, for the method to be overridden, it must have the same signature as the original abstract method. Thus, you have to have an Object parameter, call it compareTo, and return an int. You can cast it to Polynomial so that you can actually do the comparison, but you must take an Object as input.
[CODE=java]
public class Foo implements Comparable {
private int bar;
public int compareTo(Objec t o){
Foo other = (Foo) o;
if (this.bar > other.bar)
return 1;
else if (this.bar < other.bar)
return -1;
return 0;
}
}
[/CODE]Leave a comment:
-
I had this thought earlier as well. When I change it, I get this error:Originally posted by kedmotsokoi don't get it? you class is called Polynomial or Object?
try this... i don't get your situation right...maybe it may work!!
public int compareTo(Polyn omial ob1)
[code=java]Polynomial.java :28: Polynomial.Term is not abstract and does not override abstract method compareTo(java. lang.Object) in java.lang.Compa rable
private class Term implements Comparable[/code]
Also, I don't know if it will make a difference but for the assignment I had to make Term as an inner class of Polynomial. All of the previous code resides in Term.Leave a comment:
-
Originally posted by authorityactionI am writing a polynomial class that later I will use to add two polynomials together. Right now I am working on writing the compareTo method (my class implements Comparable). Unfortunately I am getting the following error:
[CODE=JAVA]Polynomial.java :79: cannot find symbol
symbol : variable degree
location: class java.lang.Objec t
if (this.degree == ob1.degree)
^
Polynomial.java :81: cannot find symbol
symbol : variable degree
location: class java.lang.Objec t
else if (this.degree > ob1.degree)
[/CODE]
here is my compareTo method:
[CODE=JAVA]public int compareTo(Objec t ob1)
{
if (this.degree == ob1.degree)
return 0;
else if (this.degree > ob1.degree)
return 1;
else
return -1;
}[/CODE]
I have a feeling that my error will be something simple. Thanks in advance for any help!
i don't get it? you class is called Polynomial or Object?
try this... i don't get your situation right...maybe it may work!!
public int compareTo(Polyn omial ob1)Leave a comment:
-
compareTo Help
I am writing a polynomial class that later I will use to add two polynomials together. Right now I am working on writing the compareTo method (my class implements Comparable). Unfortunately I am getting the following error:
[CODE=JAVA]Polynomial.java :79: cannot find symbol
symbol : variable degree
location: class java.lang.Objec t
if (this.degree == ob1.degree)
^
Polynomial.java :81: cannot find symbol
symbol : variable degree
location: class java.lang.Objec t
else if (this.degree > ob1.degree)
[/CODE]
here is my compareTo method:
[CODE=JAVA]public int compareTo(Objec t ob1)
{
if (this.degree == ob1.degree)
return 0;
else if (this.degree > ob1.degree)
return 1;
else
return -1;
}[/CODE]
I have a feeling that my error will be something simple. Thanks in advance for any help!
Leave a comment: