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; } }