Question: Create a Java file called chores.java . Inside, write a Java lot-drawing program that allows the user to enter a list of names and a

Create a Java file called chores.java . Inside, write a Java lot-drawing program that allows the user to enter a list of names and a list of chores (equal lengths). The program should assign each person a random chore. Print out the pairs (i.e. name + chore). My Answer and review make use of the shuffle method within the collections framework but I am going to insist that you make use of it.

import java.util.*; import java.util.ArrayList; import java.util.Scanner; import java.util.Random; //Define the main function. class chores { //Define the main function.

static int getNum(ArrayList v) { // Size of the vector int n = v.size(); // Make sure the number is within // the index range int index = (int)(Math.random() * n); // Get random number from the vector int num = v.get(index); // Remove the number from the vector v.set(index, v.get(n - 1)); v.remove(n - 1); // Return the removed number return num; }

public static void main(String[] args) { //Declare two Array lists. ArrayList name = new ArrayList(); ArrayList chore = new ArrayList(); //Create an object of Scanner class. Scanner sc = new Scanner(System.in); //Prompt the user to enter the number of chors //and the names wants to enter. System.out.print("Please enter the count of " + "name and chores: "); //Store the count in the variable count. int count = sc.nextInt(); //For consuming nextLine charecter before //entering the string values. sc.nextLine(); //Declare and initialize a variable. int loop = 0; //Begin the while loop to enter the names and the chores. while (loop <= count) { System.out.print("Please enter your name: "); name.add(sc.nextLine()); System.out.print("Please enter your chore: "); chore.add(sc.nextLine()); loop++; }

for(int i = 0; i < name.size(); i++){ System.out.println(name.get(i)); } for(int i = 0; i < chore.size(); i++){ System.out.println(chore.get(i)); }

ArrayList v = new ArrayList<>(count); // Fill the vector with the values // 0,1, 2, 3, ..., n-1 for (int i = 0; i <= count; i++) v.add(i); // While vector has elements // get a random number from the vector and print it // Randomly selecting chore for each person int ind = 0; while (v.size() > 0) { System.out.println("Random chore selected for name " + name.get(ind++) + " is " + chore.get(getNum(v)) + "."); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!