file output/ input & file handling

Thread Solved

Join Date: Sep 2009
Posts: 77
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training

Re: file output/ input & file handling

 
0
  #11
Sep 27th, 2009
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:
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
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,967
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 516
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: file output/ input & file handling

 
0
  #12
Sep 27th, 2009
From your assignment spec:
  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.

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.
Last edited by VernonDozier; Sep 27th, 2009 at 4:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 55
quuba quuba is offline Offline
Posting Whiz

Re: file output/ input & file handling

 
0
  #13
Sep 27th, 2009
In this case
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
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training

Re: file output/ input & file handling

 
0
  #14
Sep 27th, 2009
Originally Posted by VernonDozier View Post
From your assignment spec:
  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.

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.
ohhhh, that makes perfect sense. your right. i have one more question though, in this part of the code
  1. while(counter <= 20)
  2. {
  3.  
  4. randomNumber = ((int)(0+ Math.random()* 5));
  5. outFile.println(winCounter); <----- what would i put here to make the output like what you described?
  6.  
  7.  
  8. if (randomNumber == 1) <-------- how could i change this
  9. { to represent what
  10. you described, would
  11. != work?
  12. winCounter++;
  13. counter++;
  14.  
  15. }
  16. else
Last edited by gibson.nathan; Sep 27th, 2009 at 6:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training

Re: file output/ input & file handling

 
0
  #15
Sep 27th, 2009
alright, i think this may be the way to do what you were describing,
  1. PrintWriter outFile = new PrintWriter (new File("bottleCap.txt"));
  2.  
  3. while(counter <= 20)
  4. {
  5.  
  6. randomNumber = ((int)(0+ Math.random()* 5));
  7. outFile.println(winCounter);
  8.  
  9. if (randomNumber == 1)
  10. {
  11.  
  12. winCounter++;
  13.  
  14. }
  15.  
  16.  
  17.  
  18. }
  19.  
  20.  
  21. outFile.close ( );
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training

Re: file output/ input & file handling

 
0
  #16
Sep 27th, 2009
but sometimes it drives me nuts when professors use incorrect terminology like this.
dude i wish i had a professor, lol im taking ap computer science through a distance learning program at my high school. im a junior, and i have no teacher i can actually talk to. sadly that why im on these forums so much.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,967
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 516
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: file output/ input & file handling

 
0
  #17
Sep 27th, 2009
Originally Posted by gibson.nathan View Post
alright, i think this may be the way to do what you were describing,
  1. PrintWriter outFile = new PrintWriter (new File("bottleCap.txt"));
  2.  
  3. while(counter <= 20)
  4. {
  5.  
  6. randomNumber = ((int)(0+ Math.random()* 5));
  7. outFile.println(winCounter);
  8.  
  9. if (randomNumber == 1)
  10. {
  11.  
  12. winCounter++;
  13.  
  14. }
  15.  
  16.  
  17.  
  18. }
  19.  
  20.  
  21. outFile.close ( );
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.

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".

  1. 3
  2. 1
  3. 2
  4. 4
  5. 0
  6. 3
  7. 1
  8. 4
  9. 4
  10. 2
  11. 3
  12. 0
  13. 2
  14. 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:


It 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training

Re: file output/ input & file handling

 
0
  #18
Sep 27th, 2009
im getting very confused. i tried to follow your explanation and i somehow arrived at this:

  1. for(int counter = 0; counter <= 20; counter++)
  2. {
  3. randomNumber = ((int)(0+ Math.random()* 5));
  4.  
  5. while (randomNumber == 1 )
  6. {
  7. winCounter++;
  8. counter++;
  9. outFile.println(loseCounter);
  10.  
  11. while(randomNumber == 2)
  12. {
  13.  
  14. winCounter++;
  15. counter++;
  16.  
  17. {
  18. while (randomNumber == 3)
  19. {
  20. winCounter++;
  21. counter++;
  22.  
  23.  
  24. while (randomNumber == 4)
  25. {
  26. winCounter++;
  27. counter++;
  28.  
  29. while (randomNumber == 5)
  30. {
  31. winCounter++;
  32. counter++;
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: gibson.nathan is an unknown quantity at this point 
Solved Threads: 0
gibson.nathan gibson.nathan is offline Offline
Junior Poster in Training

Re: file output/ input & file handling

 
0
  #19
Sep 27th, 2009
oh i meant to move the outfile to the for loop and change all the wincounters on 2-5 to losecounters
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,967
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 516
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: file output/ input & file handling

 
0
  #20
Sep 27th, 2009
ONE for-loop. ONE do-while loop inside of that for-loop. Here's a skeleton:

  1. // create an output file and open it.
  2.  
  3.  
  4. int numTrials = 20;
  5.  
  6. for (int i = 0; i < numTrials; i++) // this is the start of your OUTER loop
  7. {
  8. // create ONE counter variable and initialize it to 0
  9. int randomNumber = -1; // initialize to some nonsense value. This will be over-written.
  10.  
  11. do /* this is the start of your INNER loop */
  12. {
  13. // generate a random number and assign randomNumber to it
  14. // increment the counter variable.
  15. }
  16. 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 */
  17.  
  18. // write contents of the counter variable to the file.
  19. } /* End of outer loop */
  20.  
  21. // 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1497 | Replies: 28
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC