469,328 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

compareTo Help

authorityaction
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
4 12167
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
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
Laharl
849 Expert 512MB
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
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.

Similar topics

2 posts views Thread by Jon Skeet [C# MVP] | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.