|
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;
}
}
|
|
|
Compiling your code gives:
C:\jexp>javac tunes.java
tunes.java:14: selectionSort(java.lang.Comparable[]) in Sorting cannot be applie
d to (CDCollection)
Sorting.selectionSort(music);
^
tunes.java:17: foreach not applicable to expression type
for(CDCollection data : music)
^
tunes.java:144: boolean cannot be dereferenced
return(title.equals((CD)other).getTitle());
^
Note: tunes.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
|
|
1 — selectionSort method use
This method is looking for an array of type Comparable; so send the CD array collection of CDCollection. #2 (next) has more...
2 — for each args
The "music" variable *is* a CDCollection. What you want is the array of CD objects kept by CDCollection; the array is referenced (pointed to) by the collection instance variable. Add a get accessor method to the CDCollection class to return the array collection and then:
for(CD cd : music.getCDs())
System.out.println(cd);
|
|
3 — typo: misplaced "("
return (title.equals((CD)other).getTitle());
|
|
should be
return title.equals(((CD)other).getTitle());
|
|
To show another option I changed the CDCollection addCD method to use System.arraycopy so we can increase the collection array size by one at a time. You are using a Vector class style of size management for the array which results in a lot of null values for the excess capacity array elements. This is okay but you'll need to be careful to avoid these null elements in your sorting and printing methods.
Also made some changes to the first sorting method.
The code posted below runs okay.
import java.text.NumberFormat;
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);
//i get an error here saying Sorting cannot be applied to CDCollection
Sorting.selectionSort(music.getCDs());
//i also get an error here saying for each not applicable for expression type
for(CD cd : music.getCDs())
System.out.println(cd);
}
}
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;
}
if(min != index)
{
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;
}
}
}
class CDCollection
{
private CD[] collection;
private int count;
private double totalCost;
/** This is my CDCollection class*/
public CDCollection()
{
collection = new CD[0];
count = 0;
totalCost = 0.0;
}
public void addCD(String title, String artist, double cost, int tracks)
{
CD[] temp = new CD[collection.length+1];
System.arraycopy(collection, 0, temp, 0, collection.length);
temp[collection.length] = new CD(title, artist, cost, tracks);
collection = temp;
totalCost += cost;
count++;
}
public CD[] getCDs() { return 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] + "\n";
return report;
}
}
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)
{
//i get an error here saying boolean cannot be dereferenced.?
return title.equals(((CD)other).getTitle());
// ...or
// what if other is the same title by a different artist
//return this == other;
}
public int compareTo(Object other)
{
String otherTitle = ((CD)other).getTitle();
return title.compareTo(otherTitle);
}
public String getTitle()
{
return title;
}
}
|
|
|
|
|
|
|
|
|
|