Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

9.4 Write a complete program that stores and prints participants in a party. As

ID: 3682263 • Letter: 9

Question

9.4

Write a complete program that stores and prints participants in a party. As part of your program, write a Party class that implements these members:

An instance constant that holds the maximum number of guests.

Three instance variables that hold, respectively, the actual number of guests, an array of the

names of the guests, and the party host’s name.

A constructor that stores the maximum number of guests and the host’s name.

An addGuest method that adds a guest to the guest list or prints an error message if the guest is

already on the list or there’s no more room on the guest list.

A helper method, isOnList, that receives a parameter named guest. The method returns

true if guest is on the guest list and returns false otherwise.

A printParty method that prints the party’s host and guests.

Provide a driver class that tests your Party class. Your driver class should contain this main

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

When compiled and run, your driver class and Party class together should produce this output:

Roberto Baggio is already on the guest list.

Diego Maradona cannot come to the party. The guest list is full.

Guest list for David Beckham's party:

Roberto Baggio

Zinedine Zidane

Johan Cruyff

Explanation / Answer

class Party

{

//declaring variables

private final int max;

int actualnumber;

String guests[]=new String[50];

String host;

//initializing them through constructor

public Party(int n,String x)

{

max=n;

host=x;

}

//method to add guest names to array

void addGuest(String name)

{

int i=0;

//finding if guest already exists on th list

if(!isOnList(name))

{

while(guests[i]!=null)

i++;

//finding if guest list is full

if(i<max)

//adding guest to list

guests[i]=name;

else

System.out.println(name+" cannot come to the party. The guest list is full");

}

else

{

System.out.println(name+" is already on the list");

}

}

//method to print all guest names

void printParty(){

int i=0;

System.out.println("guest list for "+host+"'s party:");

while(guests[i]!=null)

{

System.out.println(guests[i]);

i++;

}

}

//method to find if a guest name already exists

boolean isOnList(String name)

{

int i=0;

while(guests[i]!=null)

{

//if name is already on list return true

if(guests[i].equals(name))

return true;

i++;

}

return false;

}

}

public class TestClass

{

//driver class that tests party class

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();

}

}