Link to home
Start Free TrialLog in
Avatar of D4Ly
D4Ly

asked on

Comparable compareTo method

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.
ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of D4Ly
D4Ly

ASKER

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.
if you remove implements Comparable from the class definition what exactly fails to compile?
Avatar of D4Ly

ASKER

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.
You shouldn't be adding a dummy compareTo method, if its not Comparable then you shouldn't be declare it as being comparable.

Avatar of D4Ly

ASKER

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?
> It came that way, and I must leave it.

Then you more than likely should be implementng it correctly :)
Avatar of D4Ly

ASKER

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?
that exception is caused by _vList being null.
Avatar of D4Ly

ASKER

even when not setting _vList to null in the constructor, the error still occurs...can you elaborate?
_vList being null *is* the problem
you cannot call a method on a null, thats what causes NPE's.
Avatar of D4Ly

ASKER

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?
you need to create an list and have _vList reference it.
Avatar of D4Ly

ASKER

i don't understand how this helps...nor how to implement what you're suggesting.  sorry.
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");
Avatar of D4Ly

ASKER

thank you!.