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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
  1. 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;
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education