• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to demonstarte the idea of Polymorphism and inheritance in Java?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Listen, I really need help with my assignment I've been working on it for hours and I'm just stuck!

I don't think I understood the question fully and what's the ms is actually expecting me to output for her.

I created the super class along with it's 2 sub classes and the main class for testing everything.

1- But How do I make a condition for the value price to be greater than 0? 2- What does the Ms. mean by saying create a collection of of three Food that are either FastFood or Pizza? 3- and how do I add objects to the array in the main class?

This is the question:

Create a class Food that has:
a. Two private attributes • name which specifies the name of food • price, which specifies the price as a double value. The price can be any value, but must be checked to be greater than 0, in order to be valid.

b. A constructor with two arguments that sets the values of both instance variables to those passed as parameters. Implement the class Food with all mentioned methods in addition to set and get methods.

Derive a class FastFood that inherits from Food and has
a. a private attribute type, which specifies whether the fast food is “Fresh” or “Hot”.

b. a constructor that initializes all attributes Implement the class FastFood with all mentioned methods in addition to accessor and mutator methods.

Derive a class Pizza that inherits from FastFood and has
a. two private attributes • an array toppings of five String values • a boolean value for isSpicy that specifies whether the sauce is spicy (value 1) or not (value 0).

a. a constructor that initializes all instance variables. The type of the FastFood should be set to “Hot” value.

4.Write a test program that test the above classes demonstrating the idea of polymorphism. Let us say we want to set up a collection of three Food that are either FastFood or Pizza. Create an array of type Food and add objects to it. Next output the information about the chosen food using method toString().

and this is my code so far:




and for the sub class FastFood:




sub class Pizza:



and finally this is my main class:

 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.) price>0
2.) you have two classes FastFood and Pizza, you need to implement those and take 3 objects and put them in a collection.
3.) in your main class you have an array, let's say it is called myArray, so you would take one of the open indexes, maybe 4, and say MyArray[4]=MyObject.
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this is my question.. How do I write the condition for the price to be greater than 0??
and how do I create the collection??
 
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:Yes, this is my question.. How do I write the condition for the price to be greater than 0??
and how do I create the collection??



So the first question is, how are you meant to specify the price? Are you meant to type it in at the command line? Are you meant to hardcode it in your main method?
Secondly, I think your teacher wants you to use a primitive array and not use the collections. Have you studied collections? If not, then you would need to create an array of type FastFood, so that you can store both FastFood and Pizza in there.
BTW, I don't know why you have created no-arg constructors, and why you are calling them in your main method. From the assignment you have been given, it seems to me your teacher only wants constructors that take arguments.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:Yes, this is my question.. How do I write the condition for the price to be greater than 0??


what I wrote "price>0" all you need to is put that in the conditional you choose to use.

Lucy Margret wrote:and how do I create the collection??


I like ArrayLists, so "ArrayList<FastFood> ff = new ArrayList();" Then add 3 of the appropriate objects to it.
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what I wrote "price>0" all you need to is put that in the conditional you choose to use.



So I did it this way in the Food class

 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BTW, I don't know why you have created no-arg constructors, and why you are calling them in your main method. From the assignment you have been given, it seems to me your teacher only wants constructors that take arguments.



I don't know how to call them in my main class so I thought the only way is to create objects for them
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:I don't know how to call them in my main class so I thought the only way is to create objects for them



In your main class, as in any other class, to "call" a class, instantiate it, you follow this process:

Once you have a reference to the instantiated object you can have appropriate access to it's resources...
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:

what I wrote "price>0" all you need to is put that in the conditional you choose to use.



So I did it this way in the Food class



Kind of - you would have to actually set the price, and not output "valid value".
However, you also need to remember that price can be set in the constructor, so you need to stop price being set <= 0 in the constructor too.

In the main you should have variables such as price and name, set them (maybe by reading in from the console), and then pass them when calling the constructors.




 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In your main class, as in any other class, to "call" a class, instantiate it, you follow this process:



So I created a polymorphic array but i got an error I think am doing it wrong

 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel like I'm messing up the code more I'm so lost sorry! I'm still a beginner in Java so I don't really get it what you're saying but I really appreciate you explaining to me!

 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean I didn't even understand what's the output she wants! Like she should provide a sample output so I know what to code and how it should look like
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:I feel like I'm messing up the code more I'm so lost sorry! I'm still a beginner in Java so I don't really get it what you're saying but I really appreciate you explaining to me!



First let's aim to get the classes right before we worry about putting them in an array and calling toString().

This code is wrong:


Do the following instead:


I don't actually like the type choices "fresh" or "hot" because you can get a freshly made hot pizza!

So you need to get rid of all constructors that have parameters because your teacher doesn't seem to want these.

So get rid of Food(), FastFood() and Pizza().

Then in your main method call things like:

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:. . .
I like ArrayLists, so "ArrayList<FastFood> ff = new ArrayList();" . . .

Don't declare a list by its implementation. Declare it by its interface.
Don't use a raw type. Always make sure to apply the generics to the implementation. That should read:-
List<FastFood> ff = new ArrayList<>();
In earlier versions than Java7, you had to insert the actual type parameter in the <>.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:
So I created a polymorphic array but i got an error I think am doing it wrong


Without telling us what error you received, then all we can say is: yes, you're doing it wrong.

But with the code you showed, I saw you created an array with 3 elements 0-2, you filled elements 0 and 1, then you ask for the first 5 elements from the array that only has 3 elements of which you only have 2 filled. Therein is probably your problem at this point.

Please say what errors you get so we can help in tracking the errors too.

BTW: relax, breathe, center your thoughts... we've all been there before--you have to go through the beginning to get to the end. Even 30 years of coding experience starts out with a "Hello Program" sometimes several.
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edited the code




Food class:



FastFood class:




Pizza class:


I deleted the extra constructors and I created a reference object for the 2 sub classes
It builds successfully when I run it
But now the output is the problem I don't what to output and how
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now! In my main class she's asking to output only 3 objects so the array should contain only 3
but in the Pizza class the toppings in the array are 5
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Then in your main method call things like:



This object is called referencing object, right? I can use it on any class that inherits from the main class
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry about the output for the moment. Sort out the classes. This is wrong.



It is wrong because
a) setPrice() should be setPrice(double price).
b) You're not actually setting the price in the method, you are simply outputting that it is valid without setting it.

You also need to consider how you are going to stop someone setting the price to <= 0 in the constructor.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:I got it now! In my main class she's asking to output only 3 objects so the array should contain only 3
but in the Pizza class the toppings in the array are 5



Yes.
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:Don't worry about the output for the moment. Sort out the classes. This is wrong.



It is wrong because
a) setPrice() should be setPrice(double price).
b) You're not actually setting the price in the method, you are simply outputting that it is valid without setting it.

You also need to consider how you are going to stop someone setting the price to <= 0 in the constructor.



Is this the way to do it? Using a ternary operator

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:
Is this the way to do it? Using a ternary operator



You could indeed use a ternary operator, but that isn't the issue here.

The issue is that if price > 0, you shouldn't output "Valid value". Instead, you should set this.price=price;
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:

Ahmed Bin S wrote:Don't worry about the output for the moment. Sort out the classes. This is wrong.



It is wrong because
a) setPrice() should be setPrice(double price).
b) You're not actually setting the price in the method, you are simply outputting that it is valid without setting it.

You also need to consider how you are going to stop someone setting the price to <= 0 in the constructor.



Is this the way to do it? Using a ternary operator



or this way?

 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:

Lucy Margret wrote:
Is this the way to do it? Using a ternary operator



You could indeed use a ternary operator, but that isn't the issue here.

The issue is that if price > 0, you shouldn't output "Valid value". Instead, you should set this.price=price;



So this way is better then

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:

or this way?



If you replace the < with <= (less than or equal) then that would work. Although personally I would have:


Now how about stopping someone from setting the price to less than or equal to zero when calling the Constructor in the main method.

For example, I can currently do:



This is wrong, because we will end up setting the price to 0 in the object. Now this one is actually a bit tricky for a beginners exercise, because you need to stop an object from being created if the price is less than or equal to zero - maybe the best approach for a beginners exercise would be to allow the price to be set to <= 0, but stop anyone from viewing this incorrect price. So, your getPrice() method should check that the price is greater than 0, and if it is, return the price, and if it isn't, output a message saying the price is invalid.
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:

Lucy Margret wrote:

or this way?



If you replace the < with <= (less than or equal) then that would work. Although personally I would have:


Now how about stopping someone from setting the price to less than or equal to zero when calling the Constructor in the main method.

For example, I can currently do:



This is wrong, because we will end up setting the price to 0 in the object. Now this one is actually a bit tricky for a beginners exercise, because you need to stop an object from being created if the price is less than or equal to zero - maybe the best approach for a beginners exercise would be to allow the price to be set to <= 0, but stop anyone from viewing this incorrect price. So, your getPrice() method should check that the price is greater than 0, and if it is, return the price, and if it isn't, output a message saying the price is invalid.



But then what will I put in the setPrice?
the getMethod can return a value within the if statement or outside it?
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:
But then what will I put in the setPrice?
the getMethod can return a value within the if statement or outside it?



