Exercise 2: Write a Lottery class that simulates a lottery. The class has the fo
ID: 642362 • Letter: E
Question
Exercise 2: Write a Lottery class that simulates a lottery. The class has the following attributes: ? lottery Numbers: array of five integers ? user Array: array of five integers ? matches: number of matches and the following methods: ? Constructor: takes in no arguments. It should use the Random class to generate five random numbers in the range of I through 9 and puts them in the lottery Numbers array Note: (int)(Math.randotn()*9+I) generates random numbers in the range 1 through 9. . Method that accepts an array of five integers that represent a person?s lottery picks. Method should compare the corresponding elements in the two arrays and finds the number of matches. For example, the following shows the lottery Numbers array and the user array with sample numbers stored in each. There are two matching digits (numbers 9 and 3). Note that the digits should be in the same position as well. Lottery Numbers array: 7 4 9 1 3 user array: 4 2 9 7 3 . Method print Prize that prints the lottery Numbers array, the user array and the prize. The prize is determined according to the following rules: Grand Prize (SI million): all five digits match $500,000 prize: four out of five match $1000 prize: three out of five match $10 prize: two out of five match $2 prize: one out of five match Sony, nothing for you none out of five match Demonstrate the class in a program that asks the user to enter live numbers. The program should display the lottery Numbers array and the user?s array and the number of digits that match. It should also display a message about the prize. Note: With random numbers, it is really hard to win even a $10 prize.Explanation / Answer
import java.util.Scanner;
public class lottery {
int[] lotteryNumbers=new int[5];
int[] userArray= new int[5];
int matches=0;
public lottery()
{
for(int i=0;i<lotteryNumbers.length;i++)
{
lotteryNumbers[i]=(int)(Math.random()*9+1);
}
}
public int[] ticketno()
{
Scanner scan=new Scanner(System.in);
System.out.println("Please enter your lottery number:");
for(int i=0;i<userArray.length;i++)
{
userArray[i]=scan.nextInt();
}
return userArray;
}
public void printPrize(int user[], int lott[])
{
for(int i=0;i<=5;i++)
{
user[i]=userArray[i];
lott[i]=lotteryNumbers[i];
System.out.print(lotteryNumbers[i]);
System.out.print(userArray[i]);
}
for(int i=0;i<=5;i++)
{
if(lotteryNumbers[i]==userArray[i])
{ matches=matches+1;
}
}
if(matches==5)
{
System.out.println("Congratulations!. You won a grand prize of $1 million");
}
else if(matches==4)
{
System.out.println("Congratulations!. You won a prize of $500,000");
}
else if(matches==3)
{
System.out.println("Congratulations!. You won a prize of $1000");
}
else if(matches==2)
{
System.out.println("Congratulations!. You won a prize of $10");
}
else if(matches==1)
{
System.out.println("Congratulations!. You won a prize of $2 ");
}
else
{
System.out.println("Sorry, nothing fpr you!");
}
}
public static void main(String[] args) {
shortnum sn=new shortnum();
sn.ticketno();
sn.printPrize(sn.userArray[], sn.lotteryNumbers[]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.