Comparable confusion

Hi,

I find Comparable confusing.

In one of the notes, I read this. (The code is really lenght, so i just write the skeleton)

public class gsSort
{

public static void sort(Comparable[] a,int size)
{
int i=0;.....

for(i=0;i<size-1;i++)
int indexofsmallest=findSmallest(index,a,size);
change(i,indexofsmallest,a);
}

}
public static int findSmallest(int index, Comparable a,int size)
{

Comparable min=a[index];
int indexofmin=index;

for(int i=index+1;i<size;i++)
if(a[i].compareTo(min)<0)
{
min=a[i];
indexofmin=i;
}
return indexofmin;
}

public static void change(int i,int j,Comparable[]a)
{
Comparable temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

public class demo{

public static void main(String []args)
{

Double []d=new Double[10];

..
....
gsSort.sort(d,d.length);
...
}
}


Does the above class implements the Comparable interface ? , if yes, i thought it should have the word implement

something like public class demo implements Comparable.....

thanks

zizi21Asked:
Who is Participating?
 
sciuriwareConnect With a Mentor Commented:
Indeed String and Double implement Comparable and do have a method 'compareTo()'

The rule is: if you implement Comparable, then you MUST have a method 'compareTo()'.

;JOOP!
0
 
basicinstinctCommented:
that's exactly what it should say - implements Comparable - otherwise it's not implementing the Comparable interface
0
 
zizi21Author Commented:
but this code is from my lecture notes which is from a textbook and it says this" Both the Double and String classes implement the Comparable interfaces"..
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
basicinstinctCommented:
yes, and have you looked at the source code for the String and Double classes?
0
 
sciuriwareCommented:
It is legal to have a method 'compareTo()' and NOT to implement Comarable.
In effect the name 'compareTo()' is a free choice.
Until you "promise" to have one.

;JOOP!
0
 
sciuriwareCommented:
From the official source of String.java (it is 3003 lines of code!):


....................................
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];
..........................................
    public int compareTo(String anotherString) {
      int len1 = count;
      int len2 = anotherString.count;
      int n = Math.min(len1, len2);
      char v1[] = value;
      char v2[] = anotherString.value;
      int i = offset;
      int j = anotherString.offset;

...................................

;JOOP!
0
 
zizi21Author Commented:
Sorry..if this is obvious.. but do you meant to say that :

Because in my main, I am using Double and String objects, the Double and String classes already has
(the word implements Comparable), I have already implemented Comparable and do not need to enforce it in my class

public class gsSort. thanks
0
 
basicinstinctConnect With a Mentor Commented:
Hi zizi

Short answer, 'no', your class does not implement Comparable.

The Double and String classes implement comparable.  Your class does not implement comparable just because it uses classes that do so.

You only need to implement comparable in your class if it represents some kind of object that can be compared to another.  Strings can be compared to each other in terms of alphabetical ordering.  I can't really see why your class needs to implement comparable.


0
 
zizi21Author Commented:
sorry..just finished two other exams..going back to java..forgive me for the late reply...
0
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.

All Courses

From novice to tech pro — start learning today.