| | |
file output/ input & file handling
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
wow. i think there is a discrepancy in our understanding of what each other means. i am simulating a drink contest with bottle tops. each bottle top has a one in five chance of winning. i have to create an output file that records the... wait never mind, here are the exact instructions: i have gotten pretty far, but it is the last part that is getting me. once i import the file back i don't exactly know how to use the imported numbers to calculate the probability that i need. the rest though i think i have done pretty correctly.
•
•
•
•
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 times a die must be rolled in order to
win a prize. (This represents one trial.) Print this value to a
text file.
4. Conduct at least 1,000 trials.
5. Read the data back in from all of the trials.
6. Calculate the average number of times a die must be rolled in order to win a prize.
7. Print the result to the screen.
Suggestion: Write this program in stages. First, work on the part that conducts trials and
print the results to the screen; however, during testing only use about 20 trials. Second,
print the results of each trial (the number of rolls to get a prize) to a file. Check the file to
verify it matches the screen output. Third, read the trial data back in and calculate the
average. Fourth, print the results.
Expected Output: When your program runs, your output should simply print a message
indicating the average number of bottles of Boost you would need to drink to win a prize
•
•
Join Date: Jan 2008
Posts: 3,967
Reputation:
Solved Threads: 516
From your assignment spec:
There is some very loose, inaccurate language in this assignment spec. You're stuck with it, but sometimes it drives me nuts when professors use incorrect terminology like this. The probability is KNOWN ahead of time and has nothing to do with any random rolls of a die. Similarly, the expected number of trials that it will take before you get your first winner is also known and can be calculated EXACTLY. Hence any random number generation would be an incorrect approach to this problem. Take words like "determine", "calculate", and "probability" with a grain of salt here. I commented in a similar vain regarding statistics/probability terminology last night to someone. Might have been another of your threads. Not sure. Anyway, end of sermon regarding terminology.
Regarding your problem, since in line 3, "trial" is defined as the number of times before you get a winner, your output file should look something like this:
These should average to around 5. Read 'em all in and take the average. That's your answer to number 6. Your file is incorrect. You should have no zeroes. If you open ten bottles and the first nine aren't winners and the tenth is, you write this to the file:
not this:
I can't think of any other way to read the spec.
- 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 times a die must be rolled in order to
win a prize. (This represents one trial.) Print this value to a
text file. - Conduct at least 1,000 trials.
- Read the data back in from all of the trials.
- Calculate the average number of times a die must be rolled in order to win a prize.
- Print the result to the screen.
There is some very loose, inaccurate language in this assignment spec. You're stuck with it, but sometimes it drives me nuts when professors use incorrect terminology like this. The probability is KNOWN ahead of time and has nothing to do with any random rolls of a die. Similarly, the expected number of trials that it will take before you get your first winner is also known and can be calculated EXACTLY. Hence any random number generation would be an incorrect approach to this problem. Take words like "determine", "calculate", and "probability" with a grain of salt here. I commented in a similar vain regarding statistics/probability terminology last night to someone. Might have been another of your threads. Not sure. Anyway, end of sermon regarding terminology.
Regarding your problem, since in line 3, "trial" is defined as the number of times before you get a winner, your output file should look something like this:
6 3 7 2 6 8 10 2These should average to around 5. Read 'em all in and take the average. That's your answer to number 6. Your file is incorrect. You should have no zeroes. If you open ten bottles and the first nine aren't winners and the tenth is, you write this to the file:
not this:
0
0
0
0
0
0
0
0
0
1I can't think of any other way to read the spec.
Last edited by VernonDozier; Sep 27th, 2009 at 4:11 pm.
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 55
In this case
only marked lines are important
where Wins: can be omitted ( always==1 winner)
and Total: can be a another counter, which can be setup to zero ,when the record is saved to a file.
•
•
•
•
Wins: 0 Total: 0
Wins: 0 Total: 1
Wins: 0 Total: 2
Wins: 0 Total: 3
Wins: 0 Total: 4
Wins: 0 Total: 5
Wins: 1 Total: 6 <----------
Wins: 1 Total: 7
Wins: 1 Total: 8
Wins: 2 Total: 9 <----------
Wins: 2 Total: 10
Wins: 2 Total: 11
Wins: 2 Total: 12
Wins: 2 Total: 13
Wins: 2 Total: 14
Wins: 2 Total: 15
Wins: 2 Total: 16
Wins: 3 Total: 17 <----------
Wins: 4 Total: 18 <----------
Wins: 4 Total: 19
where Wins: can be omitted ( always==1 winner)
and Total: can be a another counter, which can be setup to zero ,when the record is saved to a file.
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
•
•
•
•
From your assignment spec:
- 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 times a die must be rolled in order to
win a prize. (This represents one trial.) Print this value to a
text file.- Conduct at least 1,000 trials.
- Read the data back in from all of the trials.
- Calculate the average number of times a die must be rolled in order to win a prize.
- Print the result to the screen.
There is some very loose, inaccurate language in this assignment spec. You're stuck with it, but sometimes it drives me nuts when professors use incorrect terminology like this. The probability is KNOWN ahead of time and has nothing to do with any random rolls of a die. Similarly, the expected number of trials that it will take before you get your first winner is also known and can be calculated EXACTLY. Hence any random number generation would be an incorrect approach to this problem. Take words like "determine", "calculate", and "probability" with a grain of salt here. I commented in a similar vain regarding statistics/probability terminology last night to someone. Might have been another of your threads. Not sure. Anyway, end of sermon regarding terminology.
Regarding your problem, since in line 3, "trial" is defined as the number of times before you get a winner, your output file should look something like this:
6 3 7 2 6 8 10 2
These should average to around 5. Read 'em all in and take the average. That's your answer to number 6. Your file is incorrect. You should have no zeroes. If you open ten bottles and the first nine aren't winners and the tenth is, you write this to the file:
not this:
0 0 0 0 0 0 0 0 0 1
I can't think of any other way to read the spec.
Java Syntax (Toggle Plain Text)
while(counter <= 20) { randomNumber = ((int)(0+ Math.random()* 5)); outFile.println(winCounter); <----- what would i put here to make the output like what you described? if (randomNumber == 1) <-------- how could i change this { to represent what you described, would != work? winCounter++; counter++; } else
Last edited by gibson.nathan; Sep 27th, 2009 at 6:36 pm.
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
alright, i think this may be the way to do what you were describing, it seems like that would keep it from printing out all those zeros, but now when i go to open the txt to look at the output notepad goes non-responding.
Java Syntax (Toggle Plain Text)
PrintWriter outFile = new PrintWriter (new File("bottleCap.txt")); while(counter <= 20) { randomNumber = ((int)(0+ Math.random()* 5)); outFile.println(winCounter); if (randomNumber == 1) { winCounter++; } } outFile.close ( );
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
•
•
•
•
but sometimes it drives me nuts when professors use incorrect terminology like this.
•
•
Join Date: Jan 2008
Posts: 3,967
Reputation:
Solved Threads: 516
•
•
•
•
alright, i think this may be the way to do what you were describing,it seems like that would keep it from printing out all those zeros, but now when i go to open the txt to look at the output notepad goes non-responding.Java Syntax (Toggle Plain Text)
PrintWriter outFile = new PrintWriter (new File("bottleCap.txt")); while(counter <= 20) { randomNumber = ((int)(0+ Math.random()* 5)); outFile.println(winCounter); if (randomNumber == 1) { winCounter++; } } outFile.close ( );
You have an infinite loop here. counter never gets adjusted.
You need to step back and look at your program and do a redesign. For 20 trials, you will generate far more than 20 random numbers. You don't know how many numbers you'll need to generate, but the number of trials will be known (20, later to be 1000), so stick that in a for-loop.
Within each trial, you'll generate a random number and from that, decide whether you have a "winner". You've decided to have the range be 0 to 4 and have a winner declared if that number is 1. That's fine. When 1 shows up, that's the end of the trial, correct? Hence that needs to be somewhere in a while loop. For brevity's sake, let's say you have three trials. Let's say these are your random numbers. Note that you do not know in advance how many random numbers you'll need to produce. Here's one "run".
text Syntax (Toggle Plain Text)
3 1 2 4 0 3 1 4 4 2 3 0 2 1
Lines 1 through 2 are trial 1. Lines 3 through 7 are trial 2. Lines 8 through 14 are trial 3. Your output file will be this:
2
5
7It took 2 tries for trial 1, 5 for trial 2, 7 for trial 3.
We're talking about a nested loop. How many trials? 3. That's an exact number known ahead of time, so we're talking about a for-loop. The inner loop is the trial itself. Do we know how many times that loop will be gone through in advance? No. While loop or Do-While loop. Is it guaranteed to occur at least once? Yes? Do-while loop (you could have a plain old while loop if you prefer). How do we know when the inner loop is done? When the random number is 1. Keep that in mind when thinking of the loop condition you need to write.
How many numbers are we outputting to the file? 3. This answer should help you place where to put the file output statement.
•
•
Join Date: Sep 2009
Posts: 77
Reputation:
Solved Threads: 0
im getting very confused. i tried to follow your explanation and i somehow arrived at this:
that is probably entirely wrong. i modeled this more towards the counting the number of wrong so that i actually get the number it takes to get the prize, but my program is still doing the infinite loop thing and i cant look at the output.
Java Syntax (Toggle Plain Text)
for(int counter = 0; counter <= 20; counter++) { randomNumber = ((int)(0+ Math.random()* 5)); while (randomNumber == 1 ) { winCounter++; counter++; outFile.println(loseCounter); while(randomNumber == 2) { winCounter++; counter++; { while (randomNumber == 3) { winCounter++; counter++; while (randomNumber == 4) { winCounter++; counter++; while (randomNumber == 5) { winCounter++; counter++;
•
•
Join Date: Jan 2008
Posts: 3,967
Reputation:
Solved Threads: 516
ONE for-loop. ONE do-while loop inside of that for-loop. Here's a skeleton:
That's it! Not much code. ONE do-while loop inside of ONE for-loop. You have far too many loops.
JAVA Syntax (Toggle Plain Text)
// create an output file and open it. int numTrials = 20; for (int i = 0; i < numTrials; i++) // this is the start of your OUTER loop { // create ONE counter variable and initialize it to 0 int randomNumber = -1; // initialize to some nonsense value. This will be over-written. do /* this is the start of your INNER loop */ { // generate a random number and assign randomNumber to it // increment the counter variable. } while (randomNumber != 1); /* End of inner loop. Do the inner loop while there is not a winner. It's a winner if the random number is 1 */ // write contents of the counter variable to the file. } /* End of outer loop */ // close the output file
That's it! Not much code. ONE do-while loop inside of ONE for-loop. You have far too many loops.
Last edited by VernonDozier; Sep 27th, 2009 at 9:01 pm.
![]() |
Similar Threads
- Trouble with file handling (Python)
- file handling (C++)
- help needed on file handling (Python)
- about file handling (C)
- File handling: How to search and replace strings in file using user input (C)
- please give me a simple file handling program (C)
- file handling in html and jsp (HTML and CSS)
- C File handling - search within file without reading content? (C++)
- Bubble sort & File output jibrish errors??? (C)
Other Threads in the Java Forum
- Previous Thread: Scanner library error
- Next Thread: java.
Views: 1497 | Replies: 28
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener add android applet arguments array arrays binary build c++ chat class classes client code combobox component convert data database db design detection eclipse error event exception file forloop fractal givemetehcodez graphics gui helpwithhomework homeworkassignment html ide image input integer interface j2me java javafx jframe jpanel jtextarea jtextfield julia key lazy linked linked-list list loop looping main method methods mobile netbeans newbie number object objects oracle output page pattern pixel problem programming read recursion remote remove return robot scanner screen search server service set size sql string swing system test text text-file threads timer transfer translate tree user web






