-1

I am trying to figure out how to call and output my methods correctly. However, in the output, only one method call is shown (the last one). How do I get it to output all of the method calls and not just the last one. Seems like all of the previous method calls are getting overridden and only the last one persists. I am just beginning in Java. Any help would be appreciated.

Here is the PartyDriver Class where I make 5 method calls. Only the last one is showing in the printParty method.

import java.util.ArrayList;

public class PartyDriver
{

public static void main(String[] args)

{


  Party party = new Party(3, "David Beckham");

  ArrayList<String> guest = new ArrayList<>();

  party.addGuest("Roberto Baggio");

  party.addGuest("Zinedine Zidane");

  party.addGuest("Roberto Baggio");

  party.addGuest("Johan Cruyff");

  party.addGuest("Diego Maradona");

  party.printParty();

} // end main

}//End Party Driver

Here is the Party Class with all of my methods:

import java.util.ArrayList;

public class Party
{


  // instance variables that will hold your data
  // private indicates they belong to this class exclusively


  private int maxGuests;
  private String host;
  private String guest;



  //Constructor
  public Party(int maxGuests, String host)
  {

    this.maxGuests = maxGuests;
    this.host = host;
  }



  //getter
  // define type of data to be returned
  public String getHost()
  {
    return host;
  }



  //setter
  // setters have type void b/c they return nothing
  public void setHost(String host)
  {
    this.host = host;
  }


 //*************************************
 //Method to add to guest list
  public void addGuest(String guest)
  {

    this.guest = guest;
  }


//*************************************
//Method to print party
public void printParty()
  {
    System.out.println("Guest list for " +
      this.host + "'s party is: \n\n" +
       this.guest + ".\n");

  } // end Print Party method


}//end class Party
Fryguy
  • 17
  • 4

1 Answers1

0

No methods but PrintParty() print anything because you haven't told any methods but PrintParty() to print anything.

One way to print output to the console is to use System.out.println().

I've adjusted each of your methods to print something to the screen. I also added a guests instance variable to your Party class and made your addGuest(String guest) method modify that instance variable appropriately.

import java.util.ArrayList;

public class Party
{
    // instance variables that will hold your data
    // private indicates they belong to this class exclusively
    private int maxGuests;
    private String host;
    private ArrayList<String> guests;

    //Constructor
    public Party(int maxGuests, String host)
    {
        System.out.println("Initializing party with maxGuests: '" + maxGuests + "' and host: '" + host + "'");
        this.guests = new ArrayList<String>();
        this.maxGuests = maxGuests;
        this.host = host;
    }

    //getter
    // define type of data to be returned
    public String getHost()
    {
        System.out.println("Setting host to: " + host);
        return host;
    }

    //setter
    // setters have type void b/c they return nothing
    public void setHost(String host)
    {
        System.out.println("Setting host to: " + host);
        this.host = host;
    }

    //*************************************
    //Method to add to guest list
    public void addGuest(String guest)
    {
        if (guests.size() < maxGuests)
        {
            System.out.println("Adding guest: " + guest);
            this.guests.add(guest);
        }
        else
        {
            System.out.println("Guest list full");
        }
    }

    //*************************************
    //Method to print party
    public void printParty()
    {
        System.out.println("Guest list for " +
        this.host + "'s party is: \n\n" +
        this.guests + ".\n");
    } // end Print Party method

    public static void main(String[] args)
    {

      Party party = new Party(3, "David Beckham");
      party.addGuest("Roberto Baggio");
      party.addGuest("Zinedine Zidane");
      party.addGuest("Roberto Baggio");
      party.addGuest("Johan Cruyff");
      party.addGuest("Diego Maradona");
      party.printParty();

    } // end main

}//end class Party
  • Okay I will try this suggestion that you made. But what I was saying is that my printParty method is supposed to return more than just one name. It is suppose to return 3 names. It is only returning the last name which is "Diego Maradona". I called the addGuest method 5 times. But only one name ("Diego Maradona") seemed to be added to the Guest method. I can't figure out why the other names aren't being added to the guest list because they are also a part of the printParty method. The printParty method is supposed to print all of the names on the guest list and it's only printing one name. – Fryguy Apr 02 '15 at 12:28