Question

The Assignment Write a Java application (named GoatsAndCars.java) that plays the game automatically. Your application will...

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 player’s choice 2,000 times, and so on). Some of this repetitive code can is unavoidable, and after all, that’s 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()

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class MontyHall
{
    public static void main(String[] args)
    {
        boolean[] withOutChange = new boolean[1000];
        boolean[] withChange = new boolean[1000];
        boolean[] doors = new boolean[3];

        for (int x = 0; x < withOutChange.length; x++)
        {
            placePrizes(doors);
            withOutChange[x] =  playGame(doors, false);
        }

        for (int x = 0; x < withChange.length; x++)
        {
            placePrizes(doors);
            withChange[x] = playGame(doors, true);
        }

        System.out.println("Chances of winning without changing: " + countPrizes(withOutChange) + "%");
        System.out.println("Chances of winning with changing   : " + countPrizes(withChange) + "%");
    } // end of main method

    public static void placePrizes(boolean[] doors)
    {
        for (int x = 0; x < doors.length; x++)
        {
            doors[x] = false;
        }
        int carDoor = (int)(Math.random() * 3);
        System.out.println("CAR DOOR IS: " + carDoor);
        doors[carDoor] = true;
    }  // end of placePrizes method

    public static boolean playGame(boolean[] doors, boolean changeDoor)
    {
        int playerChoice = (int)(Math.random() * 3);
        System.out.println("Players first choice: " + playerChoice);
        int revealedDoor = -1;

        for (int x = 0; x < doors.length; x++)
        {
            if (!doors[x] &&  x != playerChoice)
            {
                revealedDoor = x;
                break;
            }
        }
        System.out.println("revealed door: " + revealedDoor);

        if (changeDoor)
        {
            int randomNumber = -1;
            int originalChoice = playerChoice;
            do
            {
                randomNumber = (int)(Math.random() * 3);
            }
            while(!(randomNumber != originalChoice && randomNumber != revealedDoor));
            playerChoice = randomNumber;
            System.out.println("The players second choice is: " + playerChoice);
        }  // end of if block
        return doesPlayerWin(playerChoice, doors);
    } // end of playGame method

    public static boolean doesPlayerWin(int playerChoice, boolean[] doors)
    {
        if (doors[playerChoice])
        {
            System.out.println("You won!");
            return true;
        }
        else
        {
            System.out.println("You lost...");
            return false;
        }
    } // end of doesPlayerWin method

    public static double countPrizes(boolean[] wins)
    {
        int winsCount = 0;
        for (int x = 0; x < wins.length; x++)
        {
            if (wins[x] == true)
            {
                winsCount++;
            } // end of if statement
        }  // end of for block
        return ((double)winsCount/(double)wins.length) * 100.0;
    }
} // end of class
Add a comment
Know the answer?
Add Answer to:
The Assignment Write a Java application (named GoatsAndCars.java) that plays the game automatically. Your application will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...

    Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies. 2. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard. This is the base code given: public class NumbersDemo { public static void main (String args[]) { // Write your...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • Please help me write these in R script / Code 1, Suppose you're on a game...

    Please help me write these in R script / Code 1, Suppose you're on a game show, and you're given the choice of three doors. Behind one door is a car; behind the others, goats. You pick a door, say #1, and the host, who knows what's behind the doors, opens another door, say #3, which has a goat. He then says to you, "Do you want to pick door #2?" What is the probability of winning the car if...

  • MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans...

    MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans IDE to create the main class called MasterMind.java MasterMind class Method main() should: Call static method System.out.println() and result in displaying “Welcome to MasterMind!” Call static method JOptionPane.showMessageDialog(arg1, arg2) and result in displaying a message dialog displaying “Let’s Play MasterMind!” Instantiate an instance of class Game() constants Create package Constants class Create class Constants Create constants by declaring them as “public static final”: public...

  • in java Feedback? CHALLENGE ACTIVITY 5.2.2: Printing array elements. Write three statements to print the first...

    in java Feedback? CHALLENGE ACTIVITY 5.2.2: Printing array elements. Write three statements to print the first three elements of array run Times. Follow each statement with a newline. Ex: If runTime = (800,775, 790, 805, 808) print: 800 775 790 Note: These activities will test the code with different test values. This activity will perform two tests, both with a 5 element array. See 'How to Use zyBooks". Also note: If the submitted code tries to access an invalid array...

  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

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