setPrice is fine now, don't worry about that. You are checking if the price is <= 0, and if it is, you don't allow it to be set.

However, setPrice isn't the only way you can set the price. You also set the price when you create the object.

Look at your own code, what are you doing here?



You are setting the price.

So whilst your code can currently stop someone setting the price <= 0 when calling setPrice, it cannot stop them from setting the price to <= 0 when creating the object. Even if you put that check you have inside setPrice inside the constructor, it still wouldn't work, because the price variable would simply be set to 0 when the food object is created.

Yes, you can return a value from within the if statement, but ignore my suggestion, as your method returns a double, and so you can't just output that the price is wrong.
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So a simple "solution" would be to stop anyone from viewing the price if it is set to <= 0. So your getPrice() would look like




oh ok! I wrote it this way



I'll fix it! Thank you! So doing this way we can ensure that no one will be able to set an invalid value, right?
The other classes are they correct?
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is when I do your way writing the return in the if rather than the else, I get an error saying "missing return statement"!!
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:


So a simple "solution" would be to stop anyone from viewing the price if it is set to <= 0. So your getPrice() would look like




oh ok! I wrote it this way



I'll fix it! Thank you! So doing this way we can ensure that no one will be able to set an invalid value, right?
The other classes are they correct?



No, please see my post above. That won't actually work, you will need to throw an exception, but I think that's just complicating things for a beginners exercise, so just leave getPrice as it was.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucy Margret wrote:The thing is when I do your way writing the return in the if rather than the else, I get an error saying "missing return statement"!!



Indeed, which is why I changed it - have you been taught about exceptions?
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:

Lucy Margret wrote:The thing is when I do your way writing the return in the if rather than the else, I get an error saying "missing return statement"!!



Indeed, which is why I changed it - have you been taught about exceptions?



This way?

 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BTW: relax, breathe, center your thoughts... we've all been there before--you have to go through the beginning to get to the end. Even 30 years of coding experience starts out with a "Hello Program" sometimes several.



I'm trying to center my thoughts and all but this is my first assignment on polymorphism and the due date for it is tomorrow and the ms refuses to help us or explain the question for us!
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand, I've had a few of those classes myself.

Lucy Margret wrote:
I'm trying to center my thoughts and all but this is my first assignment on polymorphism and the due date for it is tomorrow and the ms refuses to help us or explain the question for us!

 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:I understand, I've had a few of those classes myself.

Lucy Margret wrote:
I'm trying to center my thoughts and all but this is my first assignment on polymorphism and the due date for it is tomorrow and the ms refuses to help us or explain the question for us!



Can you help me with my code, please? My latest code that I fixed with Ahmed can you check it please and tell me if I'm doing something wrong?
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last piece of code you posted in the thread looks good, the one where you throw the exception if the price is less than or equal to 0. So I would assume you have code now that compiles and gives you answers, they may not be what you exactly want yet, but hopefully they work enough to run.

I am going to bed, I will be up at about 6 more hours, and I'll take a look before I head over to a nearby town to teach some firemen how to use ham radios. So if you are having problems--focus on what is going on, list each and work on them 1 at a time. Give a good description of what is left along with errors or descriptions of what is not right--lucid descriptions, because this is after all, you asking us for help, so please make it easy for us to understand what it is that you think is wrong.

I will check back in the morning--well, later this morning.

Les

BTW: remember you have to declare or catch that cost exception you throw--the caller should be in a try/catch block.

Lucy Margret wrote:Can you help me with my code, please? My latest code that I fixed with Ahmed can you check it please and tell me if I'm doing something wrong?

 
Lucy Margret
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok this is the updated code

Right now, there are no errors thank god!
But I'm still not getting an output, and I don't what the output should be in order to code it
How do I use the array in with the 5 toppings in the Pizza class? what's it's use?
and how do I call all the objects from the classes in main class to show the needed output?
also, you said something about catch and try.. where do I put that?..

Super class



Sub class FastFood



sub class Pizza



main class for testing

 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, you have a setToppings with no parameter--setters always take arguments, you are going to set something equal to the argument value, so you need to take in String values for toppings and set what toppings you have on the pizza. I would do just a straight 5 Strings so you make your users enter each topping and a dummy value or "" for none.

OK now for your output. You need to take your FoodArray in main and use toString() to print out the information from your foods. You will need to implement a toString() method in your Food object for this. So look back in Food and see what properties you have that can be printed and make a method to print them:

hopefully that will take care of this assignment for you.

Lucy Margret wrote:Ok this is the updated code


main class for testing

 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic