Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Comparable Interface

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    Default Comparable Interface

    Comparable Interface In Java?

    public class CD implements Comparable 
    private String title, artist;
    private double cost;
    private int tracks;
    // Creates a new CD with the specified information.
    public CD (String name, String singer, double price, int numTracks)
    title = name;
    artist = singer;
    cost = price;
    tracks = numTracks;
    // Returns a string description of this CD.
    public String toString()
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    String description;
    description = fmt.format(cost) + "\t" + tracks + "\t";
    description += title + "\t" + artist;
    return description;
    public int compareTo(Object o) 
    CD other = (CD) o; 
    int result = (this.title.compareTo(other.title) 
    if (result == 0) 
    result = (this.artist.compareTo(other.artist);
    return result; 

    Public class CDCollection
       private CD[] collection;
       private int count;
       private double totalCost;
       //  Constructor: Creates an initially empty collection.
       public CDCollection ()
          collection = new CD[100];
          count = 0;
          totalCost = 0.0;
       //  Adds a CD to the collection, increasing the size of the
       //  collection if necessary.
       public void addCD (String title, String artist, double cost,
                          int tracks)
          if (count == collection.length)
          collection[count] = new CD (title, artist, cost, tracks);
          totalCost += cost;
       //  Returns a report describing the CD collection.
       public String toString()
          NumberFormat fmt = NumberFormat.getCurrencyInstance();
          String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
          report += "My CD Collection\n\n";
          report += "Number of CDs: " + count + "\n";
          report += "Total cost: " + fmt.format(totalCost) + "\n";
          report += "Average cost: " + fmt.format(totalCost/count);
          report += "\n\nCD List:\n\n";
          for (int cd = 0; cd < count; cd++)
             report += collection[cd].toString() + "\n";
          return report;
       //  Increases the capacity of the collection by creating a
       //  larger array and copying the existing collection into it.
       private void increaseSize ()
          CD[] temp = new CD[collection.length * 2];
          for (int cd = 0; cd < collection.length; cd++)
             temp[cd] = collection[cd];
          collection = temp;

    The problem is that I am adding a method from CD to another class COLLECTIONS using the compareTo from this method and I can't figure out. Here are the specs:

    Add a findCD() method
    It has two input parameters: title and artist.
    This method returns the CD object that matches the title and artist.
    It returns a null if no match is found.

    Any help would be appreciated.
    Last edited by yelrubk; April 28th, 2010 at 03:07 AM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Comparable Interface

    If I understand your problem correctly, you wish to add a findCD() method to your CDCollection class? Seems to me the easiest way to do so is to keep your CD's in a HashSet, but if that is too complex just iterate over your array. Depending upon how you want to search, you may wish to create a 'dummy' CD based upon the function parameters, say a title and artist to search, then use the compareTo (or override the equals method) to check if they are the same.

Similar Threads

  1. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM
  2. Interface problem please help!!
    By joff1403 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2010, 11:09 AM
  3. Interface Implementation
    By Samyx in forum Object Oriented Programming
    Replies: 1
    Last Post: December 2nd, 2009, 03:46 AM
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM
  5. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 02:49 AM