Make a complete program which stores and prints the participants of a party. As
ID: 3666806 • Letter: M
Question
Make a complete program which stores and prints the participants of a party. As a part of your program, write
a Party class that uses 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
import java.util.*;
class Party
{
int n;
ArrayList <String> names=new ArrayList<String>();
String h;
Party(int n1, String h1)
{
n=n1;
h=h1;
}
void addGuest(String na)
{
if(names.size()<3 && !names.contains(na))
names.add(na);
else
System.out.println(na+"cannot come to the
party. The guest list is full.");
}
void printParty()
{
System.out.println("Host:"+h);
System.out.println("Guest are:");
for(int i=0;i<3;i++)
{
System.out.println(names.get(i));
}
}
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
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.