By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

Can't get my inventory program to compile

P: 59
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the assignments...

4. CheckPoint: Inventory Program Part 1
Resource: Java: How to Program
Due Date: Day 5 [Individual] forum
Choose a product that lends itself to an inventory (for example, products at your
workplace, office supplies, music CDs, DVD movies, or software).
Create a product class that holds the item number, the name of the product, the number
of units in stock, and the price of each unit.
Create a Java application that displays the product number, the name of the product, the
number of units in stock, the price of each unit, and the value of the inventory (the
number of units in stock multiplied by the price of each unit). Pay attention to the good
programming practices in the text to ensure your source code is readable and well
documented.


1. CheckPoint: Inventory Program Part 2
Resource: Java: How to Program
Due Date: Day 4 [Individual] forum
Modify the Inventory Program 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 the name of the product.


2. CheckPoint: Inventory Program Part 3
Resource: Java: How to Program
Due Date: Day 7 [Individual] forum
Modify the Inventory Program by creating a subclass of the product class that uses one
additional unique feature of the product you chose (for the DVDs subclass, you could use
movie title, for example). In the subclass, create a method to calculate the value of the
inventory of a product with the same name as the method previously created for the
product class. The subclass method should also add a 5% restocking fee to the value of
the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking
fee.


4. CheckPoint: Inventory Program Part 4
Resource: Java: How to Program
Due Date: Day 5 [Individual] forum
Modify the Inventory Program to use a GUI. The GUI 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 GUI should display the value of the entire inventory, the additional attribute,
and the restocking fee.


2. CheckPoint: Inventory Program Part 5
Resource: Java: How to Program
Due Date: Day 7 [Individual] forum
Modify the Inventory Program by adding a button to the GUI that allows the user to move
to the first item, the previous item, the next item, and the last item in the inventory. If the
first item is displayed and the user clicks on the Previous button, the last item should
display. If the last item is displayed and the user clicks on the Next button, the first item
should display.
Add a company logo to the GUI using Java graphics classes.

Here is the last program that I was able to get to compile... I am supposed to remove the user input section, but no matter what I try, it doesn't work....

[Program removed as per site rules - see FAQ]

Pleeeease java geniuses... HELP!!! :)
I can be reached at <email removed>
Nov 29 '06 #1
Share this Question
Share on Google+
109 Replies


10K+
P: 13,264
Let's go one stage at a time then

[Program removed as per site rules - see FAQ]
Nov 29 '06 #2

Expert 100+
P: 1,510
your print statements in main() are a mixture of C++ and Java, i.e. to print to the screen in Java you use System.out.println() not System.out.printf()
main() should look like
Expand|Select|Wrap|Line Numbers
  1. public class Inventory
  2. {
  3.    // main methods begins execution of java application
  4.    public static void main( String args[])
  5.    {
  6.          Supplies mySupplies = new Supplies();
  7.  
  8.          System.out.println("\n\nItem Name: "+mySupplies.getItemName()); //display item name
  9.                  System.out.println("Item Number: "+mySupplies.getItemNumber()); //display item number
  10.                  System.out.println("Quantity in Stock: "+mySupplies.getStockQuantity()); //display quantity in stock
  11.                  System.out.println("Item Price:$ "+mySupplies.getItemPrice()); //display item price
  12.                  System.out.println("Value of Inventory:$ "+mySupplies.calculateInventoryValue()); //display total value of inventory for this item
  13.  
  14.          System.out.println("\n\nEnter item name or enter the word stop to quit:");//prompt
  15.               //   mySupplies.setItemName(input.next()); // read item name or stop
  16.  
  17.  
  18.    } // end main method
  19. }//end class Inventory
  20.  
when run it gives
Item Name:
Item Number: 0.0
Quantity in Stock: 0.0
Item Price:$ 0.0
Value of Inventory:$ 0.0


Enter item name or enter the word stop to quit:

you now need to call setItemName(), setItemNumber(), etc to set up data in mySupplies
Nov 29 '06 #3

P: 59
yes please and thank you... you are a life saver!!
Nov 29 '06 #4

P: 59
Let's go one stage at a time then

