Question

I want to add a Guest to the array guests, can you guys see what am doing wrong, or cant I call a a string, constructor, constructor ?

The error says: the method addGuest(Guest) in the type table is not applicable for the argument ( String, Tea,Cake). I am trying to put a guest in to an array

here is my code.

guests[0]=table.addGuest("Alice",new Tea("RoseShip Tea",false,true),new Cake("Chocolate Sponge"));

and constructor of class Guest is like this:

public Guest(String name, Tea newTea, Cake newCake)

constructor of class Tea is:

public Tea(String name, boolean suiker, boolean melk)

Class Cake:

public Cake(String name) {
    this.name = name;
}

addGuest method:

public void addGuest(Guest guest)

and what am trying to do is this:

guests[0]=table.addGuest(new Guest("Alice"),new Tea("RoseShip Tea",false,true),new Cake("Chocolate Sponge"));
Was it helpful?

Solution 2

You should use,

Guest g = new Guest("Alice",new Tea("RoseShip Tea",false,true),new Cake("Chocolate Sponge"));
table.addGuest(g);

instead of

guests[0]=table.addGuest("Alice",new Tea("RoseShip Tea",false,true),new Cake("Chocolate Sponge"));

because you pass the arguments required for the constructor of Guest to the wrong method(addGuest(<Guest>)), which requires the actual Guest-object.

EDIT
Further, this will also not work

guests[0]=table.addGuest(...);

because table.addGuest(...) is of type void, so it wont return anything, so you will get an compiler error.
I recommend rethink the use of guests[] , you could probably use a collection (like LinkedList or ArrayList) to solve this problem.

OTHER TIPS

You are passing wrong parameters to the method. addGuest can only take guest parameter in your program. either you hava to overload method to support your parameters like addGuest(String, Tea,Cake) or you have to pass only guest name as parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top