aspose file tools*
The moose likes Beginning Java and the fly likes Java help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java help" Watch "Java help" New topic
Author

Java help

Gav Doc
Greenhorn

Joined: Apr 29, 2010
Posts: 6
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Gav Doc
Greenhorn

Joined: Apr 29, 2010
Posts: 6
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.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Let's start at the beginning--show us the code you have for a3, a4, and a5.
Gav Doc
Greenhorn

Joined: Apr 29, 2010
Posts: 6


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
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343


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

Joined: Apr 29, 2010
Posts: 6
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

It'd be easier if you'd TellTheDetails: what's supposed to happen, what actually happens, etc. Why make us guess?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Rancher

Joined: Sep 29, 2008
Posts: 12617

Note also that the method "calculateCost(...)" doesn't.
Gav Doc
Greenhorn

Joined: Apr 29, 2010
Posts: 6
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

Can't help then. But you could fix the issues I mentioned as a start.
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
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


-Nev
"It's about choosing your battles, not fighting somebody else's war"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
. . . and nobody told them what iffy practice writing == true and == false is?
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

It's much simpler to just write:

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java help