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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 4 of 4

Thread: 3 classes - Simple school assignment - ArrayLists

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 3 classes - Simple school assignment - ArrayLists

    Hello everyone,

    I was given an assignment in a JAVA class that seems relatively simple. However, to an inexperienced programmer like myself, this is very confusing. All I need to do is complete the classes for CDCollection and Tunes. Here comes the "What have you tried?" question. Folks, I honestly have no idea where to start. I don't expect much help but anything is appreciated. I will update this frequently with what I have so far.


    What I see is that there is an array list of type CD called collection. When the default constructor runs (no parameters) in CDCollection, it just initializes some values. Looking at the main class (Tunes), when I create an object of type CD Collection, it has no parameters. So it will use that default constructor. Perhaps after I create an object in the main class, I can run music.addCD("Magnolia", "Turnover", 10.0, 11); --- this will call the addCD method, but now I have to create that.






    //  Tunes.java  
    //  Demonstrating the use of an array list of objects.
     
    public class Tunes
    {
       //-----------------------------------------------------------------
       //  Creates a CDCollection object and tests the methods that add, remove, and
       //  report on the status of the collection.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          CDCollection music = new CDCollection ();
     
          // To be implemented
     
          System.out.println (music);
       }
    }




    import java.util.ArrayList ;
    //  Represents a collection of compact discs.
     
    public class CDCollection
    {
       private ArrayList<CD> collection;
       private int count;
       private double totalCost;
     
       //-----------------------------------------------------------------
       //  Creates an initially empty collection.
       //-----------------------------------------------------------------
       public CDCollection ()
       {
          collection = new ArrayList<CD>();
          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)
       {
         // To be implemented
       }
     
       //-----------------------------------------------------------------
       //  Removes a CD from the collection.  Takes appropriate action if the
       //  CD to be removed is not in the collection.
       //-----------------------------------------------------------------
       public String removeCD (String title)
       { String result = null ;
         // To be implemented
         return result ;
       }
     
       //-----------------------------------------------------------------
       //  Returns a report describing the CD collection.
       //-----------------------------------------------------------------
       public String toString()
       {
         String report = null ;
           // To be implemented
         return report ;
       }
    }





     
    //  CD.java      
    //  Represents a compact disc.
     
    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 description of this CD.
       //-----------------------------------------------------------------
       public String toString()
       {
          String description;
     
          description ="\n cost: " + cost + "\ttracks:" + tracks + "\t";
          description += title + "\t" + artist;
     
          return description;
       }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 3 classes - Simple school assignment - ArrayLists

    Why do you have no idea where to start? Did you just wake up after 4 months and realize you were in a Java class when you thought you were a History major? We can't teach you what you've missed or hold your hand through all that you must do to complete this assignment.

    I suggest you read the assignment, review the provided skeletons of all 3 classes so that you have a rough idea of how they work together, and then take each class one at a time and fill in the blanks. Review the instance variables in each class to give you hints what the methods should do. For example, adding a CD to the collection should probably do something with the 'count' variable. You should probably start with CD, then complete CDCollection, and then start testing, starting with the constructor and then every method.

    If you run into problems while testing, come back with code, errors, and questions.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    TSSF44 (February 14th, 2014)

  4. #3
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: 3 classes - Simple school assignment - ArrayLists

    Quote Originally Posted by GregBrannon View Post
    Why do you have no idea where to start? Did you just wake up after 4 months and realize you were in a Java class when you thought you were a History major? We can't teach you what you've missed or hold your hand through all that you must do to complete this assignment.

    I suggest you read the assignment, review the provided skeletons of all 3 classes so that you have a rough idea of how they work together, and then take each class one at a time and fill in the blanks. Review the instance variables in each class to give you hints what the methods should do. For example, adding a CD to the collection should probably do something with the 'count' variable. You should probably start with CD, then complete CDCollection, and then start testing, starting with the constructor and then every method.

    If you run into problems while testing, come back with code, errors, and questions.
    Thank you, I'm working on it as I type. Yes, that is true. I guess I will just have to take more risks trying things before posting questions asking for help

    --- Update ---

    Here's what I have so far:


     public static void main (String[] args)
       {
          CDCollection music = new CDCollection ();
     
          // confused by this whole aspect. Should I be creating new objects for each CD ,or is that not necessary?
     
     
          // CD cdOne = new CD("Help!", "The Beatles", 10.0, 14);
     
           // here I have called the addCD method, passing it some parameters. 
            music.addCD("Help!", "The Beatles", 10.0, 14);
     
     
           System.out.println (music);
       }
    }


     
    //addCD method that I called in the main class 
       public void addCD (String title, String artist, double cost,
                          int tracks)
       {
           count++;      // increment count, now there is 1 cd
     
           totalCost = totalCost + cost;      // the total cost of the cd collection is  (whatever it was before), plus the cost of this current CD
     
           collection.add(new CD (title, artist, cost, tracks));          // Add a new CD to the ArrayList, passing it those arguments
     
     
       }


    Now I am trying to access this arraylist. Will check back with more code in a few...

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 3 classes - Simple school assignment - ArrayLists

    confused by this whole aspect. Should I be creating new objects for each CD ,or is that not necessary?
    Since the method to add a CD in the CDCollection class takes all of the parameters needed to create the CD, you can assume that addCD():

    1. Creates the CD object
    2. Adds the resulting object to the collection

    Can the CD object be anonymous (without a reference that can be used later) in that process? Yes.

Similar Threads

  1. ArrayLists and Classes
    By melki0795 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 28th, 2014, 04:48 PM
  2. Help with school assignment/arrays
    By d0mlnance in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2013, 09:00 PM
  3. Help with Coding School Assignment
    By mattmattmatt in forum Loops & Control Statements
    Replies: 12
    Last Post: October 8th, 2013, 09:11 PM
  4. Problem with School Assignment.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 17th, 2012, 05:12 PM
  5. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM