• 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

Java help

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gav Doc wrote:...on the getters and setters I'm not sure what exactly it wants to be returned and stuff...


Welcome to the ranch!

For help on the getters and setters, see this tutorial on encapsulation.

For the other items, show us what you have so far, and where you're stuck.
 
Gav Doc
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

marc weber wrote:

Gav Doc wrote:...on the getters and setters I'm not sure what exactly it wants to be returned and stuff...


Welcome to the ranch!

For help on the getters and setters, see this tutorial on encapsulation.

For the other items, show us what you have so far, and where you're stuck.



Cheers for the welcome marc.

public void printDetails ()
{
System.out.println(title);
System.out.println(cost);
}

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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start at the beginning--show us the code you have for a3, a4, and a5.
 
Gav Doc
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That's what I have for Question A, all the parts together. Bit stuck on the method conditions. Also should the methods and getters and setters be public or private?


EDIT by mw: Added code tags.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How come a5 isn't satisfied? Is that what you mean by "method conditions" (which doesn't make any sense to me, by the way)?

Regarding public/private getters--if it's private, who'd be able to call them? Who needs to be able to call them?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gav Doc wrote:...should the methods and getters and setters be public or private?


The encapsulation tutorial linked to above says...

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods.

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Gav Doc
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It'd be easier if you'd TellTheDetails: what's supposed to happen, what actually happens, etc. Why make us guess?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This purpose of this code eludes me:This code shouldn't even compile:Nor will this, for the same reason plus an additional one:It's almost certainly not what you mean, either.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note also that the method "calculateCost(...)" doesn't.
 
Gav Doc
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See can't post what it's meant to print out as the question has been taken off our internal system in college.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't help then. But you could fix the issues I mentioned as a start.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:This purpose of this code eludes me:



lmao. If sports == true then set sport to...what...it...already.....is?? Wait I'm confused. :P
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I were to guess, I would say the code was originally written as...

Then the coder realized it would be better to simply use...

...but just forgot to remove the original block.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and nobody told them what iffy practice writing == true and == false is?
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: . . . and nobody told them what iffy practice writing == true and == false is?



Well the variable is obviously boolean so really all he'll need to do to fix that problem is



maybe even an ternary statement.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's much simpler to just write:

No need for an if-statement or a ternary expression.
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:It's much simpler to just write:

No need for an if-statement or a ternary expression.



agreed, was just clarifying something stated previously.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic