Results 1 to 4 of 4
  1. #1
    SungsooKim is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default Biginner! Array to LinkedList

    Hello guys.

    now I am studying about data structure. And I have some problems solving quizz

    I have to change from array to linkedlist

    But I am not sure which part I have to change.

    Do I have to use nodes class? or just use linkedlist methods.

    this is codes that I have to change

    import java.text.NumberFormat;

    public class CD
    {
    private String title, artist;
    private double cost;
    private int tracks;


    public CD (String name, String singer, double price, int numTracks)
    {
    title = name;
    artist = singer;
    cost = price;
    tracks = numTracks;
    }

    public String toString()
    {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    String description;

    description = fmt.format(cost) + "\t" + tracks + "\t";
    description += title + "\t" + artist;

    return description;
    }
    }
    //drive class

    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);
    music.addCD ("Come On Over", "Shania Twain", 14.95, 16);
    music.addCD ("Soundtrack", "Les Miserables", 17.95, 33);
    music.addCD ("Graceland", "Paul Simon", 13.90, 11);

    System.out.println (music);

    music.addCD ("Double Live", "Garth Brooks", 19.99, 26);
    music.addCD ("Greatest Hits", "Jimmy Buffet", 15.95, 13);

    System.out.println (music);
    }
    }
    //I have to change this class...
    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()
    {
    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;
    }
    }

    // I changed above class like this
    import java.text.NumberFormat;
    import java.util.*;


    public class CDCollection
    {
    private CD[] collection;
    private int count;
    private double totalCost;
    LinkedList<CD> list = new LinkedList<CD>();
    //-----------------------------------------------------------------
    // Constructor: Creates an initially empty collection.
    //-----------------------------------------------------------------
    public CDCollection ()
    {
    collection = new CD[100];
    count = 0;
    totalCost = 0.0;
    }

    public void addCD (String title, String artist, double cost,
    int tracks)
    {
    for(int i = 0; i < collection.length; i++) {
    collection[i] = new CD(title,artist,cost,tracks);
    list.add(collection[i]);
    count =i;
    }

    }


    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 result is horrible...= (

    Do you have any idea about this?

    Thank you
    Last edited by SungsooKim; 09-28-2009 at 09:09 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Please use code tags if you have to post code.
    Are you supposed to write your own LinkedList class or are you allowed to use java.util.LinkedList?

  3. #3
    SungsooKim is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default Well

    I don't care....better way is better..=)

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    So what is this horrible result? Clean up your code first though. If you have a List<CD> you no longer need a CD[]. Get rid of that from your CDCollection class. Your add method should also change to simply add to the list.

Similar Threads

  1. Help with LinkedList<AnyType>
    By wesgarner in forum New To Java
    Replies: 2
    Last Post: 09-24-2009, 04:00 PM
  2. LinkedList help
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 09-19-2009, 08:24 AM
  3. Creating Array of LinkedList
    By sasikumardr in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 11:25 AM
  4. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 02:52 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
  •