Have a college exam in this on Tuesday so getting some practice in, this is just a sample of what the exam will be like btw.
Lunches are either sandwiches or hot lunches. All sandwiches come with a meat and a number of salads. All hot lunches come with either chips or side salad. The cost of the lunch is displayed to the user and any change required is calculated. It is always assumed that the customer pays the right amount or more than the right amount (cashier ensures the customer never gives too little, not the program). Sometimes, certain items are not available. The system must print a message telling the customer that the lunch they ordered is not available when they order it.
(a) Implement an abstract class Lunch:
1) Lunch has the following private members: title, cost, availability
2) Lunch must have a constructor which takes in the title as a parameter
3) Lunch must have a setter and getter for availability and cost
4) Lunch must have a printDetails() method which prints the title and cost of the lunch
5) Lunch must have a takePayment() method which takes in the amount of money tendered (given to the cashier) and returns the correct change
(b) Lunch as two subclasses, Sandwich and HotLunch:
1) Sandwich has private member variables: meat type, number of salads
2) HotLunch as a private member variable: chips (true) or side salad (false)
3) The cost of a sandwich is 3 euro for the meat + .55 cent for each salad. There is a limit of 5 salads, and there must be at least 3 salads. Should the customer order above to below those values, set the number of salads to the closest acceptable value (e.g. if they order 1 salad, give them 3, if they order 20, give them 5).
4) HotLunches are 4.50 euro with chips and 3.95 with side salad
5) Write the constructors for both of the subclasses; ensure that the correct parameter is passed up to the super class and that constraints are enforced on the number of salads. The constructor should calculate the cost of the lunch and call the setter method on the super class so that the cost is set.
6) Sandwich should have a getter method for meat.
The bolded ones are the ones were my main problems keep arising, I'm ok with working with them but like for example on the getters and setters I'm not sure what exactly it wants to be returned and stuff.
Any advice, help, guidelines, etc... will be appreicated.
For the other items, show us what you have so far, and where you're stuck.
Cheers for the welcome marc.
public void printDetails ()
public void takePayment()
That's what I have for a4 and a5, not sure on the conditions I need.
Questions b3 and b4 I have separate double varibles, plan on just using a user input for amount of salads which would then be multipled by .55c. Then using if statements to restrict the it to between 1 and 5 salads. They should be fine.
For questions b5 and b6 I'm lost, could be just the way the questions are written tbh.
This line needs some attention. I think this is meant to be the "constructor which takes in the title as a parameter."
The syntax is off, but this closer to a declaration of a variable called "lunch" that is set to a new instance of Lunch. For one thing, this is dangerous because it's declared in the Lunch class, and if every instance of Lunch HAS-A Lunch, then you will be forever creating new lunches.
What does a constructor declaration look like? (Hint: It's kind of like a method declaration, but without a return type.)
Joined: Apr 29, 2010
Got that sorted over the weekend with a bit of help from my mate, had my exam today and well didn't get it working.
Not sure if anyone can help, but I'm sure yous can figure out what it needs to print, the first code section is the super class, then two subclasses and then the test class.