Computer Science 210
Data Structures

Fall 2016, Siena College

Lab 2: Lottery Simulator
Due: 10:00 AM, Wednesday, September 21, 2016

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 to answer and practice programs to write.

For the programs here, you may use arrays, but not ArrayLists or any other advanced data structure (yet).

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

Practice Programs

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 if you're not sure how to tackle the whole problem. This is often how even expert programmers attack a larger problem: incrementally. You can (and should) stop and test your program after each step. This practice program is worth 10 points.

Here is one possible way to approach this problem incrementally.

  1. Read in one word.
  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.

Here is a sample run of my solution code:

Please enter the next word ("done" to end): this
Please enter the next word ("done" to end): lab
Please enter the next word ("done" to end): is
Please enter the next word ("done" to end): so
Please enter the next word ("done" to end): much
Please enter the next word ("done" to end): fun
Please enter the next word ("done" to end): done
You entered 6 words.
The longest word(s): this, much

Practice Program: Write the program to play the Silver Dollar Game at the end of Chapter 3 in Bailey. Write your program in a file named CoinStrip.java. Include a main method in this class to play the game. More details and suggestions below. This practice program is worth 15 points.

Here is a sample run of my solution:

Current configuration: -0-1-2--3
Specify coin number and move distance.
2 1
Moving coin 2 by 1
Current configuration: -0-12---3
Specify coin number and move distance.
1 3
Invalid move!
Current configuration: -0-12---3
Specify coin number and move distance.
6 2
Invalid move!
Current configuration: -0-12---3
Specify coin number and move distance.
3 3
Moving coin 3 by 3
Current configuration: -0-123
Specify coin number and move distance.
1 1
Moving coin 1 by 1
Current configuration: -01-23
Specify coin number and move distance.
0 1
Moving coin 0 by 1
Current configuration: 0-1-23
Specify coin number and move distance.
2 1
Moving coin 2 by 1
Current configuration: 0-12-3
Specify coin number and move distance.
1 1
Moving coin 1 by 1
Current configuration: 01-2-3
Specify coin number and move distance.
2 1
Moving coin 2 by 1
Current configuration: 012--3
Specify coin number and move distance.
3 1
Moving coin 3 by 1
Current configuration: 012-3
Specify coin number and move distance.
3 1
Moving coin 3 by 1
Game over.  Final configuration: 0123

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 elementary school math. In reality, if you play this game long enough, you should expect to 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 2: 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.. (2 points)

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

Adding File I/O

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, extend your program so it reads an arbitrary number of numbers to bet on and amounts of those bets. Specify this in your input file by having the number of bets you wish to place on the first line. Let's call that "numBets". The file should then have numBets additional lines, with the number to bet on and a bet amount on each line. For each drawing, all numBets bets are placed and processed. (Hint: construct and populate a pair of arrays for the numbers on which the player is betting and the amount being bet on each number.)

For reference, here is a sample input file that is used by my solution code to generate (for example) this sample output file. And here is the keyboard/terminal interaction for my code to do this:

Please enter the name of the file with your lottery numbers and bets: sampleinput.txt
How many drawings would you like to simulate? 50
Please enter a file name for simulation results: sampleoutput.txt

Submitting

Before 10:00 AM, Wednesday, September 21, 2016, submit your lab for grading. There are two things to do to complete the submission: (i) Copy your file with the answers to the lab questions into your project directory. Be sure to use the correct file name. If you prepared your answers in Word, export to a PDF file and submit that. and (ii) upload a copy of your lab (a .7z or .zip file containing your project directory) to Blackboard under "Lab2". Don't forget to check your programming assignment programs for compliance with the Style Guide for CSIS 210 Programs

Grading

This assignment is worth 80 points, which are distributed as follows:

> FeatureValueScore
Lab Question 1 2
Lab Question 2 2
Lab Question 3 2
LongestWord correctness 10
CoinStrip correctness 15
LotterySimulation Input parameters (file names and number of drawings) 2
LotterySimulation Reads one number and bet amount from file 4
LotterySimulation Main loop (correct number of drawings) 3
LotterySimulation Random drawings 3
LotterySimulation Correctly reports bet amount and number for each drawing 4
LotterySimulation Correctly detects and reports win/loss on a drawing 5
LotterySimulation Reads multiple bets in file (store in array) 6
LotterySimulation Handles multiple bets in simulation (from array) 6
LotterySimulation Reports simulation results at the end 4
LotterySimulation Simulation output (but not prompts) to file instead of terminal 2
LotterySimulation comments 5
LotterySimulation naming conventions 3
LotterySimulation formatting 2
Total 80