Update code so that each participant gets to purchase for another random participant. Meaning the purchaser and giver could be different people. Need output to be (potentially different giver for each person than the person purchasing them a gift): Jill purchases a gift for Bob Bob purchases gift for Steve Steve purchases a gift for John John purchase a gift for Jill Currently, it prints out this (which makes the exchange just between those two people instead of each person having someone different other than the person purchasing a gift for them): Jill would exchange gifts with Bob Steve would exchange gifts with John
- Update code so that each participant gets to purchase for another random participant. Meaning the purchaser and giver could be different people.
Need output to be (potentially different giver for each person than the person purchasing them a gift):
Jill purchases a gift for Bob
Bob purchases gift for Steve
Steve purchases a gift for John
John purchase a gift for Jill
Currently, it prints out this (which makes the exchange just between those two people instead of each person having someone different other than the person purchasing a gift for them):
Jill would exchange gifts with Bob
Steve would exchange gifts with John
2. Create UML Diagram
// Participant Class
class Participant {
// Private Members
private String firstName;
private int age;
public Participant(String firstName, int age) {
this.firstName = firstName;
this.age = age;
}
// Getters And Setters
public String getFirstName() {
return firstName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
import java.util.Random;
// Main Class
public class Main {
public static void main(String[] args) {
// Input Participants From The User
UI ui = new UI();
Participant[] participants = ui.inputParticipants();
// Shuffle The Array Of Participants
shufflepParticipants(participants);
// Print Gifts Exchanged
System.out.println("\nGifts\n--------------------");
for (int i = 0; i < participants.length / 2; i++) {
Participant p1 = participants[i];
Participant p2 = participants[participants.length - i - 1];
System.out.printf("%s(%d) Would Exchange Gift With %s(%d)\n",
p1.getFirstName(), p1.getAge(), p2.getFirstName(), p2.getAge());
}
}
// Shuffle Array Of Participants
public static void shufflepParticipants(Participant[] participants) {
Random rand = new Random();
Participant temp;
// Shuffle Array
for (int i = participants.length - 1; i > 0; i--) {
int index = rand.nextInt(i + 1);
temp = participants[index];
participants[index] = participants[i];
participants[i] = temp;
}
}
}
// UI Class
import java.util.Scanner;
public class UI {
Scanner input = new Scanner(System.in);
Participant[] inputParticipants() {
// Input Number Of Participants
System.out.print("Input Number Of Participants: ");
int numParticipants = Integer.parseInt(input.nextLine());
// While Number Of Participants Is Odd,
// Keep Inputting Number Of Participants
while (numParticipants % 2 == 1) {
System.out.println("Number Of Participants Must Be Even!");
System.out.print("Input Number Of Participants: ");
numParticipants = Integer.parseInt(input.nextLine());
}
// Initialize Array Of Participants
Participant[] participants = new Participant[numParticipants];
// Input All Particicpants
for (int i = 0; i < participants.length; i++) {
System.out.printf("Participant #%d\n-------------------\n", i + 1);
// Input Participant First Name And Age
System.out.print("Enter Name: ");
String firstName = input.nextLine();
System.out.print("Input Age: ");
int age = Integer.parseInt(input.nextLine());
participants[i] = new Participant(firstName, age);
System.out.println();
}
// Return Array Of Participants
return participants;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps