Question: JAVA: Starting with a bag of 49 differently-numbered lottery balls, there are 49 different but equally likely ways of choosing the number of the first
JAVA:
Starting with a bag of 49 differently-numbered lottery balls, there are 49 different but equally likely ways of choosing the number of the first ball selected from the bag, and so there is a 1 in 49 chance of predicting the number correctly. When the draw comes to the second number, there are now only 48 balls left in the bag (because the balls already drawn are not returned to the bag) so there is now a 1 in 48 chance of predicting this number.
Thus for each of the 49 ways of choosing the first number there are 48 different ways of choosing the second. This means that the odds of correctly predicting 2 numbers drawn from the 49 in the correct order are calculated as 49 48. On drawing the third number there are only 47 ways of choosing the number; but of course we could have got to this point in any of 49 48 ways, so the chances of correctly predicting 3 numbers drawn from 49, again in the correct order, is 49 48 47. This continues until the sixth number has been drawn, computed as follows:
Odds of Winning = (49 48 47 46 45 44) / (6 x 5 x 4 x 3 x 2 x 1)
Part 1 / Part 2
Odds of Winning are 13,983,816
Technical Approach:
- Using the JOptionPanes Input box prompt the user for the maximum numbers in the game. For this assignment supply 49.
- Using the JOptionPanes Input box prompt the user for how many numbers will be selected. For this assignment supply 6.
- Write a loop (you pick the loop type: for, while, do while) to compute the first part of the formula (49 48 47 46 45 44):
- Start the loop at the high end of range in this example 49
- Stop the loop when the counter is less than high end of the range - numbers (49 6) to be chosen, in this example 43.
- Put the answer into a primitive data type variable that can store the calculated value.
- Write a loop (you pick the loop type: for, while, do while) to compute second part of the above formula which is the factorial for the number of balls/numbers to be picked:
- In this example 6! Your code should do this: (6 x 5 x 4 x 3 x 2 x 1) and put the result into a primitive data type variable that can support the computed value.
- Compute the chances of winning by dividing the first part by the second part and put into a primitive data type that can store the computed value. See above for the answer.
- Output to the screen the results using Javas JOptionPane objects. showMessageDialog method and format it as a proper number such as: 13,983,816
- Make sure to use Hungarian notation and ample comments. Your code should contain your name, date, Programming Assignment 7 and some general comments as to what the purpose of the code is.
- Make sure you code it as efficiently as possible to maximize your grade. Please note the directions suggest two different loops and Prof. C wrote it in this manner to help explain the logic behind the coding. HOWEVER, to maximize your grade and efficiency try to code the logic with ONE loop.
- Upload just the Java source code file the one that ends in .java
- Retest: Try a 30 number 5 pick game and the results should be: 142,506.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
