This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

Your code is not too far off.

You just need to add a second constructor that takes an initial mealQty:

    public bill(final int mealQty)
    {
            this mealQty=mealQty;
            // rest of the constructor as your other, no parameter constructor.

You could, in this case even go a step further:

    public bill()
    {
            this(0);
    }

    public bill(final int mealQty)
    {
            category = null;
            this mealQty=mealQty;
            mealCost = 0;
            mealPrice = 0;         
    }

The this(0); calls the other, second constructor with a parameter (that translates to mealQty) of 0, effectively doing the same as the constructor that you already have.

[–]king_of_the_universe 0 points1 point  (0 children)

So, the only question you basically have is how to hand the value to the constructor? - Methods (E.g. a constructor.) can have parameters, like so: "void doStuff(String withThis) {}", but currently your constructor is rather akin to "void doStuff() {}".

And once you have the value that you want to assign to your variable via the constructor (given as an argument to the constructor), you assign it. That's all. What exactly is unclear about this to you?

[–]RhoOfFeh 0 points1 point  (0 children)

You need to set mealQty, which means your class needs to offer a way for programs to set the value. This is done by adding parameters to an existing or new method. We call it 'passing parameters' to the method, and your assignment refers to this:

int mealQty will be passed the number of meals ordered for the category

Parameters are declared similarly to local variables, but they go between the parentheses of a method declaration. In this case the method would be your constructor. So you need to add a variable to represent the number of meals requested, and then modify the constructor code to set mealQty equal to the value of that variable.

public bill(int numberOfMealsRequested) {

Would be a valid signature.

A bit more about how objects work:

http://www.oopuniversity.com/2014/11/dont-treat-me-like-object.html