Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA- Please include comments Write a Lottery class that simulates a 6-number lo

ID: 3704570 • Letter: J

Question

JAVA- Please include comments

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. The method should first display the lottery numbers generated in the constructor as a "cheat" to more easily be able to test the logic of matching the numbers. 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!"

Create a method called checkDuplicates which receives any array of ints, and an integer number. The method will return a true or false, depending on whether the integer number passed to the method already exists in the array of ints (also passed to the method).

Call the checkDuplicates method from both the constructor of the Lotto class and the getUserPicks() method also of the Lotto class. This will prevent you from writing duplicate code in both of these places.

Explanation / Answer

Source Code:-
------------------------
package com.samples;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Lottery
{
    int userLotteryPicks[];
    int lotteryNumbers[];
    int size;
    public Lottery(int size)
    {
        Random rand = new Random();
        this.size=size;
        userLotteryPicks=new int[size];
        lotteryNumbers=new int[size];
        for(int i=0;i<size;i++)
        {
            int number = rand.nextInt(60);
            lotteryNumbers[i]=number;
        }
        for(int i=0;i<size;i++)
        {
            System.out.print(lotteryNumbers[i]+" ");
        }
    }
    public void getUsersPicks()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println(" Please Enter "+size+" Numbers");
        for(int i=0;i<size;i++)
        {
            userLotteryPicks[i]=sc.nextInt();
        }
        sc.close();
    }
    public int checkLotteryMatch()
    {
        int count=0;
        //Arrays.sort(userLotteryPicks,0,userLotteryPicks.length);   
        //Arrays.sort(lotteryNumbers,0,lotteryNumbers.length);
        for(int i=0;i<userLotteryPicks.length;i++)
        {
            for(int j=i+1;j<lotteryNumbers.length;j++)
            {
                if(userLotteryPicks[i]==lotteryNumbers[j])
                {
                    count++;
                }
            }
        }
        return count;
    }
    public static void main(String[] args)
    {
        System.out.println("The Random Numbers are");
        Lottery obj=new Lottery(6);
        obj.getUsersPicks();
        int matches=0;
        matches=obj.checkLotteryMatch();
        if(matches==0)
        {
            System.out.println("Sorry, no matches today. Try again!");
        }
        else if(matches==3)
        {
            System.out.println("Congratualations receive a free Lottery Ticket");
        }
        else if(matches==4)
        {
            System.out.println("Congratualations will receive a $2000 prize");
        }
        else if(matches==5)
        {
            System.out.println("Congratualations,will receive a $50,000 prize");
        }
        else if(matches==6)
        {
            System.out.println("Congratualations,will receive a $1,000,000 Prize");
        }
       
    }

}


Sample output:-
---------------------------
The Random Numbers are
32    46    58    1    49    25   
Please Enter 6 Numbers
34
90
31
56
12
8
Sorry, no matches today. Try again!

The Random Numbers are
0    58    16    59    57    59   
Please Enter 6 Numbers
59
12
59
23
90
1
Congratualations will receive a $2000 prize