Need help with assignment
This assignment involves simulating a lottery drawing. In this type of lottery game, the player picks a set of numbers. A random drawing of numbers is then made, and the player wins if his/her chosen numbers match the drawn numbers (disregarding the order of the numbers).
More specifically, 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.
The TN Lottery's Powerball game is a good example of this type of lottery. 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:
{(59 x 58 x 57 x 56 x 55) / (5 x 4 x 3 x 2 x 1)} x 35 = 175,223,510
A single ticket's chance of winning the jackpot is then 1/175,223,510, which is about 0.0000000057. To put that probability in perspective, suppose you were to sell your Harbor Town mansion for $600,000 and use the proceeds to buy Powerball tickets. Since tickets cost $2 each, $600,000 will buy you 300,000 tickets. Even with that many, your chance of winning the jackpot is still a miserably low 0.0017 - less than two tenths of one percent. Makes you think twice about buying that lottery ticket, doesn't it?
In general, for this type of lottery drawing the number of possible tickets is:
[{n x (n - 1) x (n - 2) x ... x(n - k + 1)}/ {k x (k - 1) x (k - 2) x ... x 3 x 2 x 1}] x m
The Assignment
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.
Error Checking
Implement error checking on all user inputs, to the extent we've discussed it in class (don't worry about data type mismatches). Error checking should repeatedly prompt for the input until the user enters a valid value. For example, the user should not be able to enter negative inputs. The user input should also be checked to see that it matches the entered values for n and m - for example, in a game where n = 59, the user should not be able to pick 60 for his/her ticket. Also make sure that you're checking for distinct inputs when the player chooses numbers - a player shouldn't be able to pick the same number more than once (with the exception of the bonus number).
Code Organization
Your program should use methods to break the code down into manageable chunks. At minimum, include the following methods (although you are welcome to add more if you want).
// Returns the probability of winning the jackpot in a lottery
drawing with the
// specified parameters.
public static double jackpotChance(int k, int n, int m)
// 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.
public static int[] enterNumbers(int k, int n)
// Makes the computer draw k distinct random integers between 1
and n. Returns an // array of the drawn numbers.
public static int[] drawNumbers(int k, int n)
// Returns whether the two arrays a and b contain the same
elements, without
// regard to order. For example, if a = {1, 4, 3, 2} and b = {1, 2,
3, 4},
// this method should return true.
public static boolean containSameElements(int[] a, int[] b)
*************************************************SOURCE CODE*******************************************
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/*
* Code written in java
*/
public class LotteryTicket {
public static Scanner sc = new Scanner(System.in);
// enterNumbers(): 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.
public static int[] enterNumbers(int k, int n) {
//first lets declare a set to contain the distinct numbers entered by user
Set<Integer> set = new HashSet<>();
//Loop1: To enter k inputs
System.out.println("Enter k ticket numbers: \n");
for(int i=0; i<k; i++) {
System.out.print("Enter ticket number " + (i+1) + ": ");
//Loop2: To make sure that the user enters a valid number
while(true) {
int num = sc.nextInt();
//Check if the num is within valid range
if(num >=1 && num <= n) {
//check if the number is distinct
if(!set.contains(num)) {
//copy the number to set
set.add(num);
break;
} else {
//number is not distinct
System.out.print("\nThis number already entered before. Enter again: ");
}
}
//else continue Loop2 until user enters a valid number
else {
System.out.print("\nEnter a valid ticket number: ");
}
}
}
//copy the set elements to array
int a[] = new int[k];
int i=0;
for(Integer s : set) {
a[i] = s;
i++;
}
return a;
}
// drawNumbers(): Makes the computer draw k distinct random integers between 1 and n. Returns an
// array of the drawn numbers.
public static int[] drawNumbers(int k, int n) {
ArrayList<Integer> list = new ArrayList<Integer>();
//First, lets add all the valid integers to a temporary arraylist
for (int i=1; i<=n; i++) {
list.add(new Integer(i));
}
//Now, shuffle the contents of list
Collections.shuffle(list);
//Let a[] contains k distinct random integers between 1 and n
int a[] = new int[k];
for (int i=0; i<k; i++) {
a[i] = list.get(i);
}
return a;
}
// containSameElements(): Returns whether the two arrays a and b contain the same elements, without
// regard to order. For example, if a = {1, 4, 3, 2} and b = {1, 2, 3, 4},
// this method should return true.
public static boolean containSameElements(int[] a, int[] b) {
//Lets copy elements of a[] into a set
Set<Integer> set = new HashSet<>();
for(int i=0; i<a.length; i++)
set.add(a[i]);
int j=0;
//Lets iterate through b[]
while (j < b.length) {
//if b[j] is present in the set, a common element is found, return true
if (set.contains(b[j]))
return true;
j++;
}
return false;
}
// jackpotChance(): Returns the probability of winning the jackpot in a lottery drawing with the
// specified parameters.
public static double jackpotChance(int k, int n, int m) {
//prod1 = {n x (n - 1) x (n - 2) x ... x(n - k + 1)}, prod2 = {k x (k - 1) x (k - 2) x ... x 3 x 2 x 1}
double prod1 = 1, prod2 = 1;
//Loop to calculate prod1
for(int i=0; i<k; i++) {
prod1 = prod1*(n-i);
}
//Loop to calculate prod2
for(int i=1; i<=k; i++) {
prod1 = prod1*k;
}
//return the probabality value = {prod1/prod2}*m
return (double)(1/((prod1/prod2)*m));
}
public static void main(String[] args) {
int n, k, m, bonus;
while(true) {
System.out.print("\n To continue, press 1, 0 to exit.");
int input = sc.nextInt();
if(input == 1) {
System.out.print("\nEnter n: ");
n = sc.nextInt();
System.out.print("\nEnter k: ");
k = sc.nextInt();
System.out.print("\nEnter m: ");
m = sc.nextInt();
//Take k distinct integers from user
int userNumbers[] = enterNumbers(k, n);
System.out.println("\n User input number are : ");
for(int i=0;i <k; i++) {
System.out.print(userNumbers[i] + " ");
}
System.out.print("\n Enter the bonus number : ");
while(true) {
int num = sc.nextInt();
if(num >=1 && num <= m) {
bonus = num;
break;
} else {
System.out.print("Please enter the bonus number between 1 & " + m + ": ");
}
}
System.out.println("\nProbability of winning = " + jackpotChance(k, n, m));
//draw k distinct random numbers
int winningNumbers[] = drawNumbers(k, n);
System.out.println("\n Winning number are : ");
for(int i=0;i <k; i++) {
System.out.print(winningNumbers[i] + " ");
}
if(containSameElements(userNumbers, winningNumbers)) {
System.out.println("\nCongratulations, you have won!!");
} else {
System.out.println("\nSorry, you have lost!!");
}
} else if(input == 2) {
return;
} else {
System.out.println("\nPlese presse a valid button");
}
}
}
}
************************************************OUTPUT**********************************************
Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery...
4.26 In a lottery game, three winning numbers are chosen uniformly at random from (1, ,100), sampling without replacement. Lottery tickets cost $1 and allow a player to pick three numbers. If a player matches the three winning numbers they win the jackpot prize of $1,000. For matching exactly two numbers, they win $15. For matching exactly one number they win $3 d) Hoppe shows that the probability that a single parlayed ticket will ulti- mately win the jackpot is...
c++ please help You are going to create a PowerBall Lottery game with functions. First, you are going to generate 5 UNIQUE numbers between 1 and 69. Then you are going to generate the powerball number, which is a number between 1 and 26. Next you are going to ask the user for 5 numbers. Users should only be allowed to enter numbers in the range 1-69 for regular numbers, 1-26 for powerball pick. You will compare each of the...
The Powerball lottery is played twice each week in 44 states, the District of Columbia, and the Virgin Islands. To play Powerball, a participant must purchase a $2 ticket, select five numbers from the digits 1 through 69, and then select a Powerball number from the digits 1 through 26. To determine the winning numbers for each game, lottery officials draw 5 white balls out a drum of 69 white balls numbered 1 through 69 and 1 red ball out...
A certain lottery game asks a person to choose five numbers between 1 and 50 (nonrepeating) and then one "powerball" number between 1 and 30. In order to win the jackpot, all 6 numbers must match. (a) What is the probability of winning the jackpot? (b) To win the second grand prize, the person must match only 3 of the 5 numbers and cannot match the powerball number. What is the probability of winning the second prize?
Please help to solve question 1 and subsections 1-4 1: Powerball Consider the multi-state lottery Powerball game. Each ticket is $2 and allows a player to select 5 white balls from 1 to 69 (without replacement), and 1 Red Powerball, from 1 to 26. The order of the five white balls does not matter when evaluating a win. If there are 64 losing whiteball numbers, how many ways can the winner pick 4 of them. If the player is only...
In one lottery, a player wins the jackpot by matching all five numbers drawn from white balls (1 through 45) and matching the number on the gold ball (1 through 32). If one ticket is purchased, what is the probability of winning the jackpot? The probability of winning the jackpot with one ticket is (Type an integer or a simplified fraction)
In a lottery game, the jackpot is won by selecting five different whole numbers from 1 through 38 and getting the same five numbers (in any order) that are later drawn. In the Pick 4 game, you win a straight bet by selecting four digits (with repetition allowed), each one from 0 to 9, and getting the same four digits in the exact order they are later drawn. The Pick 4 game returns $5 comma 000 for a winning $1...
a. Calculate the number of possible lottery tickets if the player must choose 8 numbers from a collection of 43 numbers (1 through 43), where the order does not matter, The winner must match all 8 b. Calculate the number of lottery tickets if the player must choose 7 numbers from a collection of 62 numbers (1 through 62), where the order does not matier. The winner must match all 7. c. In which lottery does the player have a...
In the lottery game Fantasy 5 you have to select 5 numbers from the numbers {1,2,3,.......,38,39}. The jackpot today is $76,000. Each ticket costs $1. In this lottery each number must be different and the order of the numbers does not matter. a.) What is the probability of having a winning ticket? b.) What is the expected value of this game? c.) A fair game has an expected value of 0. How large does the jackpot have to be for...
Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements: The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...