Click to See Complete Forum and Search --> : im trying to Sort my program with insertionSort and selectionSort


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

dlorde
February 27th, 2006, 02:07 PM
You post a big block of unformatted code with no comments at all, and you don't even tell us what the problem is...

No, wait... I see, there's a comment buried right near the end... you've got your parentheses in a twist - there's too much going on in one line, break the line up into separate statements.

Before we can help you, you have to help us - by explaining, as clearly as possible, what the problem is...

p.s. compareTo(..) is supposed to return a result.

alaina02
February 27th, 2006, 04:36 PM
i left comments next to the lines of code that i was receiving errors from......all im trying to do is sort the CDCollection by title.

dlorde
February 27th, 2006, 05:56 PM
i left comments next to the lines of code that i was receiving errors from......OK, I now see the other comments :rolleyes:
Sorting.selectionSort(music); //i get an error here saying Sorting cannot be applied to CDCollection.Sorting.selectionSort(...) is declared as taking a Comparable[] list (an array of Comparable, confusingly named 'list'). You are trying to pass it a CDCollection, which is not an array of Comparable. It complains because it's expecting an array of Comparable, not a CDCollection.
for(CDCollection data : music) //i also get an error here saying for each not applicable for expression typeThis is a similar problem. the short form 'for' statement expects a subclass of Iterable, such as a Collection. CDCollection has a name that sounds like a Collection subclass, but it doesn't subclass or implement anything. The 'for' statement complains because it's expecting an Iterable or Collection subtype, not a CDCollection.

The other error I covered previously.

I could probably suggest some fixes if the code was formatted so I could see what was going on. <shrug> it's up to you...

Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman