Question

Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an...

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!"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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("\n 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("\n 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: \n");
       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("\n 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

Add a comment
Know the answer?
Add Answer to:
Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help with this, and leave comments explainging the code you wrote, thank you for your...

    Please help with this, and leave comments explainging the code you wrote, thank you for your help 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...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    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...

  • COP2221 - Intermediate C++ Programming Module #6 Assignment One 20 points This assignment refers to Learning...

    COP2221 - Intermediate C++ Programming Module #6 Assignment One 20 points This assignment refers to Learning Outcome #2: Create and utilize arrays to store lists of related data Programming Problem You have been hired to create a C++ program to simulate the lottery. Your program allows users to see how often their lottery number choices match a randomly generated set of numbers used to pick lottery winners. The company, "How Lucky Are You?, Inc." wants a modular, professional and user-friendly...

  • java/javafx assignment: Island Paradise just started running their city lotto. You've been tasked with writing a...

    java/javafx assignment: Island Paradise just started running their city lotto. You've been tasked with writing a program for them. The program is going to allow to user to first select their lotto numbers. The program will then randomly generate the winning lotto numbers for the week and then check the winning numbers against the random ticket the user played in the Lotto to see how many numbers the user guessed correctly. The rules for lotto work as follows: Select 7...

  • Write a class named DartSector. The constructor should accept a sector number and position {Single, Double,...

    Write a class named DartSector. The constructor should accept a sector number and position {Single, Double, Treble, Outer & Inner} represented with the integers 1 – 5 respectively. The class should have 3 methods: • getSectorColor – method should return the pocket’s color (as a String) • singleThrow – method should generate 2 random numbers; one in the range (1..20) and another (1..5); and return the score for the throw. • throwThree – method should generate 3 sets of random...

  • Using the Random, write a simulation that will "play" the California Super LOTTO. Your program logic will allow the player to: 1) Pick any 6 integer numbers from 1 - 70. Your logic should prev...

    Using the Random, write a simulation that will "play" the California Super LOTTO. Your program logic will allow the player to: 1) Pick any 6 integer numbers from 1 - 70. Your logic should prevent the player from selecting a value LESS than 1 or greater than 70. The logic should prevent the player from selecting the SAME number (i.e. they cannot have two or more of their selections be the SAME number). 2) Display the players' 6 number selections...

  • Write a Java program that determines if a person hits the "Pick 3" loterry numbers. Have...

    Write a Java program that determines if a person hits the "Pick 3" loterry numbers. Have the program to randomly create a 3 digit lottery number. Have the user enter the number they will be playing and the results: (DO NOT USE ARRAYS or STRINGS) -If the user input matches the lottery number in the exact order, the award is $5Million. -If all the digits in the user input match all the digits in the lottery number, the award is...

  • Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...

    Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions) Functions you need to implement: Define function initialize() that takes array lotterNumbers[] as a parameter and assigns...

  • Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT