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.
Read (and interact with) my notes on Java Basics up to the section on “Arrays.”
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:
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!
This is your first serious Java programming lab, so I’ve helped you get started. At GitHub, select Import Repository. Then:
The import might take a few minutes. When done, clone the repo and get ready to work.
In the file PiEstimator.java:
IllegalArgumentException
with the message At least one dart required
if the number of darts is less than 1. 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):
In the file SharedBirthday.java:
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.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:
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.
Online:
On hardcopy or email, answers to the following:
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?