at CD.compareTo<CD.java:44>
at Sorts.insertionSort<Sorts.java:66>
at Tunes.main<Tunes.java:66>
I'm pretty sure I just need to change something small and it will work.
Thanks
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);
Sorts.insertionSort (music.getCDS());
System.out.println (music);
}
}
public class Sorts
{
//-----------------------------------------------------------------
// Sorts the specified array of integers using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (int[] numbers)
{
int min, temp;
for (int index = 0; index < numbers.length-1; index++)
{
min = index;
for (int scan = index+1; scan < numbers.length; scan++)
if (numbers[scan] < numbers[min])
min = scan;
// Swap the values
temp = numbers[min];
numbers[min] = numbers[index];
numbers[index] = temp;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of integers using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (int[] numbers)
{
for (int index = 1; index < numbers.length; index++)
{
int key = numbers[index];
int position = index;
// shift larger values to the right
while (position > 0 && numbers[position-1] > key)
{
numbers[position] = numbers[position-1];
position--;
}
numbers[position] = key;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] objects)
{
for (int index = 1; index < objects.length; index++)
{
Comparable key = objects[index];
int position = index;
// shift larger values to the right
while (position > 0 && objects[position-1].compareTo(key) > 0)
{
objects[position] = objects[position-1];
position--;
}
objects[position] = key;
}
}
}
import java.text.NumberFormat;
public class CD implements Comparable
{
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;
}
//------------------------------------------------------------------
public int compareTo (Object other)
{
int result;
if (title.equals(((CD)other).title))
result = artist.compareTo(((CD)other).artist);
else
result = title.compareTo(((CD)other).title);
return result;
}
}
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[1];
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;
}
//------------------------------------------------------------------
public CD[] getCDS()
{
return collection;
}
}
Forgot: The code is supposed to alphabetize the cd's by title.
Thanks

New Topic/Question
Reply


MultiQuote





|