This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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
Joined: Nov 13, 2011
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.
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.
Joined: Oct 13, 2005
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.