winner numbers

advertisement
COSC 1047 –Introduction to Computer Science II
Assignment #4
Due: March 23, 2006
Simulation of lotto 6-49
Write a complete Java program that will simulate Lotto 6-49.
For those of you who don't know the rules of Lotto 6-49, here's a brief overview:
Tickets cost $2 each. For each ticket, customers pick 6 numbers between (and including)
1 and 49. On Saturdays, the lotto organizers pick 6 numbers and 1 bonus number.
On Saturday, October 8, the winning numbers and amount of money won were:
Draw Date
Regular Numbers
Sat Oct 8
5
13
15
17
24
27
Bonus#
Prize Payouts
29
Number
prize
matching
$23,363,006
6
$7359178
5+bonus
$49826
5
$1372
4
$48
3
$10
2+bonus
$5
Write a program that will simulate how much money a million dollars worth of lottery
tickets (that is, 500,000 tickets) would have won on the draw of Saturday, October 8.
The user will enter the 6 winning numbers, plus the bonus number. The program will
simulate the purchase of 500,000 randomly-generated tickets, and output the winnings
earned from these tickets.
Some Details:
Your program will do the following:
1. After outputting a brief welcome message, ask the user to pick the 6 winning
numbers. Also prompt the user to enter a bonus number.
2. Then, use Math.random() to generate a random number between 1 and 49. This
number will represent one of the numbers on a lottery ticket.
3. Compare this random ticket number to the winning numbers. Keep track of any
matches. Generate and compare numbers six times.
4. Calculate how much money the lottery ticket made, using the above table from
Saturday, Oct. 8.
5. Now, repeat the above 4 steps half-a-million times to represent a million dollars
of lottery tickets! Remember to keep a running total of the income.
6. Output the final winnings.
Your program should include (at least) the following methods:
• int[] readNumbers(String header, int count, int low, int high) Create and
return an array of count integers, each one read with a JOptionPane. The numbers
must be in the range low to high (inclusive); if they are not, ask the user to reenter the number. The string header is displayed at the beginning of each input
prompt. Make the method generic; i.e. it should not be specific to this lottery
program. You do not need to check if all the numbers are unique.
• void uniqueRandomArray(int[] array, int low, int high) Fill the existing
array with random values in the range low to high (inclusive). The values must
all be unique; no duplicates are permitted. Each value must be chosen separately,
no values can be more likely to occur than any others.
Start out by asking the user if they would like to choose random winning numbers or
enter their own. If they choose random numbers, call uniqueArray to generate them
(with an array of size 7, for the 6 numbers + bonus). If they choose to enter numbers,
input the winning numbers with a call to readNumbers. In either case, print the winning
numbers using System.out.println. Use uniqueArray to generate the 6 numbers for each
ticket.
For example:
Welcome to the realistic Lotto 6/49 simulator.
Generated winning numbers: 5, 13, 24, 27, 15, 17 bonus: 29
Total winnings with a million dollar investment: $150413
Programmed by 101 Instructors
Date: 2006-Mar-08
*** End of Processing ***
A Few Hints:
 reading values into an array instead of using separate variables for each winning
number.
 Use a boolean variable to keep track of whether or not the ticket won the bonus
number.
 Remember to initialize the variable that will keep track of the winnings outside of
the loop! eg: double totalWinnings = 0.00;
for (int i=0; i<500000; i++)
{ numberOfMatches = 0; gotBonus = false; …
Hand-in: Print and submit your code. Also, submit two sets of output: one using
generated random winning numbers and one using winning numbers you enter yourself.
Download