aspose file tools*
The moose likes Beginning Java and the fly likes Help getting back on track with assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help getting back on track with assignment" Watch "Help getting back on track with assignment" New topic
Author

Help getting back on track with assignment

Justin Jackson
Greenhorn

Joined: Nov 13, 2011
Posts: 2
I am having problems with this assignment. Needless to say I have been a little confused since we got to classes and objects. I had to create a hierarchy of classes which i have done. Now I am struggling to assign my variables for each snack. I have my getters and setters but I have now completely confused my self. I am thinking I have to create an instance of snickers which I have tried. Am I on the wrong track. Any help will be appreciated.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

What exactly is your question? does this compile, does it run, does it throw an error, does it give a result you didn't expect (and what would that be?)?

It's next to impossible to help you if you don't make it clear what kind of help you would like.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Justin Jackson
Greenhorn

Joined: Nov 13, 2011
Posts: 2
My code compiles. My problem is I can not determine how to set my values for the different snacks. I created an instance of snickers in the main method. I believe I need to use that instance to set my variables and do this for each snack.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357
    
  23
Are you putting the description of the snack into its class name? If so, then you can get that back by using getClass().getName() (or getSimpleName()).

That is not really what a toString() method is for. It ought to return a brief description of the Snack, so you might describe it as a “Spicy snack: Bombay Mix with 123 calories costing 99¢” Since that is the same for all kinds of Snack, a toString() method which returns that should be in the Snack class. Then there is no need to override it in the Spicy class. Polymorphism, you know.
If you want to use a BombayMix class, too, you can have this sort of hierarchyYou can get “Spicy snack:” by writing the appropriate toString() method in the Snack class. You can add “ Bombay Mix with 123 calories costing 99¢” by writing this sort of toString method:The only problem is that it will be awkward to change BombayMix to Bombay Mix in the output.
I would suggest in this case, the Snack and Spicy classes ought to be abstract.
Why are you using float arithmetic? You probably don’t need floating-point arithmetic, since most calorie values are given as whole numbers. If you do use floating-point arithmetic, however, use doubles.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357
    
  23
A few minutes ago, I wrote:. . . Polymorphism, you know. . . .
You can only learn that sort of thing by trying it. Write some snacks and see what happens. Give the Snack class a toString method using getClass().getSimpleName(), as I suggested earlier, and try it with subclasses.You learn precious little by reading about it, but you learn lots by trying it out and seeing what happens. If something happens which you don’t understand, then you can look for an explanation. That is when helping you from this end of the Ranch becomes particularly interesting

Also, why are you not setting the values of price and calories in the constructor? You don’t want a Snack object going round with a 0¢ price or 0cal, so set up those values in the constructor, and get rid of the no-arguments constructors. You will of course have to use super(...); in the subclass constructors.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help getting back on track with assignment