Write a lottery class that simulates a lottery. The class should have an array o
ID: 3660197 • Letter: W
Question
Write a lottery class that simulates a lottery. The class should have an array of five integers named lotteryNumbers. The constructor should use the Random class (from the Java API) to generate a random number in the range of 0 through 9 for each element in the array. The class should also have a method that accepts an array of five integers that represent a person's lottery picks. The method is to compare the corresponding elements in the two arrays and return the number of the digits that match. For example, the following shows the lotteryNumbers array and the user's array with sample numbers stored in each. There are two matching digits (elements 2 and 4). lottery array: |7|4|9|1|3| user array: |4|2|9|7|3| In addition, the class should have a method that returns a copy of the lotteryNumbers array. Demonstrate the class in a program that asks the user to enter five numbers. The programshould display the number of digits that match the randomly generated lottery numbers. If all of the digits match, display a message proclaiming the user a grand prize winner.Explanation / Answer
/*100% running code and fit to the question, take a look on output*/
Output:-
Enter the five digit lottery number
Enter the digit 1 : 23
Enter the digit 2 : 44
Enter the digit 3 : 43
Enter the digit 4 : 66
Enter the digit 5 : 33
YOU LOSS!!
Computer Generated Lottery Number :
|12|38|47|48|49|
Lottery Number Of user:
|23|33|43|44|66|
Number Of digit matched: 0
Code:-
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Lottery {
int[] lotteryNumbers = new int[5];
public int[] getLotteryNumbers() {
return lotteryNumbers;
}
Lottery() {
Random randomVal = new Random();
for (int i = 0; i < lotteryNumbers.length; i++) {
lotteryNumbers[i] = randomVal.nextInt((50 - 1) + 1);
}
}
int compare(int[] personLottery) {
int count = 0;
Arrays.sort(lotteryNumbers);
Arrays.sort(personLottery);
for (int i = 0; i < lotteryNumbers.length; i++) {
if (lotteryNumbers[i] == personLottery[i]) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] personLotteryNum = new int[5];
int matchNum;
Lottery lnum = new Lottery();
Scanner input = new Scanner(System.in);
System.out.println("Enten the five digit lottery number");
for (int i = 0; i < personLotteryNum.length; i++) {
System.out.println("Enter the digit " + (i + 1) + " :");
personLotteryNum[i] = input.nextInt();
}
matchNum = lnum.compare(personLotteryNum);
if (matchNum == 5)
System.out.println("YOU WIN!!");
else
System.out.println("YOU LOSS!!");
System.out.println("Computer Generated Lottery Number :");
System.out.print("|");
for (int i = 0; i < lnum.getLotteryNumbers().length; i++) {
System.out.print(lnum.getLotteryNumbers()[i] + "|");
}
System.out.println(" Lottery Number Of user:");
System.out.print("|");
for (int i = 0; i < personLotteryNum.length; i++) {
System.out.print(personLotteryNum[i] + "|");
}
System.out.println();
System.out.println("Number Of digit matched: " + matchNum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.