Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java problem

Options
  • 29-04-2010 9:14pm
    #1
    Registered Users Posts: 17,965 ✭✭✭✭


    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.:D
    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. :)


Comments

  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    3) Lunch must have a setter and getter for availability and cost

    public boolean getAvailability()
    {

    return this.availability;

    }

    public int getCost()
    {

    return this.cost;

    }

    you might need to use a double on the cost depending on your spec


  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    OP, you're gonna have to show us an attempt. From there we can correct and help you.


  • Registered Users Posts: 17,965 ✭✭✭✭Gavin "shels"


    import java.util.Scanner;

    abstract class Lunch
    {
    Scanner keyboard = new Scanner(System.in);
    private String title;
    private double cost;
    private boolean availability;

    Lunch lunch = new Lunch(String name);

    public boolean getAvailability()
    {

    return this.availability;

    }

    public double getCost()
    {

    return this.cost;

    }


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

    public void takePayment()
    {

    }


    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?


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Should be public otherwise nothing outside the class can access them. Only make something private if you want to protect it from outside access. Things like variables.


  • Registered Users Posts: 310 ✭✭thelibertyboy


    hey gav its kev im stuck on this ****er to hope the guys can help out :)


  • Advertisement
  • Registered Users Posts: 2,265 ✭✭✭Seifer


    hey gav its kev im stuck on this ****er to hope the guys can help out :)
    Maybe try doing one of the other "sample exams" so?


  • Registered Users Posts: 310 ✭✭thelibertyboy


    there isnt any other samples atm but gav i just emailed howell he said theres going to be a big update on moodle tomorrow afternoon :)


  • Registered Users Posts: 17,965 ✭✭✭✭Gavin "shels"


    Ended up getting it done (kinda), had the right concepts myself but needed the help of a lad in my class to throw em together for me.:cool:


  • Registered Users Posts: 310 ✭✭thelibertyboy


    gav do us a favour and pm it to me im so ****in stuck on this thing :)


Advertisement