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

import java.util.Random; import java.util.ArrayList; public class MontyHall { //

ID: 671819 • Letter: I

Question

 import java.util.Random; import java.util.ArrayList;  public class MontyHall {  // First, write a function called simulate_prizedoor. // This function will simulate the location of the prize in many games -- see the detailed specification below: // //    Function //    -------- //    simulate_prizedoor // //    Generate a random array of 0s, 1s, and 2s, representing //    hiding a prize between door 0, door 1, and door 2 // //    Parameters //    ---------- //    nsim : int //    The number of simulations to run // //    Returns //    ------- //    sims : ArrayList //    Random ArrayList of 0s, 1s, and 2s //     static ArrayList<Integer> simulate_prizedoor(int nsim) {         // COMPLETE THIS PART      }  // Next, write a function that simulates the contestant's guesses for nsim simulations. // Call this function simulate_guess. The specs: // //    Function //    -------- //    simulate_guess // //    Return any strategy for guessing which door a prize is behind. This //    could be a random strategy, one that always guesses 2, whatever. // //    Parameters //    ---------- //    nsim : int //    The number of simulations to generate guesses for // //    Returns //    ------- //    guesses : ArrayList //    An ArrayList of guesses. Each guess is a 0, 1, or 2 //     static ArrayList<Integer> simulate_guess(int nsim) {         // COMPLETE THIS PART      }  // Next, write a function, goat_door, to simulate randomly revealing one of the goat doors // that a contestant didn't pick. // //    Function //    -------- //    goat_door // //    Simulate the opening of a "goat door" that doesn't contain the prize, //    and is different from the contestants guess // //    Parameters //    ---------- //    prizedoors : ArrayList //    The door that the prize is behind in each simulation //    guesses : ArrayList //    THe door that the contestant guessed in each simulation // //    Returns //    ------- //    goats : ArrayList //    The goat door that is opened for each simulation. Each item is 0, 1, or 2, and is different //    from both prizedoors and guesses //     static ArrayList<Integer> goat_door(ArrayList<Integer> prizedoors, ArrayList<Integer> guesses) {          // COMPLETE THIS PART       }  // Write a function, switch_guess, that represents the strategy of always switching a guess after the goat door is opened. // //    Function //    -------- //    switch_guess // //    The strategy that always switches a guess after the goat door is opened // //    Parameters //    ---------- //    guesses : ArrayList //    Array of original guesses, for each simulation //    goatdoors : ArrayList //    Array of revealed goat doors for each simulation // //    Returns //    ------- //    The new door after switching. Should be different from both guesses and goatdoors //     static ArrayList<Integer> switch_guess(ArrayList<Integer> guesses, ArrayList<Integer> goatdoors) {          // COMPLETE THIS PART       }  // Last function: write a win_percentage function that takes an array of guesses and prizedoors, // and returns the percent of correct guesses // //    Function //    -------- //    win_percentage // //    Calculate the percent of times that a simulation of guesses is correct // //    Parameters //    ----------- //    guesses : ArrayList //    Guesses for each simulation //    prizedoors : ArrayList //    Location of prize for each simulation // //    Returns //    -------- //    percentage : number between 0 and 100 //    The win percentage //     static double win_percentage(ArrayList<Integer> guesses, ArrayList<Integer> prizedoors) {          // COMPLETE THIS PART       }      public static void main(String[] Args) {         int number_of_trials = 10;          // DO NOT MAKE CHANGES TO THE MAIN FILE         ArrayList<Integer> prize = simulate_prizedoor(number_of_trials);         System.out.println(prize);          ArrayList<Integer> guess = simulate_guess(number_of_trials);         System.out.println(guess);                  ArrayList<Integer> goat = goat_door(prize, guess);         System.out.println(goat);                  ArrayList<Integer> new_door = switch_guess(guess, goat);         System.out.println(new_door);          System.out.println(" Win Percentage when not changing the door: " + win_percentage(guess, prize));         System.out.println(" Win Percentage after changing the door: " + win_percentage(new_door, prize));         // DO NOT MAKE CHANGES TO THE MAIN FILE     }    } 

Explanation / Answer


//program//
import java.util.Random;
import java.util.Scanner;
public class table
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Random generator = new Random();
// Initialize Variables
int user_door, open_door, other_door, prize_door;
// Generate random value 1-3
prize_door = generator.nextInt(3) + 1;
other_door = prize_door;
// Begin Game
System.out.println("Welcome to the game!");
// User Validation
do
{
System.out.println("Select the door (1, 2, or 3): ");
user_door = scan.nextInt();
}
while(user_door > 3 || user_door < 0);
do
{
open_door = generator.nextInt(3)+1;
}
while(open_door == prize_door || open_door == user_door);
System.out.println(" In a moment, I will show you where the prize is located,");
System.out.println("but first I will show you what is behind one of the other doors");
System.out.println(" Behind door number " + open_door+ " are goats!");
System.out.println("You selected door number " + user_door);
char userReply;
do
{
System.out.println(" Would you like to switch your door(y/n)? ");
userReply = scan.next().charAt(0);
}
while(userReply!='y' && userReply!='n');   
int user_duplicate = user_door;
if (userReply == 'y')
{
do
{
user_door = generator.nextInt(3)+1;
}
while(user_door == open_door || user_door == user_duplicate);
}
System.out.println("The prize is behind door number: " + prize_door);
if (user_door == prize_door)
{
System.out.println("Congratulations! You won the prize!");
}
else
{
System.out.println("Sorry. You lost.");
}
}
}