473,686 Members | 2,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compareTo Help

authorityaction
6 New Member
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 12367
kedmotsoko
8 New Member
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(Polyn omial ob1)
Feb 11 '08 #2
authorityaction
6 New Member
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:

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 Recognized Expert Contributor
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
6 New Member
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
5433
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 generic.List.IndexOf function doesn't appear to be invoking the contained class' CompareTo method. My understanding is that it should. The contained class (IssStruct) implements the IComparable and IComparable<T> interfaces. However, the List.Sort function...
0
1661
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 this control and Lining the OnClick event of these LinkButtons to this Event. (Which works fine). Now the Thing which i cannot achieve is i want to Change the Back Color of the Clicked to LinkButton To a different color and i also don't want to...
5
3785
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 List<RoleData> GetRoles() { return GetRoles(null, false); }
2
2888
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 half of what I have so far... I will post the rest of the code in a second post cause it is too long... import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; class Product implements Comparable
4
2169
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 help on this, Benny
3
1987
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.. need help asap... import java.applet.Applet; import java.text.*; import java.awt.*; import java.awt.List; import java.awt.event.*; import java.util.*;
3
4740
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 even sure if the way I've written this is going to work but anyway, I keep getting a compilation error that says: C:\Documents and Settings\Sam\GUICDInventory.java:22: cannot find symbol symbol : constructor JList(CDInventory) location: class...
2
1741
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: http://www.msmvps.com/jon_skeet C# in Depth: http://csharpindepth.com
0
1804
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 overridden by the type. The reason is because the CLR can just call these methods nonvirtually and System.ValueType overrides all of these virtual methods and expects the value in the this argument to refer to an unboxed value type instance. Remember, a...
0
8580
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8513
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8931
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6437
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4526
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2943
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2203
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.