import java.util.Random; import java.util.Scanner; class Lottery { private int l
ID: 3621770 • Letter: I
Question
import java.util.Random;import java.util.Scanner;
class Lottery
{
private int lotteryNumbers[];
public Lottery()
{
Random rand = new Random(System.currentTimeMillis());
lotteryNumbers = new int[5];
for (int i = 0;i < lotteryNumbers.length;i++)
{
lotteryNumbers[i] = Math.abs(rand.nextInt())%10;
}
}
public int compareNumbers(int[]usersNumbers)
{
int match = 0;
if (usersNumbers.length==lotteryNumbers.length)
{
for(int i =0;i<lotteryNumbers.length;i++)
{
if(usersNumbers[i]==lotteryNumbers[i])
{
match++;
}
}
}
return match;
}
}
public class LotteryApp
{
public static void main(String[] args)
{
Lottery lottery = new Lottery();
System.out.println("Lottery Application ");
System.out.println("There are 5 secret numbers in range of 0 through 9. Try to guess them. ");
Scanner kb = new Scanner(System.in);
int numbers[] = new int[5];
System.out.print("Enter numbers");
for (int i = 0; i<numbers.length;i++)
{
numbers[i] = kb.nextInt();
}
int match = lottery.compareNumbers(numbers);
if (match == 5)
{
System.out.println("You are the GRAND WINNER!!");
}
else
{
System.out.println("The number of digits matched: "+match);
}
}
}
Explanation / Answer
Dear, The program inputs an array of five number(user Guesses) and then compares it with randomly generated array Here is the code with commented wht operation is done //Header file section import java.util.Random; import java.util.Scanner; class Lottery { //data menber private int lotteryNumbers[]; //constructor public Lottery() { //instantiating random class Random rand = new Random(System.currentTimeMillis()); lotteryNumbers = new int[5]; //selects numbers from Random class between 1 to 9 and initializes to //lotteryNumbers array for (int i = 0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.