[Program removed as per site rules - see FAQ]
This one worked beautifully... I guess I was sort of close... Thank you so much... what is the next step...?
Nov 29 '06 #5

10K+
P: 13,264
your print statements in main() are a mixture of C++ and Java, i.e. to print to the screen in Java you use System.out.println() not System.out.printf()

[code removed as per site rules - see FAQ]

you now need to call setItemName(), setItemNumber(), etc to set up data in mySupplies
printf is now available in java 1.5
Nov 29 '06 #6

10K+
P: 13,264
This one worked beautifully... I guess I was sort of close... Thank you so much... what is the next step...?
Being close is the reason why it was easy to help you. Now have a look at this and make additions so that we can sort by name and test that functionality

[Code removed as per site rules - see FAQ]
Nov 29 '06 #7

P: 59
Where can I add the %.2f to get the currency to diplay two decimals?
Nov 29 '06 #8

10K+
P: 13,264
Where can I add the %.2f to get the currency to diplay two decimals?
Change this line
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Total Value is"+s.getTotalValue());
  2.  
to make it use printf instead of println.
Nov 29 '06 #9

10K+
P: 13,264
Change this line
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Total Value is"+s.getTotalValue());
  2.  
to make it use printf instead of println.
Remember Product is just a name I chose, you should change it to something that can be a superclass of some other object as per requirement in Part3
Nov 29 '06 #10

P: 59
I did this

Expand|Select|Wrap|Line Numbers
  1. System.out.printf("Total Value is: $%.2f\n"+s.getTotalValue());
and got this

$Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Inventory2.main(Inventory2.java:153)
Nov 29 '06 #11

P: 59
Product works doesn't it? it is a list of products that are in stock...

Could I use something like a date recieved, or maybe who the product was ordered by for the subclass?
Nov 29 '06 #12

10K+
P: 13,264
Product works doesn't it? it is a list of products that are in stock...

Could I use something like a date recieved, or maybe who the product was ordered by for the subclass?
And what would you be calling that Subclass?
Usually beginners are encouraged to write their own sort functions and all that. Here is how you properly use the Arrays.sort method. Read and understand the need for the comparable interface (if you have not yet done interfaces then that's the price you pay for not writting your own sort function. I have to go in the next fifteen minutes so here is where we've got so far

[code removed as per site rules - see FAQ]
Nov 29 '06 #13

P: 59
No... I am sorry... I do want to write my own sort function... I thought that was how you did it... Should I take out the import of Array then?

Will you be back on when you could help me more? You have already taught me more than my teacher has during the whole class...
Nov 29 '06 #14

10K+
P: 13,264
No... I am sorry... I do want to write my own sort function... I thought that was how you did it... Should I take out the import of Array then?

Will you be back on when you could help me more? You have already taught me more than my teacher has during the whole class...
It's a bit arguable you know. Deepends on what your teacher wants too. You would learn more java by using Arrays.sort, IMO. It is how you would want to do it in practice. On the other hand, writing your own bubblesort (or even quicksort function) might be less efficient but will improve your knowledge of loops and arrays. I suggest you ask your teacher and try to understand the Array.sort route for now. At the end, when you get time, do it both ways and compare. It's the best way to learn. Yeah I'll be in about tomorrow (about 15 hrs from now) but I'm sure there are some people around who will be able to help you.
Nov 29 '06 #15

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1.  
  2. public static void main( String args[])
  3.    {
  4.  
  5.      Product[] supplies = new Product[3];
  6.  
  7. >
  8. >
  9. >
  10.   }
  11.  
  12.  
[Code removed as per site rules - see FAQ]

Now because you've been asking the same question in many threads, it has been difficult to track the progress of your exercises. I should probably delete your other threads for this same qustion.

Now you said (somewhere else) that your teacher wants the array to be created in the main method. Will the above one be what you want?
Dec 1 '06 #16

P: 59
[removed program code as per site rules - see FAQ]

I do think it might be closer, but when I try to run it I get a no class def found error... What did I do wrong?
Dec 1 '06 #17

10K+
P: 13,264
Did you save it as Inventory2.java and compile it before running it?
Dec 1 '06 #18

P: 59
[removed program code as per site rules - see FAQ]

Ok, I thought I did, but I redid it and now it works...
This is good for that assognment I think. Next, I need to:

Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking fee
Dec 1 '06 #19

10K+
P: 13,264
You said you will stick with product so what subclass do you want to create now for something with an additional feature to product?
Dec 1 '06 #20

P: 59
Hmmm...

I dunno, how about Department?
Dec 1 '06 #21

10K+
P: 13,264
Hmmm...

I dunno, how about Department?
Department is surely not part of inventory. They want something like
Book(title), DVD(title), MobilePhone(brand), etc. The item in () is the additional feature of the product
Dec 1 '06 #22

P: 59
Ok, how bout brand then... That sounds good to me...
Dec 1 '06 #23

10K+
P: 13,264
Ok, how bout brand then... That sounds good to me...
You mean MobilePhone?
Dec 1 '06 #24

P: 59
no..

productBrand
Dec 1 '06 #25

10K+
P: 13,264
no..

productBrand
What's required is a Product first something that we can model as is a Product then we add something else to it. Alright forget it. Here is an example using mobile phone



[code removed as per site rules - see FAQ]
Dec 1 '06 #26

P: 59
So would I do that for each item... (Stapler, Pen, etc...)
How does that fit it with the array stuff from the other one? Or am I taking that out?
Dec 1 '06 #27

P: 59
I guess now I don't understand how that part fits into the other one... Did you take out the Product class and instead use mobilephone? Shouldn't the item be one of the ones from the array?
Dec 1 '06 #28

10K+
P: 13,264
I guess now I don't understand how that part fits into the other one... Did you take out the Product class and instead use mobilephone? Shouldn't the item be one of the ones from the array?
Sorry I'd got disconnected for some time.

I think you need to revisit your inheritance to fully understand what is going on. Here is what you should have

[code removed as per site rules - see FAQ]
Dec 1 '06 #29

P: 59
I get how the subclass inherits from the class,

Now since you used MobilePhone which is another item,

I am confused because I thought the subclass was supposed to be an additional charachteristic of each item, so like underneath the book, pen, etc there would be a brand name for each one... Is that incorrect?

To me it seems like mobile phone would be another addition to the array as a product...
Dec 1 '06 #30

P: 59
When I run this one, it does appear to add the mobilephone as an additional item, not an additional charachteristic for each item...
Dec 1 '06 #31

10K+
P: 13,264
When I run this one, it does appear to add the mobilephone as an additional item, not an additional charachteristic for each item...

"
Modify the Inventory Program by creating a subclass of the product class that uses one
additional unique feature of the product you chose"
The additional feature should not be added to all the items but is unique for that product only.

The subclass-superclass relationship should say a MobilePhone is a Product.

You are asked to come up with something that is a product and write a class for it reusing the other elements you already added for product. The MobilePhone is added to the Products array because it is a Product.
Dec 1 '06 #32

P: 59
This is what my teacher said...

"You want to add a subclass that extends from your product class. In it, you will add a new attribute which could be brand as you suggested. You will leave in the array and sort."

To me that sounds like she wants the subclass to add a new sttribute; brand, to each item already in the array...

I will double check..
Dec 1 '06 #33

10K+
P: 13,264
This is what my teacher said...

"You want to add a subclass that extends from your product class. In it, you will add a new attribute which could be brand as you suggested. You will leave in the array and sort."

To me that sounds like she wants the subclass to add a new sttribute; brand, to each item already in the array...

I will double check..
If you looked at the code I posted, I added brand to MobilePhone. The attribute is to be added to the subclass only as your teacher pointed out. The array and the sort is still there.
Dec 1 '06 #34

P: 59
I checked again-I thinkg she is crazy...

Anyway, she wants the out put to be the items that were already in the array, and she wants the new subclass to add a new attribute to each item...

Like


Name Pen
Number Price
Quantity
Value
Brand

Name Book
Number Price
Quantity
Value
Brand

Name Chair
Number Price
Quantity
Value
Brand

I played with it andf this is what I have, I know it is not right...

[code removed as per site rules - see FAQ]
Dec 1 '06 #35

10K+
P: 13,264
[code removed as per site rules - see FAQ]

I think she meant it to be this way rather. Each Mobile is a product that has a brand. Remember brand is unique only for objects of the type of the subclass ie MobilePhone. As far as I can see, your code for Brand does not make anysense.
Dec 1 '06 #36

P: 59
I asked her-she wants the subclass to add a new attribute to the items already in the array. Is there a way to do that?
Dec 1 '06 #37

P: 59
I am looking through my reading, and your version does make more sense-you can see why I am having trouble with this teacher...

Sorry about the confusion-I really do appreaciate your help with this...
Dec 1 '06 #38

P: 59
This seems to be working as a subclass... Now where should I add the restocking fee?
Dec 1 '06 #39

P: 59
This is getting there, but you do not want to get rid of the name of the object. That still stays there and is the field to sort on. The MobilePhone class should not have all the old attributes, they belong to the Product class and should be used from there.

This was her response to the most recent version. What she is saying does not make sense... So I need to keep the old array, AND add the new subclass that contains only the brand attribute? I think I am going to fail this class... :(
Dec 2 '06 #40

P: 59
The brand would be added to all the items. You would now have a new class and it would extend from the Product class. All the items have the old attributes and the new ones as well.

She also said this...
Dec 4 '06 #41

P: 6
The brand would be added to all the items. You would now have a new class and it would extend from the Product class. All the items have the old attributes and the new ones as well.

She also said this...

I think we are in the same class, or taking the same course. I am having trouble with the part 5. I know that the ones I have turned in so far were not exactly what was requested.
Dec 5 '06 #42

10K+
P: 13,264
This is getting there, but you do not want to get rid of the name of the object. That still stays there and is the field to sort on. The MobilePhone class should not have all the old attributes, they belong to the Product class and should be used from there.

This was her response to the most recent version. What she is saying does not make sense... So I need to keep the old array, AND add the new subclass that contains only the brand attribute? I think I am going to fail this class... :(
Unfortunately. you teacher missed that the attributes in Product (name etc) are all defined there as private and so are not available outside the product class. This means for the subclass MobilePhone you have to redefine them. Note that you do not redefine the get and set methods becuse all of them are defined as public in the Product class an so are available in the subclass MobilePhone.

If we make these attributes protected instead of private the become visible in the subclass and we no longer need to redefine them as in

[code removed as per site rules - see FAQ]
Dec 5 '06 #43

P: 59
I see what you are doing here, but she still seems to want the old items in there (book, pen, etc) and for the new subclass to add the brand attribute to the old items...
Dec 5 '06 #44

P: 6
How can we take this code and turn it into what is requested in Part 5? I have a similar Inventory program I am working on and have gotten this far but have gotten stuck on parts 4 and 5. Using the GUI is not working out.

2. CheckPoint: Inventory Program Part 5
Resource: Java: How to Program
Due Date: Day 7 [Individual] forum
Modify the Inventory Program by adding a button to the GUI that allows the user to move
to the first item, the previous item, the next item, and the last item in the inventory. If the
first item is displayed and the user clicks on the Previous button, the last item should
display. If the last item is displayed and the user clicks on the Next button, the first item
should display.
Add a company logo to the GUI using Java graphics classes.
Dec 5 '06 #45

P: 59
I am still trying to get 4 right before we move on to 5. Why don't you post your #4 if you are done with that one so we can see what you have done...?
Dec 5 '06 #46

P: 59
I can't get it to compile... Did you?
Dec 5 '06 #47

P: 6
It is actually 2 seperate files. The second one is at the bottom
Dec 5 '06 #48

10K+
P: 13,264
It is actually 2 seperate files. The second one is at the bottom
[code removed as per site rules - see FAQ]

This will make the code compile. May I say that you are using the wrong tools here. To display non-editable text eg labels, you should use a JLabel. Where did you say was the problem with this one?
Dec 6 '06 #49

P: 59
Now I got it to work... Thanks...

What do we need to do now, I guess this one...


Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.

Add a company logo to the GUI using Java graphics classes.
Dec 6 '06 #50

109 Replies

Post your reply

Sign in to post your reply or Sign up for a free account.