MontyHallParadox The purpose of this assignment is to learn
MontyHallParadox
The purpose of this assignment is to learn how to do comparisons using the if statement and passing arguments into main() through the command line. Additionally, you get more practice on how to write all of your own code and Javadoc comments.
Problem Description
The detailed description of this problem comes from the Programming Exercise E6.19 that is in the book (pages 300 & 301).
You are writing a program that runs experiments to test the Monty Hall Paradox. You will get two numbers on the command line (as strings in args of the main function). The first number is for the seed of the random number generator. You should use java.util.Random for this assignment. Not using this random number generator could result in different findings than in the unit tester. For example, you should create the random number generator thusly:
You will then run experiments to see whether sticking with your original choice or switching your choice results in more wins. The number of experiments will be passed as the second argument on the command line.
All of your code for simulating the Monty Hall Paradox will go in a static method with the signature public static int simulateMontyHallParadox(int experiments, int seed). This method will run the experiments, count how many times the contestant would have won if they switched doors and return that count. The inputs to the function are int experiments which is the number of times the experiment should be run and int seed which is the seed to the random number generator. The reason we use a specified seed for the random number generator is so that I can exactly control the outcomes which makes grading the same for all students.
Once you run all the experiments, you simply return the results which can be printed out in main. The results could look like this:
Hints:
All rejoice! This program does not require using the Scanner object.
The logic for these experiments are actually quite simple. You only need to generate a pick and the door the car is behind each iteration of the loop.
Getting Started
Like all of our exercises, so far, we are going to do this exercise by writing the source code that solves the problem first in MontyHallParadox.java. Using the techniques shown on the web page titled How to Start Every Project in this Class create a source file called MontyHallParadox.java. This is where your code will go.
Starting this week we don\'t have any code to copy for the assignment. You get to do it all! Don\'t forget to provide proper Javadoc documentation
Now go through MontyHallParadox.java, add the proper headers as in past assignments and then change the [CHANGE THIS TO YOUR INFORMATION] text to the proper items. There are two items to be changed.
Once you\'ve written your code run it by single clicking on MontyHallParadox.java in the package explorer and selecting Run->Run from the menu or using the keyboard shortcut. Examine the output. Does it do what you want? If not, how can you modify the code to do what you want?
Solution
SOURCE CODE:
import java.util.Random;
public class MontyHallParadox
{
public static void main(String[] args)
{
int s = simulateMontyHallParadox(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
System.out.println(\"Winners by switching: \" + s);
}
public static int simulateMontyHallParadox(int experiments,int seed)
{
int wins = 0;
Random generator = new Random(seed);
for(int i = 0;i < experiments;i++ )
{
int[] doors = {0,0,0};
doors[generator.nextInt(3)] = 1;
int choice = generator.nextInt(3);
int shown;
do{
shown = generator.nextInt(3);
}while(doors[shown] == 1 || shown == choice);
wins = wins + doors[3 - choice - shown];
}
return wins;
}
}
OUTPUT:
sh-4.3$ javaHelloWorld 2442443 98886788
Winners by switching: 1627965
sh-4.3$

