compareTo Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • authorityaction
    New Member
    • Feb 2008
    • 6

    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!
  • kedmotsoko
    New Member
    • Feb 2008
    • 8

    #2
    Originally posted by 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:

    [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)

    Comment

    • authorityaction
      New Member
      • Feb 2008
      • 6

      #3
      Originally posted by kedmotsoko
      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)
      I had this thought earlier as well. When I change it, I get this error:

      [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.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        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]

        Comment

        • authorityaction
          New Member
          • Feb 2008
          • 6

          #5
          Thank you Laharl! Casting to the correct object did the trick.

          Comment

          Working...