Question

Java Random Exchange

I am making a gift exchange program and need assistance with the output.

  • How do I make this code leave the first participant (giver) listed the way that the user input the information (example: a, b, c, d, ...), but the second participant (receiver) a random person from the same array (example: d, c, a, b...). I have tried the solution below which is fine other than the participants are not random they are just one-off of the original input.

  • The participant should only be a giver and a receiver once.

  • The giver and receiver can't be the same person.


The output should be something like this:
a will purchase a gift for d (age: 2)
b will purchase a gift for c (age: 3)
c will purchase a gift for a (age: 4)
d will purchase a gift for b (age: 2)



import java.util.Random;

public class ExchangeResults
{
public static void main(String[] args)
{
UserInput userinput = new UserInput();
Participant[] participants = userinput.inputParticipants();   

System.out.println("Exchange Results");

//PRINT MATCHES
shuffleParticipants(participants);
}

//MAKE RANDOM MATCHES
public static void shuffleParticipants(Participant[] participants)
{
for (int i = 0; i < participants.length-1 ; i++)
{
Participant p1 = participants[i];
Participant p2 = participants[i+1];
System.out.printf("%s will purchase a gift for %s (age: %d)\n",p1.getName(),p2.getName(),p2.getAge());
}
Participant pl = participants[participants.length-1];
Participant p0 = participants[0];
System.out.printf("%s will purchase a gift for %s (age: %d)\n", pl.getName(), p0.getName(), p0.getAge());

}
}


0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Java Random Exchange
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a...

    LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a default constructor in the Family class. This is a method that is empty and its header looks like this: public NameOfClass() Except, for the main(), take all the other methods in LE 6.2 and put them in the Family class. Transfer the class variables from LE 6.2 to Family. Strip the word static from the class variables and the method headers in the Family...

  • Java Programming: Math Quiz

    Starting codes:MathQuiz.javaimport java.util.Scanner;public class MathQuiz {    private final int NUMBER_OF_QUESTIONS = 10;    private final MathQuestion[] questions = new MathQuestion[NUMBER_OF_QUESTIONS];    private final int[] userAnswers = new int[NUMBER_OF_QUESTIONS];    public static void main(String[] args) {        MathQuiz app = new MathQuiz();        System.out.println(app.welcomeAndInstructions());        app.createQuiz();        app.administerQuiz();        app.gradeQuiz();    }    private String welcomeAndInstructions() {        return "Welcome to Math Quiz!\n" +         ...

  • **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args)...

    **JAVA*JAVA Question 1 How many k's values will be printed out? public static void main(Stringl args) for (int k-1: k10:k if (k8) continue: System.out.println k) Question 2 3.5 pts Analyze the following code. Which one is correct? package p public class A package p2: import p1. public class B extends Af int i- 5 private int j- 10: public A0I public static void main(Stringll args) B b new B0: System.out.println(a.i+", "+ajt", "+b.it""+bj): a.i is printable None of them b.i is...

  • Please help!!!!!! import java.util.Random; public class CSE205_Assignment04 { private static Random rnd = new Random(); public...

    Please help!!!!!! import java.util.Random; public class CSE205_Assignment04 { private static Random rnd = new Random(); public static void main(String[] args) { Tree_ctor_test(); Tree_insert1_test(); Tree_insert3_test(); Tree_insertMany1_test(); Tree_insertMany2_test(); Tree_insertMany3_test(); Tree_insertMany4_test(); } public static void Tree_ctor_test() { Tree<Integer> t = new BSTree<Integer>(); assertEqual("", t.toString(), "Tree_ctor_test: toString", false); assertEqual(false, t.contains(0), "Tree_ctor_test: contains", false); } public static void Tree_insert1_test() { Tree<Integer> t = new BSTree<Integer>(); t.insert(1); assertEqual("1 ", t.toString(), "Tree_insert1_test: toString", false); assertEqual(false, t.contains(0), "Tree_insert1_test: contains", false); assertEqual(true, t.contains(1), "Tree_insert1_test: contains", false); } public static...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Here is what I got so far also can anyone help me align the output #include...

    Here is what I got so far also can anyone help me align the output #include <iostream> #include <iomanip> using namespace std; class Person { private: string name;    public: Person() { name = ""; } Person(string name) { this->name = name; } string getName() { return name; } void setName(string name) { this-> name = name; } }; class Publication { private: int pubNumber; string title; Person borrower;    public: static int count; Publication() { title = ""; }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT