Computer Science 210
Data Structures

Fall 2017, Siena College

Programming Project 1: Lottery Simulator
Due: 11:59 PM, Friday, September 22, 2017

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 structures (yet). You may write methods in addition to the required main method for each program, if you wish.

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

Getting Set Up

You will receive an email with the link to follow to set up your GitHub repository lottery-project-yourgitname for this Programming Project. One member of the group should follow the link to set up the repository on GitHub, then that person should email the instructor with the other group members' GitHub usernames so they can be granted access. This will allow all members of the group to clone the repository and commit and push changes to the origin on GitHub. At least one group member should make a clone of the repository to begin work.

In your git bash window, after you clone and cd to the repository, you can type the command

touch package.bluej

which will create the little BlueJ icon for you to click on to launch BlueJ.

Please answer lab questions in the README.md file in your group's GitHub repository.

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 ("") or "done".
  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 "" or "done" 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 a smaller version of the New York Lottery's "Numbers Game". In this game, a bettor wagers on which random number between 00 and 99 will be selected that day. (In the real Numbers Game, the range is 000-999.) The payoff is 50:1, i.e., for a $1 bet, a match will result in a $50 payout. A $2 bet will result in a $100 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 34, betting $2 per drawing, a series of 10 random numbers in the 0-99 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 34, we also record that the winnings for that drawing are $100, 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, the starter repository includes a sample input file sampleinput.txt that is used by my solution code to generate (for example) the sample output file in sampleoutput.txt. 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

Your submission requires that all required deliverables are committed and pushed to the master for your repository's origin on GitHub. That's it! If you see everything you intend to submit when you visit your repository's page on GitHub, you're set.

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