LMU ☀️ CMSI 186
PROGRAMMING LABORATORY
LAB 2: VIRTUAL DARTS Due: 2020-02-06

Learning Outcomes

Students will be able to (1) write scripts in the Java programming language with variables, assignments, if-statements, etc. (2) employ random number generators in Java, (3) build Java programs with multiple methods, (4) use command line arguments in Java, (5) convert between strings and numbers in Java, (6) know when and how to check arguments to methods and throw IllegalArgumentException when necessary, and (7) build programs against a test suite.

Reading

Read (and interact with) my notes on Java Basics up to the section on “Arrays.”

Activities

You’re working on the beach when all of the sudden your Internet connection goes down. You need the value of π, but you don’t remember what it is, you can’t search the web, and you left your books at home 😔. While you are pondering your situation, a group of four people comes up to you to say hi. While chatting, you discover one of them shares your birthday. You wonder “What are the odds? 🤔” But you have no internet, no books, and you haven’t taken a probability course yet.

But wait! You can estimate the answers you need, with the help of, of all things, (virtual) darts. Knowing that the area of the unit circle is π, and the area of the square this circle inscribes is 4, you can throw a bunch of darts in the square and see what percentage of them end up in the circle. That’s π/4:

Pi Simulation
Darts thrown:
Inside circle:
4 × (inside/total):

This simulation is purposely slowed down to give you a visual idea of how things work. Your solution will be able to easily simulate millions and millions of darts very quickly.

And how can you get the odds of N people having the same birthday? Run a bunch of trials of N randomly selected dates (equivalently, throw N darts at a calendar). Over time, the ratio of the number of trials with shared dates to the number of total trials approaches the true probability you are looking for.

Remember, this is a lab!

Not only will you be writing programs to simulate virtual darts and compute shared birthday probabilities, you’ll be experimenting with your code and answering questions. It’s fun!

Initialize a repository

This is your first serious Java programming lab, so I’ve helped you get started. At GitHub, select Import Repository. Then:

For Your old repository’s clone URL
Enter https://github.com/rtoal/cmsi-186-lab-2-starter-code
For Your new repository details
Enter the repository name cmsi-186-lab-2.

The import might take a few minutes. When done, clone the repo and get ready to work.

Complete the PiEstimator class

In the file PiEstimator.java:

  1. Complete the method to estimate π from a given number of darts. Throw an IllegalArgumentException with the message At least one dart required if the number of darts is less than 1.
  2. Complete the main method invoke your estimator function with the sole command line argument. Remember that command line arguments are strings, so you will have to convert the string to an integer. If you do not have exactly one command line argument, print the message Exactly one argument required to System.err. If the argument is not in the form of an integer, print the message Argument must be an integer to System.err.

Your program should run as follows:

$ javac PiEstimator.java && java PiEstimator 1000000

Once your code is working, carry out the following two experiments (you will be reporting your results in your hard copy submission):

  1. In general, how many additional darts does it take to get one additional decimal place of accuracy in your π estimation? Provide evidence for your answer in the form of your estimation output (i.e., write the final estimate, and the number of darts used).
  2. Do you think there is a point of diminishing returns where it isn’t worth the increase in darts in order to get an additional decimal place? At how many darts and decimal places do you reach this point?

Complete the SharedBirthday class

In the file SharedBirthday.java:

  1. Complete the method to estimate the probability that N people have a shared birthday, from an M-day calendar. Compute the probability estimate by throwing N darts at a calendar of M days, for a given number of trials. The method is parameterized by (1) the number of people, (2) the number of days, and (3) the number of trials. Throw (absolutely no pun intended) an IllegalArgumentException if the number of people is less than 2, the number of days on the calendar is less than 1, or the specified number of trials is less than 1.
  2. Complete the main method invoke your estimator function with three command line arguments (people, days, trials). Remember that command line arguments are strings, so you will have to convert the strings to integers. If you do not have exactly three command line arguments, print the message Exactly three arguments required to System.err. If any argument is not in the form of an integer, print the message Arguments must all be integers to System.err. If any of your arguments are out of range (<2 people, <1 day, <1 trial), capture the exception message from your estimator method and write it to System.err.

Your program should run as follows:

$ javac SharedBirthday.java && java SharedBirthday 4 365 100000

Once your code is working, carry out the following two experiments:

  1. What is the probability, according to your program, that there is a shared birthday, in a 365 day calendar, among a group of 2 people? 100 people? 50 people? Throw at least 1,000,000 darts.
  2. What is the number of people at which the probability of a shared birthday is greater than ½?

Test

Great news! I’ve decided to supply you with automated tests for this assignment. That means you can write your code and run my test script. If the any of the tests fail, you’ll know you have to fix something up. Writing code against pre-written tests is really, really nice. There are far fewer surprises waiting for you when you get your graded lab back.

I’ve included my test files in the starter repo, which you will have imported and cloned. All you should need to do is invoke:

$ javac PiTest.java && java PiTest
$ javac BirthdayTest.java && java BirthdayTest

in the console.

Tests are there for you

Tests should normally be written before you write any code. In this lab, the tests are already written for you, ahead of time, yay! So you get to invoke the tests every time you make little changes to your code. You should write some code, then run the tests. Write more code, then test. If everything goes well, you’ll get to the point where all the tests pass. You will feel great!

But are you done, yet? It depends: take a look over your code and check your indentation, formatting, naming, code quality, etc. Clean up that code! And note: you can clean without fear, because your tests will protect you. Clean some code, then test. Clean more, test.

What to turn in

Online:

On hardcopy or email, answers to the following:

  1. The two questions in the experiments section of the π-estimator activity.
  2. The two questions in the experiments section of the shared birthday activity.
  3. Why did I specify that an exception should be thrown for integers less than 1 (as opposed to less than zero) in the π-estimator activity?
  4. Why did I specify that an exception should be thrown for the number of people less than 2 (as opposed to less than 1) in the birthday activity?
  5. You can compute the exact probability for n people and d days with this cool line in JShell:
    1-IntStream.range(1,n).mapToDouble(i->1-i/(double)d).reduce(1,(x,y)->x*y)
    
    Use this expression is JShell to get the exact probability for 100 people and 365 days? What did you program produce? How close was your program’s estimation to the true value?
  6. Use the expression above in JShell to get the exact probability for n=2 and d=10. What is the output? But wait, that cannot be exact, can it? What’s wrong (Hint: you did you reading for this lab, right?)
  7. Number of hours spent working on this lab.
  8. (Optional) Feel free to let me know what you liked/disliked about this lab, what you learned, etc.
Programming problem selection and experiments designed by Mandy Korpusik