Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

im trying to sort my program with insertionSort and selectionSort

 
alaina peeler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic