alaina02
February 27th, 2006, 10:47 AM
i have three classes here. One represents a CD, one represents a CDCollection that is an array, and one creates my CDCollection object and adds some CD's to it. Im trying to sort my CD's by title. I also have a Sorting class for this, but im stuck on my CD class that is supposed to implement Comparable. Here is my code....
public class Sorting
{
/** This is my sorting class*/
public static void selectionSort(Comparable[] list)
{
int min;
Comparable temp;
for(int index=0; index < list.length-1; index++)
{
min = index;
for(int scan = index+1; scan <list.length; scan++)
if(list[scan].compareTo(list[min]) < 0)
min = scan;
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
public static void insertionSort(Comparable[] list)
{
for(int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;
while(position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}}}
public class Tunes {
/**this is my main program*/
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", 1.90, 11);
Sorting.selectionSort(music); //i get an error here saying Sorting cannot be applied to CDCollection
for(CDCollection data : music) //i also get an error here saying for each not applicable for expression type
System.out.println(music);
}
}
import java.text.NumberFormat;
public class CDCollection
{
private CD[] collection;
private int count;
private double totalCost;
/** This is my CDCollection class*/
public CDCollection()
{ collection = new CD[100];
count = 0;
totalCost = 0.0;
}
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++;
}
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] + "\n";
return report;
}
private void increaseSize()
{
CD[] temp = new CD[collection.length * 2];
for(int cd=0; cd<collection.length; cd++)
temp[cd] = collection[cd];
collection = temp;
}
}
import java.text.NumberFormat;
public class CD implements Comparable
{
private String title, artist;
private double cost;
private int tracks;
/** Creates a new instance of CD */
public CD(String name, String singer, double price, int numTracks)
{
title = name;
artist = singer;
cost = price;
tracks = numTracks;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = fmt.format(cost) + "\t" + tracks + "\t";
description += title + "\t" + artist;
return description;
}
public boolean equals(Object other)
{
return(title.equals((CD)other).getTitle()); //i get an error here saying boolean cannot be dereferenced.
}
public int compareTo(Object other)
{
int result;
String otherTitle = ((CD)other).getTitle();
result = title.compareTo(otherTitle);
}
public String getTitle()
{
return title;
}
}
public class Sorting
{
/** This is my sorting class*/
public static void selectionSort(Comparable[] list)
{
int min;
Comparable temp;
for(int index=0; index < list.length-1; index++)
{
min = index;
for(int scan = index+1; scan <list.length; scan++)
if(list[scan].compareTo(list[min]) < 0)
min = scan;
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
public static void insertionSort(Comparable[] list)
{
for(int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;
while(position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}}}
public class Tunes {
/**this is my main program*/
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", 1.90, 11);
Sorting.selectionSort(music); //i get an error here saying Sorting cannot be applied to CDCollection
for(CDCollection data : music) //i also get an error here saying for each not applicable for expression type
System.out.println(music);
}
}
import java.text.NumberFormat;
public class CDCollection
{
private CD[] collection;
private int count;
private double totalCost;
/** This is my CDCollection class*/
public CDCollection()
{ collection = new CD[100];
count = 0;
totalCost = 0.0;
}
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++;
}
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] + "\n";
return report;
}
private void increaseSize()
{
CD[] temp = new CD[collection.length * 2];
for(int cd=0; cd<collection.length; cd++)
temp[cd] = collection[cd];
collection = temp;
}
}
import java.text.NumberFormat;
public class CD implements Comparable
{
private String title, artist;
private double cost;
private int tracks;
/** Creates a new instance of CD */
public CD(String name, String singer, double price, int numTracks)
{
title = name;
artist = singer;
cost = price;
tracks = numTracks;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = fmt.format(cost) + "\t" + tracks + "\t";
description += title + "\t" + artist;
return description;
}
public boolean equals(Object other)
{
return(title.equals((CD)other).getTitle()); //i get an error here saying boolean cannot be dereferenced.
}
public int compareTo(Object other)
{
int result;
String otherTitle = ((CD)other).getTitle();
result = title.compareTo(otherTitle);
}
public String getTitle()
{
return title;
}
}