| Author |
Message |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/11/2010 23:31:33
|
leapfrog7
Newbie
Joined: 02/11/2010 23:30:17
Messages: 13
Offline
|
I am working on my homework assignment. I have most of it done but I am stuck. The directions are:
Modify the Inventory Program Part 1 so the application can handle multiple items.
Use an array to store the items. The output should display the information one
product at a time, including the item number, the name of the product, the number of
units in stock, the price of each unit, and the value of the inventory of that product.
In addition, the output should display the value of the entire inventory.
Create a method to calculate the value of the entire inventory
Create another method to sort the array items by name
I cannot get the sorting part of it right, can you look at my code and give me some tips? I would beg but it doesn't transfer well into writing.
class Week5Ex {
public static void main (String arg[]){
ActionProduct ap1 = new ActionProduct("Green AP", 1, 5, 4.99);
ActionProduct ap2 = new ActionProduct("Red AP", 2, 10, 6.83);
Product p[] = {ap1, ap2};
public static void sortProductArrayByNameAscending(Product p[]){
boolean swap = true;
while(swap == true){
swap = false;
for(int number=0; number < (p.length - 1); ++number){
if(p[number].name.compareToIgnoreCase(p[number+1].name) > 0){
Product temp = new Product(p[number].name, p[number].number, p[number].amount, p[number].price);
p[number] = p[number+1];
p[number+1] = temp;
swap = true;
}
}
}
}
for(int number = 0; number < p.length; ++number){
System.out.printf("Item Name: %s, Item #: %d, Units in Stock: %d, Item Price: $%.2f\n",
p[number].name, p[number].number, p[number].amount, p[number].price);
System.out.printf("Inventory Value: $%.2f\n",
p[number].InventoryValue());
System.out.println();
}
System.out.println();{
System.out.printf("Total Inventory Value: $%.2f\n", ap1.InventoryValue() + ap2.InventoryValue());
}
}
private static void Product(Product p[]) {
}
}
ActionProduct Class:
class ActionProduct extends Product {
public ActionProduct(String a, int b, int c, double d){
name = new String(a);
number = b;
amount = c;
price = d;
}
public double InventoryValue(){
return (amount*price);
}
}
Product Class:
abstract class Product {
String name;
int number;
int amount;
double price;
public abstract double InventoryValue();
}
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 09:31:12
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
Is is possible to post a compilable example? What you're showing doesn't even compile.
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 10:09:10
|
leapfrog7
Newbie
Joined: 02/11/2010 23:30:17
Messages: 13
Offline
|
This will compile:
ActionProduct Class:
Product Class:
But I cannot setup the sorting method correctly and I do not know where I am going wrong. I continued to work on it, and this is what I have in stead of the first code example, but it does not compile.
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 10:13:40
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
First things first - you cannot instantiate an abstract class. You need to change
to
That lets you at least compile.
Now let me take a look at the sorting...
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 10:21:48
|
leapfrog7
Newbie
Joined: 02/11/2010 23:30:17
Messages: 13
Offline
|
Okay, thank you for your replies by the way, I was completely lost. I do get this error when I try to run the code in Eclipse:
Exception in thread "main" java.lang.ClassCastException: ActionProduct cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at InventoryProgram.main(InventoryProgram.java:10)
I am a little more confused now though because it seems I need something called a comparable for the ActionProduct, but my course has not covered this as of yet. Do I need to set up a comparable? If I do does it need to be setup in the ActionProduct class?
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 10:24:52
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
Yep, that is the next step. From the sort() docs, you can see that your Object needs to implement Comparable. That requires you to implement compareTo. It would be something like:
I think that this is what you want.
This message was edited 1 time. Last update was at 02/12/2010 10:26:24
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 10:38:58
|
leapfrog7
Newbie
Joined: 02/11/2010 23:30:17
Messages: 13
Offline
|
I really want to thank you for all your help, but I am still getting the same error when I try to run it:
Exception in thread "main" java.lang.ClassCastException: ActionProduct cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at InventoryProgram.main(InventoryProgram.java:10)
I do not understand what these errors mean or where to start looking for an answer. Do you have any ideas? Again, thank you so much for you help, it is very appreciated.
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 10:40:38
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
Did you use the ActionProduct code above? It must implement Comparable.
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 10:43:01
|
leapfrog7
Newbie
Joined: 02/11/2010 23:30:17
Messages: 13
Offline
|
I did use the code above, and there were no problems compiling it, but I still get that error.
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 11:07:10
|
stdunbar
Newbie
![[Avatar]](/images/avatar/a87ff679a2f3e71d9181a67b7542122c.png)
Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline
|
Something isn't right then. ActionProduct needs to implement Comparable and have a compareTo method. So that we're on the same page, this is the code I'm using:
InventoryProgram.java
Product.java
ActionProduct.java
When I run:
Can you verify your code?
|
Thanks for using the forums at hotjoe.com |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 02/12/2010 11:15:32
|
leapfrog7
Newbie
Joined: 02/11/2010 23:30:17
Messages: 13
Offline
|
You are right, it worked great. My problem was when I transferred your example I missed the first line:
I thought the rest of the code implemented the comparable and didn't realize it was coded in the first line. Thank you so much for your help with this, I really appreciate it.
|
|
|
 |
|
|
|
|