You may use the console (Scanner) or dialog box method. Write a Lottery class th
ID: 3660056 • Letter: Y
Question
You may use the console (Scanner) or dialog box method. Write a Lottery class that simulates a lottery. The class should have an array of 5 integers named lotteryNumbers with the following characteristics: 1. the constructor should use the Random class (from the Java API) to generate a random number in the range of 1 to 50 for each element in the array 2. The class should have a method that accepts an array of five integers that represent a person's lottery picks. They method is to compare the corresponding elements in the two arrays and return the number of digits that match. if all 5 numbers match, the method should display a " YOU WIN!" message. (Hint: use either a selection sort before comparing arrays or use a nested loop) 3. 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 5 numbers. The program should then display the computer generated lottery numbers and then the number of digits that match. -- If anyone could help me with this. I would very much appreciate it!Explanation / Answer
/*100% working 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 :");
for (int i = 0; i < lnum.getLotteryNumbers().length; i++) {
System.out.print(lnum.getLotteryNumbers()[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.