CD class
//********************************************************************
// CD.java Author: Lewis/Loftus/Cocking
//
// 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 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;
}
}
here is the cd collection class
//********************************************************************
// CDCollection.java Author: Lewis/Loftus/Cocking
//
// Represents a collection of compact discs.
//********************************************************************
import java.text.NumberFormat;
public class CDCollection
{
private CD[] collection;
private int count;
private double totalCost;
//-----------------------------------------------------------------
// 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;
}
//-----------------------------------------------------------------
// Doubles the size 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;
}
}
here is the tune class given if it helps you guys in any way, not helping me.
//********************************************************************
// Tunes.java Author: Lewis/Loftus/Cocking
//
// Driver for demonstrating 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 ("By the Way", "Red Hot Chili Peppers", 14.95, 10);
music.addCD ("Come On Over", "Shania Twain", 14.95, 16);
music.addCD ("Soundtrack", "The Producers", 17.95, 33);
music.addCD ("Play", "Jennifer Lopez", 13.90, 11);
System.out.println (music);
music.addCD ("Double Live", "Garth Brooks", 19.99, 26);
music.addCD ("Greatest Hits", "Stone Temple Pilots", 15.95, 13);
System.out.println (music);
}
}
here is the class im working on
// BetterTunes.Java- improved version of tunes program
// U.Ogbe
// 2/6/2012
import java.util.Scanner;
public class BetterTunes2
{
public static void main (String [] args)
{
String title, artist;
double cost;
int tracks;
Scanner scan = new Scanner (System.in);
Scanner scan2 = new Scanner (System.in);
Scanner scan3 = new Scanner (System.in);
System.out.print("what is the title of your cd: ");
title = scan.nextLine();
System.out.print("who is the artist of your cd: ");
artist = scan.nextLine();
System.out.print("what is the cost of your cd: ");
cost = scan2.nextDouble();
System.out.print("how many tracks are on your cd: ");
tracks = scan3.nextInt();
}
}
i need to make my tunes class able to
1. user adds cd's
2. CD class comparable " implement comparable"
3. add search by artist to CDCollection
4. sort
5. binary search
Help on how to do any of these appreciated !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

New Topic/Question
Reply


MultiQuote


|