Write a Java program to simulate a lottery drawing. Your program should allow th
ID: 3690303 • Letter: W
Question
Write a Java program to simulate a lottery drawing. Your program should allow the user to enter values for k, n, and m above. Then, ask the user to enter the numbers that s/he wishes to play (this is your program’s equivalent of “buying” a lottery ticket). Store the k distinct numbers into an array, but keep the bonus number separate.
The program should compute and display the probability of winning the jackpot for the selected game. Then, it should randomly “draw” the winning numbers and determine if the user-entered numbers match. Keep in mind that the matching for the k distinct values should disregard the order of the numbers. The winning numbers should be shown, along with an appropriate message indicating whether or not the user won the jackpot. Allow the user to play as many games as s/he wants.
a player picks k distinct numbers between 1 and n (inclusive), as well as one bonus number between 1 and m (inclusive). “Distinct” means that none of the first k numbers may be repeated. However, the bonus number may or may not coincide with one of the k distinct numbers. The k distinct numbers as well as the bonus number must match the drawn values for the player to win the jackpot.
In this game, a player picks five numbers between 1 and 59, inclusive, and one “Powerball number” between 1 and 35, inclusive. Here, the number of possible lottery tickets is:
For his type of lottery drawing the number of possible tickets is :
59 x58x57x56x55 5x4x3x2x1 x35-175,223,510Explanation / Answer
import java.util.*;
public class Lottery {
//Returns the probability of winning the jackpot in a lottery drawing with the
//specified parameters.
//STILL NEEDED: ALL
public static double jackpotChance(int k, int n, int m)
{
double jackpotChance = 0;
System.out.println(factorial(n));
return jackpotChance;
}
public static double factorial(int n) {
double result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
//Allows the user to enter k distinct integers between 1 and n. Must include
//error checking to ensure that each entered number is within the valid range and
//does not coincide with any previously entered number. Returns an array of
//the entered numbers.
//STILL NEEDED: ERROR CHECKING TO ENSURE USERPICK IS A DISTINCT NUMBER
public static int[] enterNumbers(int k, int n)
{
System.out.println();
Scanner input = new Scanner(System.in);
//asks for user input, takes user input for variables k & n
System.out.println("Enter k.");
k = input.nextInt();
System.out.println("Enter n.");
n = input.nextInt();
final int SIZE = k; //stores k into final variable SIZE
int enterNumbers[]= new int[SIZE]; //declares enterNumbers array
int userPick;
System.out.println("Enter " + k + " distinct integers between 1 and " + n + ".");
int i=SIZE;
for (i = 0; i < enterNumbers.length; i++) {
System.out.println("Enter the next number to store:");
userPick = input.nextInt();
if (userPick >= 1 && userPick <= n) {
enterNumbers[i] = userPick;
} else {
System.out.println("Invalid input, please try again.");
i--;
}
}
System.out.println();
System.out.println("Your chosen numbers are: ");
for (i = 0; i < enterNumbers.length; i++) {
System.out.print(enterNumbers[i] + " ");
}
System.out.println();
System.out.println("Enter m.");
int m = input.nextInt();
jackpotChance(k,n,m);
drawNumbers(k,n);
return enterNumbers;
}
//Draws k distinct random integers between 1 and n. Returns an array of the drawn
// numbers.
//STILL NEEDED: ERROR CHECKING TO ENSURE RANDOMNUMBER IS A DISTINCT NUMBER
public static int[] drawNumbers(int k, int n)
{
int drawNumbers[]= new int[k]; //declares enterNumbers array
int randomNumber;
int i=k;
for (i = 0; i < drawNumbers.length; i++) {
randomNumber = (int) (Math.random()*n + 1);
drawNumbers[i] = randomNumber;
}
System.out.println();
System.out.println("Your drawn numbers are: ");
for (i = 0; i < drawNumbers.length; i++) {
System.out.print(drawNumbers[i] + " ");
}
return drawNumbers;
}
//Returns whether the two arrays a and b contain the same elements, without
//regardtoorder. Forexample,ifa={1,4,3,2}andb={1,2,3,4},
//this method should return true.
// public static boolean containSameElements(int[] enterNumbers, int[] drawNumbers)
// {
//
// }
public static void main (String[] args)
{
System.out.println("Welcome to THE LOTTERY GAME!");
System.out.println("----------------------------");
int k = 0;
int n = 0;
enterNumbers(k,n);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.