Results 1 to 7 of 7
Thread: Trying to work out this program
- 07-18-2010, 12:54 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Trying to work out this program
I'm having a hard time understanding this program.
Basically the program should either sort the array of items by name or by price and display the full list that is now sorted.
I get the comparator and all that, but I must be missing something because two of the files will not compile. The objects aren't being recognized
These two will.
Java Code:import javax.swing.*; import java.util.*; import java.text.*; public class CoffeeDriver { public static void main(String[] args) { char choice; Item inventory[] = new Item[4]; inventory[0] = new Item("Coffee", 1.00); inventory[1] = new Item("Water", 2.00); inventory[2] = new Item("Milk", 1.50); inventory[3] = new Item("Bagel", 1.25); inventory[4] = new Item("Donut", 0.75); choice = JOptionPane.showInputDialog(null, "Welcome to Wings Coffee Shop." + "\nWould you like to see these items sorted by" + "\nname or by price? (n/p)", JOptionPane.QUESTION_MESSAGE).charAt(0); choice = Character.toLowerCase(choice); switch(choice) { case 'n': Arrays.sort(inventory); System.out.println(); System.out.println("Sorted by name"); for (int i=0; i<4; i++) { Item item = inventory[i]; String name = item.getName(); double price = item.getPrice(); System.out.println(name + " " + price); } break; case 'p': Arrays.sort(inventory); System.out.println(); System.out.println("Sorted by price"); for (int i=0; i<4; i++) { Item item = inventory[i]; String name = item.getName(); double price = item.getPrice(); System.out.println(name + " " + price); } break; default: JOptionPane.showMessageDialog(null, "Invalid entry, Please try again"); break; } } }
Java Code:import java.util.Comparator; public class Item implements Comparable<String> { String name; double price = 0.00; // Constructor public Item(String itemName, double itemPrice) { this.name = itemName; this.price = itemPrice; } // Set name method public void setName(String name) { this.name = name; } // Get name method public String getName() { return name; } // Get price method public void setPrice(double price) { this.price = price; } // Set price method public double getPrice() { return price; } //compare items by name: public int compareTo(String otherName) { return name.compareTo(otherName); } }
These two won't.
The errors:Java Code:package comparable.ex03; import java.util.Comparator; public class PriceComparator<Item> implements Comparator<Item> { public int compare(Item o1, Item o2) { int itemPrice1 = o1.getPrice(); int itemPrice2 = o2.getPrice(); if(itemPrice1 > itemPrice2) { return 1; } else if(itemPrice1 < itemPrice2) { return -1; } else return 0; } }
PriceComparator.java:8: cannot find symbol
symbol : method getPrice()
location: class java.lang.Object
int itemPrice1 = o1.getPrice();
^
PriceComparator.java:9: cannot find symbol
symbol : method getPrice()
location: class java.lang.Object
int itemPrice2 = o2.getPrice();
The errors:Java Code:package comparable.ex03; import java.util.Comparator; public class NameComparator<Item> implements Comparator<Item> { public int compare(Item o1, Item o2) { String itemName1 = o1.getName(); String itemName2 = o2.getName(); return itemName1.compareTo(itemName2); } }
NameComparator.java:8: cannot find symbol
symbol : method getName()
location: class java.lang.Object
String itemName1 = o1.getName();
^
NameComparator.java:9: cannot find symbol
symbol : method getName()
location: class java.lang.Object
String itemName2 = o2.getName();
Isn't there an easier way to sort and display a list like this?
How do I get this to work???
- 07-18-2010, 01:10 AM #2
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
also
When I run CoffeeDriver I see the initial screen to get the input from the user but then get this error:
Exception in thread "main" java.lang.ClassCastException: Item cannot be cast to java.lang.String
at Item.compareTo(Item.java:9)
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at CoffeeDriver.main(CoffeeDriver.java:49)
-
Issues:
I'm surprised you don't get an array index out of range when you try to add a 5th Item in the [4] slot of your Item[4] array. You should know that this array will hold four Item objects at index [0], [1], [2], and [3].
If Item implements a generic Comparable, it shouldn't be a Comparable<String> but rather it should extend Comparable<Item> since you're comparing one Item to another. Then in the compareTo method you will pass an Item object in the parameter and compare its String with the "this" String (if String comparison is what you want to do).
- 07-18-2010, 01:20 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Yeah I fixed that, changed it to a 4.
- 07-18-2010, 01:30 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
I'm not sure I know what you mean here when referring to change the compareTo method
how do I get to to compare it from the return statement?
Java Code:import java.util.Comparator; public class Item implements Comparable<Item> { String name; double price = 0.00; // Constructor public Item(String itemName, double itemPrice) { this.name = itemName; this.price = itemPrice; } // Set name method public void setName(String name) { this.name = name; } // Get name method public String getName() { return name; } // Get price method public void setPrice(double price) { this.price = price; } // Set price method public double getPrice() { return price; } //compare items by name: public int compareTo(Item otherName) ???? { return name.compareTo(otherName);???? } }
-
You want to compare the name held in this Item, with the name held in the other Item, so do just that:
Java Code:public int compareTo(Item other) { return name.compareTo(other.getName()); }
- 07-18-2010, 03:14 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
JScrollPane doesn't work?
By ecliptical in forum New To JavaReplies: 4Last Post: 01-25-2010, 12:35 AM -
Why doesn't this work?
By Corder10 in forum New To JavaReplies: 1Last Post: 07-04-2009, 10:33 PM -
my loop doesn't work.. pls help???
By ashton in forum New To JavaReplies: 5Last Post: 01-16-2009, 08:24 AM -
how would i get this to work...?
By deeadeed in forum New To JavaReplies: 6Last Post: 12-06-2007, 02:58 AM -
Program don't work
By baltimore in forum New To JavaReplies: 1Last Post: 08-04-2007, 09:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks