Write a Lottery class that simulates a 6-number lottery (e.g. \"Lotto\"). The cl
ID: 3775953 • Letter: W
Question
Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an array of six integers named lotteryNumbers, and another array of six integers named userLotteryPicks. The class' constructor should use the Random class to generate a unique random number in the range of 1 to 60 for each of the 6 elements in the lotteryNumbers array. Thus, there should be a loop that keeps generating random numbers until all 6 numbers are unique. The Lottery class should have a method, getUsersPicks(), which prompts the user to enter 6 unique numbers between 1 - 60, which will be stored in the array of integers named usersLotteryPicks. Thus, there should be validation to ensure that each number the user selects is different from all the other numbers that he/she entered previously, and to keep looping until the user enters 6 unique numbers. Finally, the Lottery class should have a method, checkLotteryMatch(), which will compare the 2 arrays - lotteryNumbers & usersLotteryPicks - and return the number of digits that match. The digits do not have to match in the exact order, so a nested loop should be created that goes through each digit in the user's lottery picks array and compares it to each digit in the lotteryNumbers array, counting each of the matches. The checkLotteryMatch() method will return an int containing the number of matches.
Write a Driver class called the LotteryGame, which instantiates the Lottery class. Once a Lottery object has been created, the driver will call the Lottery object's getUsersPicks() method, followed by the checkLotteryMatch() method. Use the results of the checkLotteryMatch() method to determine if the user is a winner by using the following criteria:
For a 3-digit match, display a message to the user that he/she will receive a free Lottery ticket as the prize
For a 4-digit match, display a message to the user that he/she will receive a $2000 prize
For a 5-digit match, display a message to the user that he/she will receive a prize of $50,000.
For a 6-digit match, display a message to the user that he/she will receive a grand prize of $1,000,000.
If there are no matches, display the following message to the user: "Sorry, no matches today. Try again!"
Explanation / Answer
import java.util.Random; //For random number creation
import java.util.Scanner; //For scanner class to accept data
//Class Lottery
class Lottery
{
//Array declaration
int lotteryNumbers[];
int userLotteryPicks[];
Lottery()
{
//Array creation
lotteryNumbers = new int[6];
userLotteryPicks = new int[6];
//Random number object created
Random r = new Random();
//Sets the lower and upper bound
int Low = 1;
int High = 60;
//Generates the random number
int i, Result, f = 0;
//Initializes the array elements to zero
for(i = 0; i < 6; i++)
{
lotteryNumbers[i] = 0;
userLotteryPicks[i] = 0;
}
i = 0;
//Generates unique random number
do
{
Result = r.nextInt(High-Low) + Low;
//Checks for the duplicate
for(int t = 0; t < 6; t++)
{
if(lotteryNumbers[t] == Result)
f = 1;
}
//If no duplicate then store the number and increase the counter
if(f == 0)
{
lotteryNumbers[i] = Result;
i++;
}
else
f = 0;
}while(i < 6);
}
//Accepts 6 unique numbers from the user
void getUsersPicks()
{
//Scanner calss object created to accept numbers
Scanner sc = new Scanner(System.in);
int i = 0, f = 0, no;
System.out.println(" Enter 6 Unique numbers: ");
//Loops till 6 unique numbers
do
{
System.out.println("Enter a number: ");
no = sc.nextInt();
//Checks for the duplicate number
for(int t = 0; t < 6; t++)
{
if(userLotteryPicks[t] == no)
f = 1;
}
//If not a duplicate number then store the number increase the counter
if(f == 0)
{
userLotteryPicks[i] = no;
i++;
}
//If duplicate displays the error message
else
{
System.out.println(" Duplicate Number Entered: ");
f = 0;
}
}while(i < 6);
}
//Matches the lotteryNumbers with userLotteryPicks and counts number of match
int checkLotteryMatch()
{
int match = 0, r, c;
//Checks for the matching
for(r = 0; r < 6; r++)
{
for(c = 0; c < 6; c++)
{
if(lotteryNumbers[r] == userLotteryPicks[c])
match++; //Counts number of matching
}
}
return match;
}
}
//Driver class
public class LotteryGame
{
public static void main(String ss[])
{
//Creates object for the Lottery class
Lottery lg = new Lottery();
int res;
//Displays the unique random generated
System.out.println("Random Numbers: ");
for(int t = 0; t < 6; t++)
System.out.print(" " + lg.lotteryNumbers[t]);
lg.getUsersPicks();
res = lg.checkLotteryMatch();
//Checks the number of match and displays the appropriage message
if(res == 3)
System.out.println("he/she will receive a free Lottery ticket as the prize");
else if(res == 4)
System.out.println("he/she will receive a $2000 prize");
else if(res == 5)
System.out.println("he/she will receive a prize of $50,000");
else if(res == 6)
System.out.println("he/she will receive a grand prize of $1,000,000");
else if(res == 0)
System.out.println("Sorry, no matches today. Try again!");
else
System.out.println(" Less than 3 match");
}
}
Output 1:
Random Numbers:
50 53 19 35 1 46
Enter 6 Unique numbers:
Enter a number:
10
Enter a number:
53
Enter a number:
50
Enter a number:
10
Duplicate Number Entered:
Enter a number:
1
Enter a number:
46
Enter a number:
33
he/she will receive a $2000 prize
Output 2:
Random Numbers:
15 58 20 34 57 3
Enter 6 Unique numbers:
Enter a number:
15
Enter a number:
58
Enter a number:
3
Enter a number:
57
Enter a number:
20
Enter a number:
34
he/she will receive a grand prize of $1,000,000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.