Write a program that simulates the Powerball lottery. Create a PowerBall class t
ID: 651710 • Letter: W
Question
Write a program that simulates the Powerball lottery. Create a PowerBall class that contains: - A field for an int array of size 6. - A constructor that initializes this array with 6 random values (0-9). - A method called checkMatch that takes as an argument an int array and returns how many numbers match the class's int array. To match, the same numbers must be in the same position. Write a main class which asks to user to enter 6 numbers as their lottery ticket and store it in an array. Then create a PowerBall object and give the user's ticket to the checkMatch method. Then figure out the amount won based on the return value. The winnings are determined as follows: If 0 numbers match, no winnings If 1 number matches, win $4 If 2 numbers match, win $7 If 3 numbers match, win $100 If 4 numbers match, win $10,000 If 5 numbers match, win $1 Million If all 6 numbers match, win Grand Prize of $450 Million Output the user's lottery ticket, the powerball numbers, how many matched, and the amount of money won. (JAVA PROGRAM HELP)
Explanation / Answer
Here you go :)
comment if you have any doubts.
//It also performs error checking for the user input so that it is between 0 and 9
//PowerBall class
import java.util.*;
public class PowerBall
{
private int balls[];
public PowerBall()
{
balls=new int[6];
Random r=new Random();
for(int i=0;i<balls.length;i++)
{
balls[i]=r.nextInt(10);
}
}
public int checkMatch(int[] userArray)
{
int matches=0;
for(int i=0;i<balls.length;i++)
{
if(balls[i]==userArray[i])matches++;
}
return matches;
}
public static void main(String[] args)
{
int[] userArray=new int[6];
Scanner input=new Scanner(System.in);
for(int i=0;i<userArray.length;i++)
{
while(true)
{
System.out.print("Enter the number "+(i+1)+": ");
userArray[i]=input.nextInt();
input.nextLine();
if(userArray[i]>=0 && userArray[i]<=9)break;
}
}
PowerBall pb=new PowerBall();
int count=pb.checkMatch(userArray);
System.out.println("User's lottery ticker: "+Arrays.toString(userArray));
System.out.println("Powerball numbers: "+Arrays.toString(pb.balls));
System.out.println("Matched numbers: "+count);
switch(count)
{
case 0:System.out.println("No winnings!");break;
case 1:System.out.println("You have won $4");break;
case 2:System.out.println("You have won $7");break;
case 3:System.out.println("You have won $100");break;
case 4:System.out.println("You have won $10,000");break;
case 5:System.out.println("You have won $1 Million");break;
case 6:System.out.println("You have won $450 Million");break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.