By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

compareTo Help

authorityaction
P: 6
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:

Expand|Select|Wrap|Line Numbers
  1. Polynomial.java:79: cannot find symbol
  2. symbol  : variable degree
  3. location: class java.lang.Object
  4.             if (this.degree == ob1.degree)
  5.                                   ^
  6. Polynomial.java:81: cannot find symbol
  7. symbol  : variable degree
  8. location: class java.lang.Object
  9.             else if (this.degree > ob1.degree)
  10.  
here is my compareTo method:

Expand|Select|Wrap|Line Numbers
  1. public int compareTo(Object ob1)
  2.         {
  3. if (this.degree == ob1.degree)
  4. return 0;
  5. else if (this.degree > ob1.degree)
  6. return 1;
  7. else
  8. return -1;
  9.         }
I have a feeling that my error will be something simple. Thanks in advance for any help!
Feb 11 '08 #1
Share this Question
Share on Google+
4 Replies


P: 8
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:

Expand|Select|Wrap|Line Numbers
  1. Polynomial.java:79: cannot find symbol
  2. symbol  : variable degree
  3. location: class java.lang.Object
  4.             if (this.degree == ob1.degree)
  5.                                   ^
  6. Polynomial.java:81: cannot find symbol
  7. symbol  : variable degree
  8. location: class java.lang.Object
  9.             else if (this.degree > ob1.degree)
  10.  
here is my compareTo method:

Expand|Select|Wrap|Line Numbers
  1. public int compareTo(Object ob1)
  2.         {
  3. if (this.degree == ob1.degree)
  4. return 0;
  5. else if (this.degree > ob1.degree)
  6. return 1;
  7. else
  8. return -1;
  9.         }
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(Polynomial ob1)
Feb 11 '08 #2

authorityaction
P: 6
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(Polynomial ob1)
I had this thought earlier as well. When I change it, I get this error:

Expand|Select|Wrap|Line Numbers
  1. Polynomial.java:28: Polynomial.Term is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
  2.     private class Term implements Comparable
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.
Feb 11 '08 #3

Expert 100+
P: 849
By implementing the Comparable interface, you have to override its compareTo() function. The signature of this function is as follows:

Expand|Select|Wrap|Line Numbers
  1. public int compareTo(Object)
  2.  
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.

Expand|Select|Wrap|Line Numbers
  1. public class Foo implements Comparable {
  2.  private int bar;
  3.  public int compareTo(Object o){
  4.   Foo other = (Foo) o;
  5.   if (this.bar > other.bar)
  6.    return 1;
  7.   else if (this.bar < other.bar)
  8.    return -1;
  9.   return 0;
  10.  }
  11. }
  12.  
Feb 11 '08 #4

authorityaction
P: 6
Thank you Laharl! Casting to the correct object did the trick.
Feb 11 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.