Question: //******************************************************************** // CD.java Author: Lewis/Loftus // // Represents a compact disc. //******************************************************************** import java.text.NumberFormat; public class CD { private String title, artist; private double cost;

//******************************************************************** // 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() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String description; description = fmt.format(cost) + "\t" + tracks + "\t"; description += title + "\t" + artist; return description; } }
//******************************************************************** // 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() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "; report += "My CD Collection "; report += "Number of CDs: " + count + " "; report += "Total cost: " + fmt.format(totalCost) + " "; report += "Average cost: " + fmt.format(totalCost/count); report += " CD List: "; for (int cd = 0; cd < count; cd++) report += collection[cd].toString() + " "; 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; } }
//******************************************************************** // 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); 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); } }

After having these three programs above I need to add some more to these programs.

1)Add two more cds of your choice to the collection. Print out again.

----------------------------------------------------------------------------------------------------------------------------------

2)Create a int variable index in Tunes.java. This value will be used to get one title from the collection.

Add this code to CD.java (a getter):

3) public String getTitle()

{

return title;

}

Add this code to CDCollection.java:

4) public String findTitle (int i)

{

return (collection[i].getTitle());

}

Add this code to Tunes.java:

5) Using a loop to allow user to enter 0 thru number of CDs Added to select and list/display a Title of CD in the list. (example below is using the number entered called index)

6)If value entered is valid then do this line of code System.out.println(music.findTitle(index));

7)Index is used as the value entered, if value entered not valid try again. Allow loop to end!

8) If the value = 1, then Come on Over will displayed.

9)Add the code to add up the costs in the collection and display the value.

10)Place this code in a for loop in CDCollection.java and add the necessary code to the other two java files shown below. This will be similar to what you did in bullet 2 above. Also look at the for loop in toString()

total = total + (collection[cd].getCost());

In Tunes.java will ask for the total and print the total at end of program Total cost for collection:

In CD.java will give the the cost hence [hint:] getCost()

--------------------------------------------------------------------------------------------------------------------------------

11) Write a method to find most expensive CD.

12)Write a method to find CD with most tracks

Print out results.

here is the output

My CD Collection

Number of CDs: 4

Total Cost: $61.75

Average Cost: $15.44

CD Listing:

$14.95 10 Storm Front Billy Joel

$14.95 16 Come on Over Shania Twain

$17.95 33 SoundTrack Les Miserables

$13.90 11 GraceLand Paul Simon

-------------------------------------

My CD Collection

Number of CDs: 6

Total Cost: $97.69

Average Cost: $16.28

CD Listing:

$14.95 10 Storm Front Billy Joel

$14.95 16 Come on Over Shania Twain

$17.95 33 SoundTrack Les Miserables

$13.90 11 GraceLand Paul Simon

$19.99 26 Double Live Garth Brooks

$15.95 13 Greatest Hits Jimmy Buffet

-------------------------------------

My CD Collection

Number of CDs: 8

Total Cost: $134.59

Average Cost: $16.82

CD Listing:

$14.95 10 Storm Front Billy Joel

$14.95 16 Come on Over Shania Twain

$17.95 33 SoundTrack Les Miserables

$13.90 11 GraceLand Paul Simon

$19.99 26 Double Live Garth Brooks

$15.95 13 Greatest Hits Jimmy Buffet

$16.95 10 Divide Ed Sheeran

$19.95 12 Reputation Taylor Swift

enter number (-1 to quit) :

9

not valid, try again

enter number (-1 to quit) :

5

Greatest Hits

enter number (-1 to quit) :

3

GraceLand

enter number (-1 to quit) :

2

SoundTrack

enter number (-1 to quit) :

2

SoundTrack

enter number (-1 to quit) :

-1

Total cost for collection is: 134.59

Most expensive cd: Double

CD with most tracks: SoundTrack

The end folks!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!