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

In Java: How would you match together random person objects from an ArrayList to

ID: 3802027 • Letter: I

Question

In Java:
How would you match together random person objects from an ArrayList to make a random group of Pairs and add them to an ArrayList so that each Person has a match and the same person doesn't have the multiple have multiple matches? Example Rob can not be person1 in pair more than once and also can not be person2 in pair more than wants, but has to be both a person1 and person2 just not in the same pair.

public class Person private String name; private String age public class Pair private Person personl; private Person person2 Pair (Person person1 Person person2) More Codex /*This is where the help is need*/ public class Match private ArrayList pairs private ArrayList people; More Codex/

Explanation / Answer

Add getters in the pair class : say getPerson1() and getPerson2()

In the match class ,use the following method :

public static void generatePairs()

{

for(int i=0;i<people.size()/2;i++)

{

pairs.add(new Pair(people.get(i),people.get(people.size()/2+i)));

}

if(people.size()%2==1)

pairs.add(new Pair(persons.get(people.size()-1),people.get(0)));

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote