Results 1 to 11 of 11
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default A music collection printer, questions about 2 String toString methods returned maybe

    Hi, well I understand (almost) my biggest program yet. 3 pages !! almost 3 anyway. And my question is, in the code, which I comment the questions in green below, the code has 2 toString methods that output string data. And that is confusing me because it looks like both methods print out the same material. Any help Greatly appreciated. thank you! Derek


    here it is.


    Java Code:
    //********************************************************************
    //  CDCollection.java       Author: Lewis/Loftus
    //
    //  Represents a collection of compact discs.
    //********************************************************************
    
    import java.text.NumberFormat;
    
    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()[B][COLOR="darkgreen"]//notice this is the first of 2 outputs of the program[/COLOR][/B]
       {
          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;
       }
    }
    and here is the second class


    Java Code:
    //********************************************************************
    //  CD.java       Author: Lewis/Loftus
    //
    //  Represents a compact disc.
    //********************************************************************
    
    import java.text.NumberFormat;
    
    public class CD
    {
       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()[B][COLOR="darkgreen"]// notice this seems to output the same information??[/COLOR][/B]
       {
          NumberFormat fmt = NumberFormat.getCurrencyInstance();
    
          String description;
    
          description = fmt.format(cost) + "\t" + tracks + "\t";
          description += title + "\t" + artist;
    
          return description;
       }
    }
    Last edited by silverglade; 05-21-2011 at 01:16 PM.

  2. #2
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    oh wait, I think I see what they do.

    EDIT: nope still don't know what the second one does.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The CDCollection classes toString works on an array of cd objects. It creates some display up top then loops through the array of CDs and adds each CDs toString onto it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by silverglade View Post
    Hi, well I understand (almost) my biggest program yet. 3 pages !! almost 3 anyway. And my question is, in the code, which I comment the questions in green below, the code has 2 toString methods that output string data. And that is confusing me because it looks like both methods print out the same material. Any help Greatly appreciated. thank you! Derek
    One is the toString method for the CD class -- and as expected, this outputs the data held by a single CD object, and the other toString method for the CDCollection class -- and as expected outputs the data for all the CD's held by the collection. What in particular confuses you about this?

  5. #5
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    EDIT. I didn't see Fubarable's post. one sec

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Best way to test the difference is to create a single cd object and print it, then create a cd collections with > 1 cd and observe how they differ.

  7. #7
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    The example code never creates a CD object. Only one CDCollection object. The ouput is like this "System.out.println (music)" and the second output is the exact same thing. They never create a CD object or call the toString method for the CD class. Maybe they just forgot to?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by silverglade View Post
    The example code never creates a CD object. Only one CDCollection object. The ouput is like this "System.out.println (music)" and the second output is the exact same thing. They never create a CD object or call the toString method for the CD class. Maybe they just forgot to?
    Then create a couple CD's yourself add them to a collection and see what happens.

    Edit: the collection creates CD's when you call the addCD method
    Last edited by Fubarable; 05-21-2011 at 01:34 PM.

  9. #9
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    ok be right back

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You don't need to explicitly call toString when printing an object,
    Java Code:
    CD x = ...;
    System.out.println(x);
    Like I said, add to there main method and include the creation of a single cd, then print it. See if you can see a difference.

  11. #11
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Ok I did it. The CD toString method just adds a cd at the end of the CdCollection class toString method. That's it. LOL. Oh well.

    Here is what I did, I hope it is correct.


    Java Code:
    //********************************************************************
    //  Tunes.java       Author: Lewis/Loftus
    //
    //  Demonstrates the use of an array of objects.
    //********************************************************************
    
    public class Tunes
    {
       //-----------------------------------------------------------------
       //  Creates a CDCollection object and adds some CDs to it. Prints
       //  reports on the status of the collection.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          CDCollection music = new CDCollection ();
    
          music.addCD ("Storm Front", "Billy Joel", 14.95, 10);
         //ALL OTHER CD'S REMOVED
    
    
    
    
          System.out.println (music);
    
          //OK HERE IS MY ADDED CODE TO TEST CD
    
           CD testCD = new CD ("Doggy","Fat Boy", 3.00, 2);
    
           System.out.println (testCD);
       }
    }

    and here was the output when program ran.

    Java Code:
    C:\JAVA_PROGRAMMING_CODE\code1\Tunes>java Tunes
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    My CD Collection
    
    Number of CDs: 1
    Total cost: $14.95
    Average cost: $14.95
    
    CD List:
    
    $14.95  10      Storm Front     Billy Joel
    
    $3.00   2       Doggy   Fat Boy
    
    C:\JAVA_PROGRAMMING_CODE\code1\Tunes>

Similar Threads

  1. Replies: 3
    Last Post: 05-12-2011, 05:07 AM
  2. error with String toString()
    By b.m in forum New To Java
    Replies: 9
    Last Post: 12-16-2010, 02:02 PM
  3. General questions about Java Garbage Collection
    By tutiplain in forum New To Java
    Replies: 5
    Last Post: 11-19-2010, 03:57 PM
  4. Java Collection class & methods
    By box2box in forum New To Java
    Replies: 2
    Last Post: 05-26-2010, 01:51 AM
  5. Inheritance, methods, and toString...
    By Kreuz14 in forum New To Java
    Replies: 1
    Last Post: 04-01-2008, 10:12 PM

Posting Permissions

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