473,320 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 12356
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

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

Similar topics

5
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the...
0
by: RSB | last post by:
Hi Every one, i am trying to create a UserControl and i am passing a Array of strings to it. Now based on the Array elements i am creating the LinkButtons Dynamically. I am also passing a Event to...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
2
by: zaidalin79 | last post by:
r03581x has been helping me with a program, but he won't be online till Monday, and I have to have this finished by tomorrow... Is there anyone else out there that can help me? Here is the first...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
3
by: splintercell | last post by:
well i got this code from java.sun.com and tried modiifying it in all the possible ways,but to no good.. stil its not workin..pleas help me out and try postin good workinw web cralwer if u have.....
3
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not...
2
by: Jon Skeet [C# MVP] | last post by:
Author <gnewsgroup@gmail.comwrote: Where? It looks to me like it's calling double's implementation of CompareTo. -- Jon Skeet - <skeet@pobox.com> Web site: http://www.pobox.com/~skeet Blog:...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.