Results 1 to 4 of 4
  1. #1
    Yelrubk is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Comparable Interface

    Comparable Interface In Java?


    Java Code:
    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; 
    }
    Java Code:
    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)
             increaseSize();
    
          collection[count] = new CD (title, artist, cost, tracks);
          totalCost += cost;
          count++;
       }
    
       //-----------------------------------------------------------------
       //  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; 04-28-2010 at 09:45 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    What does that have to do with compareTo?
    Surely you just want an equals method?

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Tolls View Post
    What does that have to do with compareTo?
    Surely you just want an equals method?

    if you want to sort objects in arrays then you have to override the compareTo method.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Except, if you read the post, they're looking to find something in the array based on name and title. That's an equals(), not a compareTo().

Similar Threads

  1. Replies: 6
    Last Post: 04-28-2010, 12:49 AM
  2. Convert Comparable object to string or char
    By ScKaSx in forum New To Java
    Replies: 4
    Last Post: 01-25-2009, 02:02 PM
  3. Creating a Comparable object
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:38 PM
  4. interface Comparable<T> problem
    By Lennon-Guru in forum New To Java
    Replies: 3
    Last Post: 03-05-2008, 12:17 AM
  5. Using Comparable and Comparator interfaces
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •