Computer Science 523
Advanced Programming
Summer 2014, The College of Saint Rose
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.
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 =
Practice Programs
""
).
""
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.
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, 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
Before 4:30 PM, Tuesday, June 3, 2014, submit your lab for grading. There are four things you need to do to complete the submission: (i) Copy your file with the answers to the lecture assignment and 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. (ii) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab2". (iii) Demonstrate the execution of your programs for your instructor. (iv) Hand a printout of the Java files that make up the programming assignment (not practice programs) to your instructor. (2 business day grace period for demos and printouts).
Grading
This assignment is worth 75 points, which are distributed as follows:
> Feature | Value | Score |
Lecture assignment questions | 10 | |
CircleAreas correctness | 5 | |
LongestWord correctness | 10 | |
LotterySimulation Input parameters (file names and number of drawings) | 3 | |
LotterySimulation Reads one number and bet amount from file | 5 | |
LotterySimulation Reads three numbers and bet amounts from file | 3 | |
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 Reports simulation results at the end | 5 | |
LotterySimulation Simulation output (but not prompts) to file instead of terminal | 4 | |
LotterySimulation comments | 5 | |
LotterySimulation naming conventions | 3 | |
LotterySimulation formatting | 2 | |
Lab questions | 5 | |
Total | 75 | |