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.
- Create a new project called 5.06 Monte Carlo Method in the Mod05 Assignments folder.
- Create a class called BottleCapPrize in the newly created project folder.
- 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.
- Prompt the user for the number of trials. Conduct at least 1000 trials.
- Read back the data for all of the trials from the output file.
- Calculate the average number of caps opened in order to win a prize.
- 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)
{
winCounter++;
counter++;
}
else
{
counter++;
}
}
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
}
inFile.close();
}
}
**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
7
6
4
5
6
2
1
2
1
2
2
5
1
2
14
5
2
3
3
9
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.**