Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
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??
Lucy Margret wrote:Yes, this is my question.. How do I write the condition for the price to be greater than 0??
Lucy Margret wrote:and how do I create the collection??
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
what I wrote "price>0" all you need to is put that in the conditional you choose to use.
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.
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
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
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
In your main class, as in any other class, to "call" a class, instantiate it, you follow this process:
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!
Don't declare a list by its implementation. Declare it by its interface.Les Morgan wrote:. . .
I like ArrayLists, so "ArrayList<FastFood> ff = new ArrayList();" . . .
Lucy Margret wrote:
So I created a polymorphic array but i got an error I think am doing it wrong
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
Then in your main method call things like:
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
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.
Lucy Margret wrote:
Is this the way to do it? Using a ternary operator
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
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;
Lucy Margret wrote:
or this way?
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.
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?
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
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?
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"!!
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?
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 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!
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
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!
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?
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
Lucy Margret wrote:Ok this is the updated code
main class for testing
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|