• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need some help. Past due assignment  RSS feed

 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I worked on this for about 13 hours last night and couldn't get it what I need it to do. The assignment reads as follows and I've seen many posts but I can't seem to find something to make this work properly:
Modify the Inventory Program by creating a subclass of the selected product's class that uses one additional unique feature of the product that has been selected for you. You must use the subclass name and additional unique feature based on the information provided below. 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.

Selected Product's Subclass:
The subclass of your product will be Smart Phones. This means that your subclass must be called "SmartPhone", and it will extend your "Mobile Phone" class. The additional feature will be "Mobile OS". This data member should be of type "String".

Click the Assignment Files tab to submit your assignment.

I feel like I have things set up right, but I can't get it to return the items with the restocking fee, and I also can't display the OS when putting it in the Array. At this point I am lost.


Also, it does compile correctly, but it wont return the override of the getTotalValue, it only returns the value of units * price, and I can't figure out how to put the operating systems of each phone in.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Why does your SmartPhone class declare variables such as Prodname and Itemnum which hide the variables of the same name in the super class? Those variables and their gettter and setter methods are already provided for you, you don't need to duplicate them. I suggest you get rid of all the duplicates and then look at your code again, it will be easier to figure out the issues without all the distractions of excess code and without having values stored at 2 different levels.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Welcome to the Ranch.

Why does your SmartPhone class declare variables such as Prodname and Itemnum which hide the variables of the same name in the super class? Those variables and their gettter and setter methods are already provided for you, you don't need to duplicate them. I suggest you get rid of all the duplicates and then look at your code again, it will be easier to figure out the issues without all the distractions of excess code and without having values stored at 2 different levels.


Will give it a shot. I'm not honestly not sure why I had declared those variables in the subclass as well as the main. Will give that a shot, run and review. will post the results.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Updated Code:


Nothing changed. Same output after the removal of the variables. No errors.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got several issues going on here. Let's start with the low hanging fruit and see if you can make some progress.

First a word about naming conventions. Classes should always start with a capital letter, variables and method names should always be lowercase. Anything else just confuses people who are trying to read your code.


It looks like you were trying to create a method called "getMobileOS" here, but that's not what you have at all. That almost-method signature needs a pair of parenthesis and that semi-colon turns your method into a variable declaration with a value of nothing. And a getter should return the value of the private variable like this:



Your setters should look like this:

Now go through your code and fix your naming, your getters and setters, and the indentation so everything is more readable. Test your code again, and repost it and tell us what issues remain.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didn't say it would fix your code, I said it would be easier for you to work out what was wrong with it.

BTW you still have some superfluous methods such as:


Which is not only superfluous it's is actually detremental as it isn't doing what you intended it to do. You no doubt wanted to set the instance UnitStock variable to the value in the local UnitStock variable but you have set the local variable to it's own value. Where you hide the instance variable with a local variable you must use this. to access the instance variable. A correct implementation of this method is provided for you - you don;t need to provide your own.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Your setters should look like this:

Now go through your code and fix your naming, your getters and setters, and the indentation so everything is more readable. Test your code again, and repost it and tell us what issues remain.

I agree with your comments on naming etc but the OP is stuck with the variable and method naming supplied by his tutor and can't rename everything.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Okay removed superfluous methods not needed. It compiles fine without the excess. Should I go through and alter my variable and methods to lowercase. If it's not an integral part of the functionality I'd like to save the wasted time of modifying just to make them lowercase for no additional functionality. However, I will definitely keep that in mind when the 4th part of this assignment comes around this week.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rande Fisher wrote:Should I go through and alter my variable and methods to lowercase.

If your tutor supplied the MobilePhone class then you are stuck with the method and variables names he/she has supplied not matter how wrong they are ie do not change them. If it is all your work then definitely change them, just having a working program will not (or certainly should not) get you a score of 100%, some of the marks should be for style, readability, maintainability etc and the code as is fails on all those points.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which they most definitely are. I didn't have a problem with the assignments until it came to subclasses. Otherwise my mechanics were all 50/50 grade. This one I'm seeing a few point deductions along with the late penalty, but at this point as long as I can get this thing to provide the restocking fee and the Operating System of each phone, I'd rather just get it in than have another late penalty on it.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which they most definitely are
most definitely are what? Your tutors or yours?

but at this point as long as I can get this thing to provide the restocking fee and the Operating System of each phone, I'd rather just get it in than have another late penalty on it.

So does it work now? If not show us the output from the program.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Which they most definitely are
most definitely are what? Your tutors or yours?


but at this point as long as I can get this thing to provide the restocking fee and the Operating System of each phone, I'd rather just get it in than have another late penalty on it.

So does it work now? If not show us the output from the program.



Ok, the variables and methods are my own. He designated the Class names, not the methods or variables for your first questionTo answer the second. With the modifications done, it does as it did when I first posted the code. It prints each phones alphabetically, Has no mobile OS because its not designated yet, but it also doesn't recalculate the value of each phone to include the Restocking fee. So it compiles and runs, but it runs as if it was last weeks assignment before everything was added. No errors though.

How can I copy the output of the program. I'm quite new as I'm sure you are aware
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output is going to be the same because you aren't creating any SmartPhone objects and your output text doesn't output the OS.

BTW Why does your class have a getPrice() method which doesn't appear to be used or required?

 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The output is going to be the same because you aren't creating any SmartPhone objects and your output text doesn't output the OS.

BTW Why does your class have a getPrice() method which doesn't appear to be used or required?



Quite the easy question. I was getting very frustrated last night after staying up till 3:00a.m trying to work on this and I just started to completely crash and give up, so i was code stitching in hopes to get something to work. So that was a piece stitched in that I never rid the code of.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rande Fisher wrote:Quite the easy question. I was getting very frustrated last night after staying up till 3:00a.m trying to work on this and I just started to completely crash and give up, so i was code stitching in hopes to get something to work. So that was a piece stitched in that I never rid the code of.

Argh the trial and error approach to programming. We've all been there and let me give you some advice. Guessing at what might work will always waste more time either now or later on (and probably both) than turning the computer off getting out a pencil and paper and working out how to solve the problem yourself and only once you have a clear detailed description in English (or your native language) trying to write the code.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So have you got it working now?
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Rande Fisher wrote:Quite the easy question. I was getting very frustrated last night after staying up till 3:00a.m trying to work on this and I just started to completely crash and give up, so i was code stitching in hopes to get something to work. So that was a piece stitched in that I never rid the code of.

Argh the trial and error approach to programming. We've all been there and let me give you some advice. Guessing at what might work will always waste more time either now or later on (and probably both) than turning the computer off getting out a pencil and paper and working out how to solve the problem yourself and only once you have a clear detailed description in English (or your native language) trying to write the code.



Starting to see that. So i gave in and asked for help. I'm too prideful to admit my own defeat hence why this is late and I'm now bringing it to others to look at. I will most definitely take your advice on this for my next part. As for this part I have a good understanding of it, I just can't understand how to return the Total after restocking fee to override the total in the superclass and print to each phone. The MobileOS isn't as important since from what the professor said he doesn't care what the OS of the phones are just so long as we get the point that we can pass items in the subclass to the output of the program.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:So have you got it working now?


Still haven't gotten it to return the new TotalValue from the phones after a restock fee
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not having very much luck. I've tried using the InventoryValue, and TotalValue, neither of which will update the value to the overridden value needed.



I'm stressin out at the moment lol. I've gotta take a break from this or my computer might suffer the consequences. This is what I have but it wont function as I need it when it comes to overriding and returning the value. Further advice would be great.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my earlier post. You won't get it to work unless you create objects of SmartPhone. All the objects you create are of type MobilePhone so won't be using the code in the SmartPhone class.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Something like this got the restocking fee to apply, but now I am getting missing symbol errors for the Operating system which I'm not quite seeing. edit: saw an error but didnt change the missing symbol
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the exact message when you have an error ie cut and paste it into here.

I'm guessing your new code won't compile as your array is of type MobilePhone and the class MobilePhone doesn't have a getMobileOS() method. That method is only in the SmartPhone class so you either need to make your array type SmartPhone or cast the the object to type SmartPhone when you call that method. If you do the later you should really test to see if the object is of type SmartPhone before casting it (hint: use instanceof)
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a wholly different note, why does the getInventoryValue() method have a for loop with a hard loop value of '4' and which does the same thing 4 times. The for loop is superfluous as written, what was the intent of the code?
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Please post the exact message when you have an error ie cut and paste it into here.

I'm guessing your new code won't compile as your array is of type MobilePhone and the class MobilePhone doesn't have a getMobileOS() method. That method is only in the SmartPhone class so you either need to make your array type SmartPhone or cast the the object to type SmartPhone when you call that method. If you do the later you should really test to see if the object is of type SmartPhone before casting it (hint: use instanceof)



The only error i get reads:

InventoryPart3.java:24:error: cannot find symbol
System.out.println("The operating system that belongs to this phone is: " +MPhone[i].getMobileOS());
^ pointing at the period
symbol: method getMobileOS()
location: class MobilePhone


so my best guess is that youre correct, it expects it in the MobilePhone class judging by the last statement.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:On a wholly different note, why does the getInventoryValue() method have a for loop with a hard loop value of '4' and which does the same thing 4 times. The for loop is superfluous as written, what was the intent of the code?


I think that was a forgotten code that was copied and moved to its final resting place in the main method which was then rewritten. So just non-needed code.

in my last post the arrow is pointing at the period between MPhone[i] . getMobileOS
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, my previous comment about the problem is exactly right.
Because you have stored the objects in an array of type MobilePhone you only have access to the methods available in MobilePhone so do as I suggested either change the array type or do the check and cast. Doing the check and cast will allow you to create some MobilePhone and some SmartPhone objects and the code will seemlessly handle both.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony,

You sir have been fantastic. I am glad that you helped me with as much as you have. My only thing to fix is the decimal format which I'm not gonna bother you with. That should take a couple seconds to complete. I am very grateful of your help. Have a great rest of your night!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure. Glad to hear you have got it working. Good timing too as it's nearly midnight here and I'm about to shutdown for the night.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:My pleasure. Glad to hear you have got it working. Good timing too as it's nearly midnight here and I'm about to shutdown for the night.


only 7 p.m here, but all of your help has been much appreciated, have a nice night tony.
 
Taj Kore
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rande, could you post the final working code? Just for educational purposes
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!