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.
LVL 9
D4LyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dejan PažinHead of SW DevelopmentCommented:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
D4LyAuthor Commented:
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
objectsCommented:
if you remove implements Comparable from the class definition what exactly fails to compile?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

0
D4LyAuthor Commented:
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
objectsCommented:
> It came that way, and I must leave it.

Then you more than likely should be implementng it correctly :)
0
D4LyAuthor Commented:
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
objectsCommented:
that exception is caused by _vList being null.
0
D4LyAuthor Commented:
even when not setting _vList to null in the constructor, the error still occurs...can you elaborate?
0
objectsCommented:
_vList being null *is* the problem
you cannot call a method on a null, thats what causes NPE's.
0
D4LyAuthor Commented:
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
objectsCommented:
you need to create an list and have _vList reference it.
0
D4LyAuthor Commented:
i don't understand how this helps...nor how to implement what you're suggesting.  sorry.
0
objectsCommented:
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
D4LyAuthor Commented:
thank you!.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.