wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes im trying to sort my program with insertionSort and selectionSort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "im trying to sort my program with insertionSort and selectionSort" Watch "im trying to sort my program with insertionSort and selectionSort" New topic
Author

im trying to sort my program with insertionSort and selectionSort

alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
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;
}
}
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

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



These two errors come from the fact that CDCollection is not in and of itself an array of CD objects. It definitly contains an array of CD objects but it is not an array of CD objects.





This should also give an error. Your method signiture says that you will return an int, but you do not actually return the result.
[ February 27, 2006: Message edited by: Garrett Rowe ]


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296



The problem here is your parentheses. You should break this one line up into at least two lines, it will help you and your instructor understand what is going on. Barring that you will have to add another set of opening and closing parenthesis.
[ February 27, 2006: Message edited by: Garrett Rowe ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: im trying to sort my program with insertionSort and selectionSort
 
Similar Threads
Cannot find symbol error in switch-case
Interface and array
does this sort an array of integers?
help with sort code
cannot resolve a symbol with a method.