I need help doing this assignment. Here are the instructions:

                **5.06 Assignment Instructions – Bottle Cap Prize**

Instructions: Write a program that uses the Monte Carlo sampling method to estimate
the average number of bottles of e-Boost someone would have to drink to win a prize.
There is a 1 in 5 chance that a bottle cap will have a prize.

  1. Create a new project called 5.06 Monte Carlo Method in the Mod05 Assignments folder.
  2. Create a class called BottleCapPrize in the newly created project folder.
  3. Determine how many bottle caps each person has to open in order to find a winning cap. (This represents one trial.) Print this value to a text file. Review Dr. Lin’s suggestion about performing this simulation with dice.
  4. Prompt the user for the number of trials. Conduct at least 1000 trials.
  5. Read back the data for all of the trials from the output file.
  6. Calculate the average number of caps opened in order to win a prize.
  7. Print the result to the screen.

Suggestion: Write this program in stages. You may need to spread this assignment out
over time in order for all the pieces to fall into place.

• Play with some dice to visualize the Monte Carlo Method.
• Plan your algorithm. Write pseudocode or at least an outline.
• Work on the code that conducts trials and temporarily print the results to the screen; however, during testing only use about 20 trials.
• Print the results of each trial (the number of caps opened to get a prize) to a file.
• Use Notepad to verify the file data matches the screen output.
• Read the trial data back in and calculate the average.
• Print the results.

Expected Output: When your program runs

Here is what I have so far:

import java.util.Scanner;

import java.util.Random;

import java.io.IOException;

import java.io.PrintWriter;

import java.io.File;

public class BottleCapPrize1
    public static void main(String [] args)throws IOException
        Scanner in;  

        in = new Scanner(System.in);

        int randomNumber = ((int)(0+ Math.random()* 5));

        int counter = 0;

        int winCounter = 0;

        PrintWriter outFile = new PrintWriter (new File("bottleCap.txt"));

        while(counter < 20)

            randomNumber = ((int)(0+ Math.random()* 5));

            outFile.println("Wins: " + winCounter + " Total: " + counter);

            if (randomNumber == 1)




        outFile.close ( );

        String token = "";

        File fileName = new File("bottleCap.txt");

        Scanner inFile = new Scanner(fileName);

        while (inFile.hasNext())
            // don't know what to put here



**The problem is that I have no idea what I am doing. I got this code from someone else on a form that has the same assignment, but they had problems too. The problems with the code right now is that it never shows output (i think it may be an infinite loop), and whenever it gets numbers from the text file, which are


it replaces them with this when I try to run it

Wins: 0 Total: 0
Wins: 1 Total: 1
Wins: 2 Total: 2
Wins: 2 Total: 3
Wins: 2 Total: 4
Wins: 2 Total: 5
Wins: 2 Total: 6
Wins: 2 Total: 7
Wins: 2 Total: 8
Wins: 2 Total: 9
Wins: 2 Total: 10
Wins: 2 Total: 11
Wins: 2 Total: 12
Wins: 2 Total: 13
Wins: 3 Total: 14
Wins: 3 Total: 15
Wins: 3 Total: 16
Wins: 3 Total: 17
Wins: 3 Total: 18
Wins: 3 Total: 19

I'm doing this in the BlueJ compiler, if that makes a difference. If anyone can help, please put it in very simple terms.**

Because you are trying to modify other people code and you completely have no idea at all. This is a bad start. What you need to do is to step back and look at the problem again.

Now, Monte Carlo method is used to estimate a value using randomized samples. Now, how to apply this algorithm to your application -- bottle cap prize.

Step one, you need to understand how the application works. The main purpose is to find a winning bottle and the chance of finding the bottle cap prize is 1 in 5. What does that tell you? It means that the chance to win a prize from opening each bottle is 20%. This does not mean that there are 5 bottles to open but rather give you a number in your random method.

Step two, you will need to create a flow of the process.

  | Open a cap |<------------------------------+
  +------------+                               |
         | apply Monte Carolo (random number)  |
         V                                     |
         '                                     |
       /   \         +------------------+      |
     / found \  no   | increment failed |      |
    .  prize  .----> |   count number   |------+
     \   ?   /       +------------------+
       \   /
         | yes
    | Display |
    | Results |

Step three, think about how to apply the Monte Carlo method to your application. One way to apply this algorithm is to randomly pick a number from 0, 1, 2, 3, and 4. Let say if the winning number is 0. Therefore, if a picked number is equal to 0, the bottle cap is the winning one; otherwise, it is not found and count as failed. Keep picking until the number is equal to the winning number (zero). Once the winning number is picked, go to the next step.

Step four, while you keep picking a number and comparing with the winning number, each time will be cumulatively counted. This is the total bottle caps from start until the winning one is found. Therefore, display the result of total bottles needed in order to find the winning one.

Step five, once you are done up to step four, you are now done the process for 1 trial. What you need to do is to add another loop (keep restarting) up to 1000 trials. Each time you go through, you need to cumulatively keep the total bottle caps. Once you are done with the total trials, compute the average using the total number of bottle caps you cumulate from the loop and the total trial number (1000). Display the average and you are done.

This article has been dead for over six months. Start a new discussion instead.