?
Solved

Comparable compareTo method

Posted on 2004-11-28
16
Medium Priority
?
1,073 Views
Last Modified: 2013-11-23
I have a class with this signature:
public class MyVertex implements Vertex, Comparable {

I am receiving this error:
Class must implement the inherited abstract method Comparable.compareTo(Object)

but am not sure exactly what i must implement. I know that in my program i never HAVE to use whatever method i need to implement to satisfy this condition, but i do know this:
For each vertex, there is a list of incident edges associated with it inside of the vertex class...
If the edges are inserted in sorted order inside the adjacency list of
their vertex, the compareTo method would be use.
Remember that the compareTo method is used by the list. And list are
used for list of vertices, list of edges and adjacency list. If you
have 2 objects a and b, the method would return -1 if a < b, 0 if a==b
and 1 if a>b when calling a.compareTo(b)
I do NOT NEED to use this compareTo method, since it is not NECESSARY to sort the adjacency list...however to satisfy the List requirements, i must HAVE a compareTo implementation...how i do this exactly is the question.

NOTE: Vertex extends Position, and MyVertex has import List.*; in its heading.
0
Comment
Question by:D4Ly
  • 8
  • 7
16 Comments
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 2000 total points
ID: 12691474

You have to implement the method compareTo, because your class implements the Comparable interface.

To satisfy the compiler, all you have to do, is put this in the MyVertex class:

  public int compareTo(Object o)
  {
     int result = 0;
/*    
 Here you should determine the result:
if (this < o) --> result = -1;
if (this = o) --> result = 0;
if (this > o) --> result = 1;    

How to determine which of the objects is bigger is up to you. Probably you will compare their properties

If you leave it like this, all  MyVertex objects will appear as if they were equal. That is no good, you have to stick to the rules of implementing compareTo method.

You can read the exact rules here:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html#compareTo(T)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html

*/    
     return result;
  }






0
 
LVL 9

Author Comment

by:D4Ly
ID: 12691495
Thanks dejanpazin. I will use this:
public int compareTo(Object o){
          int result = 0;
          return result;
      }
as my default. Like I said, I have no intention or need at this point to compare anything...just trying to satisfy the compiler.  What I WOULD do is compare the unique data (in this case represents weight) for each vertex here, returning the heavier of the two vertices, meaning I would have a public Vertex compareTo(Object o) instead of the above method signature.

Thanks for your help. BAM. 500pts.
0
 
LVL 92

Expert Comment

by:objects
ID: 12691752
if you remove implements Comparable from the class definition what exactly fails to compile?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:D4Ly
ID: 12691812
I haven't made it there yet...i'll let you know...i'm just getting an error displaying prior to the compile telling me i must implement this method.
0
 
LVL 92

Expert Comment

by:objects
ID: 12691911
You shouldn't be adding a dummy compareTo method, if its not Comparable then you shouldn't be declare it as being comparable.

0
 
LVL 9

Author Comment

by:D4Ly
ID: 12691939
I didn't make it implement Comparable. It came that way, and I must leave it.  From what I understand this is what makes it easy for me to add instances of myVertex to a List.

Now, i am having a problem with inserting these instances ;p

ok, so in my main class when inserting a new vertex, this is called:
       public Vertex insertVertex(Object data) {
            MyVertex v = new MyVertex(data);
            _vList.insert(v); //ERROR HERE
            return (Vertex) v;
      }

_vList is declared as       protected List _vList; and is initially set to null in the constructor.
data is simply a weight value unique to each vertex.

I get a nullpointerexception on the marked line. data's value at the time of the method error is = 0

If i'm inserting an object (instance of MyVertex) that DOES exist, why do i get a nullpointerexception when inserting it?
0
 
LVL 92

Expert Comment

by:objects
ID: 12691953
> It came that way, and I must leave it.

Then you more than likely should be implementng it correctly :)
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12691970
Here's what my teacher told me about this:
The reason why you are required to implement the comparable interface
on Edges/Vertices is due to the fact that they are inserted in lists
that do require this capability. It does not mean that you absolutely
have to use it.

and then:
IF the edges are inserted in sorted order inside the adjacency list of
their vertex, the compareTo method would be use.
Remember that the compareTo method is used by the list. And list are
used for list of vertices, list of edges and adjacency list. If you
have 2 objects a and b, the method would return -1 if a < b, 0 if a==b
and 1 if a>b when calling a.compareTo(b)

The UGraph(the main class) uses lists for its internal representation. And list expects
to receive comparable. Now, in your implementation, nothing _forces_
you to use insertSorted. If you restrict yourself to insert/append and
the other methods that do not use the compareTo method, you would not
be affected by how it is implemented.

any suggestions about my previous post on the error i get?
0
 
LVL 92

Expert Comment

by:objects
ID: 12691999
that exception is caused by _vList being null.
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12692017
even when not setting _vList to null in the constructor, the error still occurs...can you elaborate?
0
 
LVL 92

Expert Comment

by:objects
ID: 12692025
_vList being null *is* the problem
you cannot call a method on a null, thats what causes NPE's.
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12692040
right...so what is the work-around for putting data in this empty list if i instantiate a list, and am not allowed to call the method to put something in making it NOT null? I have a _vList i want to store data in. How do I begin this data storing process?
0
 
LVL 92

Expert Comment

by:objects
ID: 12692176
you need to create an list and have _vList reference it.
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12692208
i don't understand how this helps...nor how to implement what you're suggesting.  sorry.
0
 
LVL 92

Expert Comment

by:objects
ID: 12692231
for example if you are using Vectors you cannot use:

Vector v = null;
v.add("abc");

because a Vector has not been created to add elements to

instead you need to use:

Vector v = new Vector();
v.add("abc");
0
 
LVL 9

Author Comment

by:D4Ly
ID: 12692260
thank you!.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question