Summer 2014, The College of Saint Rose

Lab 2: Lottery Simulator
Due: 4:30 PM, Tuesday, June 3, 2014

For this lab, your main job is to write a Java program to simulate a series of lottery drawings, keeping track of the amount spent on bets and any winnings. Then a report is made at the end indicating how much the gambler has won or lost during the simulation.

There are also some questions and practice programs to write.

You may work alone or with a partner on this lab. Only one submission per group is needed.

Getting Set Up

To get your BlueJ environment set up for this week's lab assignment, start BlueJ and choose "New Project" from the "Project" menu. Navigate to your folder for this course and choose the name "Lab2" (no spaces) for the project.

Create a document where you will record your answers to the lecture assignment and lab questions. If you use plain text, call it "lab2.txt". If it's a Word document, you can call it whatever you'd like, but when you submit, be sure you convert it to a PDF document "lab2.pdf" before you submit it.

Lecture Assignment Questions

We will usually discuss these questions at the start of class on the lab due date, so no credit can be earned for late submissions of lecture assignment questions.

LA Question 1: Any for loop can be rewritten as a while loop. Rewrite the loop in the Countdown example as a while loop. (1 point)

LA Question 2: How would you convert the Shout program to Whisper, which should take the contents of the file and force all characters to be lower case? (2 points)

LA Question 3: Assume you have a Java program that includes the following construction of a random number generator object:

Random r = new Random();

Complete the following Java statements so that they accomplish the task as described in the preceding comment. (1/2 point each)

```// x should contain an integer between 0 and 999
int x =

// y should contain an integer between 1 and 50
int y =

// z should contain an integer between -10 and 10
int z =

// a should contain a floating-point value between 0.0 and 2.0
double a =

// b should contain a floating-point value between 1.0 and 2.0
double b =

// c should contain a floating-point value between 10.0 and 50.0
double c =
```

LA Question 4: Write a method that takes two numbers are parameters that represent the width and height of a rectangle, and returns the area of the rectangle. (2 points)

LA Question 5: Write a method that takes a temperature measured in Fahrenheit as its parameter and returns the Celsius equivalent. (2 points)

Practice Programs

Practice Program: Augment the CircleAreas example program to print the circumference of each circle (2 * pi * radius) in addition to the area. Your program should print the circumferences to a second file. So you will need to read in another file name from the keyboard Scanner, need to create another PrintWriter for the second file, add println statements to print the circumference information to that second file, and close the second PrintWriter. (5 points)

Practice Program: Write a program LongestWord.java that reads in a series of words from the keyboard until an empty string is encountered, at which point it reports the longest word(s) that were presented on the input. It is essential to develop and test a program such as this incrementally. Below is a list of steps you can follow. You can (and should) stop and test your program after each step. This practice program is worth 10 points.

2. Read in words inside a loop (forever).
3. Read in words until someone enters the empty string (`""`).
4. Add a variable that keeps track of the number of words read in. Initialize it before your main loop, increment it in an appropriate place inside your main loop, and print it out after the loop.
5. Add a variable to keep track of the longest word. Update it in your main loop any time someone enters a word longer than the longest seen previously. Print it out at the end. Don't worry yet about handling ties.
6. Now worry about that case. When you detect a tie for the longest word, you should append the new word to the string containing the longest word(s). Note that we will now need to have a separate variable to remember the length of the longest word, since the string containing the longest word(s) may now contain multiple words.
7. Add a check at the end to make sure you only print the longest word(s) if at least one word was entered before `""` was entered. If not, print a separate message to that effect.

Programming Assignment: The Lottery "Numbers" Game

The lottery game we will be simulating is the New York Lottery's "Numbers Game". In this game, a bettor wagers on which random number between 000 and 999 will be selected that day. The payoff is 500:1, i.e., for a \$1 bet, a match will result in a \$500 payout. A \$2 bet will result in a \$1,000 payout, etc.. This sounds great to anyone who never passed 6th grade math. In reality, if you play this game long enough, you will lose half of your money.

Simple Simulation

Your first task is to develop a simple Java program, which you should call LotterySimulation.java, that simulates a specified number of lottery drawings, given the number to bet on and the amount of a bet. Each of these three are read in from the keyboard.

For example, if the number of drawings to simulate is 10, and the number to bet on is 284, betting \$2 per drawing, a series of 10 random numbers in the 0-999 range should be drawn. For each number drawn, we keep track of the fact that another \$2 has been bet. If the number came up as 284, we also record that the winnings for that drawing are \$1,000, and that amount is added to the accumulated winnings. In the end, report the total amount that was bet and the total winnings, followed by a statement that reports either the net amount won, the net amount lost, or that the bettor broke even.

Question 1: For the above example input, what are all of the possible values for total amount bet, total winnings, and net winnings/losses? Hint: consider what would happen if the player wins 0 times, 1 time, 2 times, etc.. (3 points)

Question 2: Run your program for the above example input and report your results. (2 points)

Once that it working, your next task is to get some of your input (the number to bet on and the amount of each bet) from a file, and to write all of the program's output (other than Terminal prompts) to a file.

Your program should prompt (at the Terminal) for the name of the input file that contains the number to bet and the amount of each bet, for the name of the file that should contain the simulation output, and for the number of drawings to simulate.

You will need to create your plain text input file in the same folder as your BlueJ project. In Windows, you can use Notepad. On a Mac, TextEdit is a good choice. The file should consist of only the 2 numbers: the number to bet on and the amount of the best, on a single line, separated by a space.

Processing Multiple Bets Per Drawing

Finally, for just the last few points, extend your program so it reads 3 numbers to bet on and amounts of those bets. Place these on 3 lines, a number and a bet amount on each line. For each drawing, all 3 bets are placed and processed.

Note: given the Java constructs we have been using so far, the best way to accomplish this is to introduce extra variables to store the additional numbers and bet amounts, and to add code inside your main loop that checks for a possible win for each of the three numbers rather than just the one.

Submitting