Question: The Assignment Write a Java application (named GoatsAndCars.java) that plays the game automatically. Your application will run 1,000 trials in which the player does not
The Assignment
Write a Java application (named GoatsAndCars.java) that plays the game automatically. Your application will run 1,000 trials in which the player does not switch, store the results (car or goat) in an array (prizeArray) and then loop through the array to determine how often the player wins the car.
Then your application will run 1,000 trials in which the player switches, store the results in prizeArray, and record how often the player wins the car.
Finally, your application will compare the two results and report whether switching doors improves the odds of winning the car or not.
As you work out an algorithm to solve this problem you will no doubt notice that since the game is played 2,000 times, there is a lot of repetition in the code (putting the goats and cars behind the doors 2,000 times, selecting the players choice 2,000 times, and so on). Some of this repetitive code can is unavoidable, and after all, thats what repetition structures are designed for. But some of this repeated code can be eliminated by having the main method call other methods, thereby, for example, having a method that stores the variables in prizeArray.
To improve your applications performance (by a nanosecond or two), and your programming skills, your application will include the following methods (in addition to public static void main():
public static void placePrizes()
public static void doesPlayerWin()
public static void countPrizes()
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
