Implement Lab PartyGuests based on the activity diagram provided. Here is a summ
ID: 3820545 • Letter: I
Question
Implement Lab PartyGuests based on the activity diagram provided. Here is a summer of the functionality the lab will provide: It reads in a certain number of guests from the user, prints the party list, selects a random guests who cannot come, and prints the updated party list. Pay attention to the comments. They give you additional information regarding specific actions.
null Guests is set to 4 input.. to read in user input declare and initialize from keyboard ocal variable rand to generate randeom number guestList Arraylist of type String nform user that we' read in numberOf Guests guests declare control varaible and initialize it increment control variable guest list guest name numberofGuests guests have been entered] print the guest list select random guest remove random guest T- Inform user that this guest won't come remove guest form the guest list print the guest list Sample output: Please enter 4 guests: guesti: Dan guest 2: Ben guest 3: Ron guest4: Tim Guest list: Dan Ben, Ron, Tim Dan can't come Updated guest list: [Ben Ron Tim]Explanation / Answer
import java.util.Scanner;
import java.util.Random;
public class guest {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
//user input to decide number of guests
System.out.print("Enter how many friends: ");
int numOfGuests = Integer.parseInt(scan.nextLine());
//Create a string array to store the names of your guests
String arrayOfNames[] = new String[numOfGuests];
for (int i = 0; i < arrayOfNames.length; i++) {
System.out.print("Enter the name of friend " + (i+1) + " : ");
arrayOfNames[i] = scan.nextLine();
}
//Now show your friend's name one by one
System.out.print("Guest List: [");
for (int i = 0; i < arrayOfNames.length; i++) {
System.out.print(arrayOfNames[i]+',');
}
System.out.print("] ");
//selecting random guest who can not come
Random rand = new Random();
int x = rand.nextInt(numOfGuests);
System.out.println(arrayOfNames[x]+" won'tcome");
//Now print the updated list
System.out.print("Guest List: [");
for (int i = 0; i < arrayOfNames.length; i++) {
if(i != x){
System.out.print(arrayOfNames[i]);
if(i < (arrayOfNames.length-1)){ //Added Code
System.out.print(',');
}
}
}
System.out.print("] ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.