Class Work (CS 312) 27 Feb 2013

Program Development

There are several full length programs in the book. I would like you to write (copy) these pieces of code and get them to run. Each program will be in a file by itself with .java extension. You must make sure that it runs as shown in the book. These programs do NOT have to be turned in. However, you are likely to see variations of these programs in either the quizzes or the tests.

Working with Random Numbers

  1. Simulate the throw of a coin 10 times - 0 for tail and 1 for head using an object in the Random class.
  2. Simulate the throw of a coin 10 times - 0 for tail and 1 for head using Math.random().
  3. Simulate the throw of a die 10 times using an object in the Random class. You will be randomly generating numbers 1 through 6.
  4. Simulate the throw of a die 10 times using Math.random(). You will be randomly generating numbers 1 through 6.
  5. Simulate the throw of two dice 10 times using an object in the Random class. You will be randomly generating pairs of numbers 1 through 6.

Challenge Problem

Pepys Problem: In 1693 Samuel Pepys asked Issac Newton which is more likely: getting 1 at least once when rolling a fair die six times or getting 1 at least twice when rolling it 12 times. Write a program that could have provided Newton with a quick answer.