To feed the gambling addiction we established with the slot machine exercise in
ID: 3763274 • Letter: T
Question
To feed the gambling addiction we established with the slot machine exercise in Assignment 3, 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. 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.
Explanation / Answer
PowerBall.java
import java.util.Random;
/**
* @author srinivas
*
*/
public class PowerBall {
public int arr[] = new int[6];
public PowerBall() {
// arr[0]=0;
arr = getRandomNonrepeat(1, 9);
}
public int checkMatch(int inArray[]) {
int match = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == inArray[i]) {
match++;
}
}
return match;
}
public static int[] getRandomNonrepeat(int iMin, int iMax) {
int iSize = iMax - iMin + 1;
int[] random = new int[6];
int iLoopRepetion = 0;
Random rand = new Random();
for (int i = 0; i < 6; i++) {
random[i] = rand.nextInt(iSize) + 1;
for (iLoopRepetion = 0; iLoopRepetion < i; iLoopRepetion++)
if (random[i] == random[iLoopRepetion])
break;
if (iLoopRepetion != (i) && iLoopRepetion != iSize - 1)
i--;
}
return random;
}
}
PowerBallTest.java
import java.util.Scanner;
public class PowerBallTest {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("----PowerBall----");
int inArray[] = new int[6];
for (int i = 1; i <= 6; i++) {
System.out.print("Enter value" + i + " :");
inArray[i - 1] = scanner.nextInt();
}
PowerBall powerBall = new PowerBall();
int match = powerBall.checkMatch(inArray);
if (match == 0) {
System.out.println(match + " Matched, no Winnings");
} else if (match == 1) {
System.out.println(match + " Matched, wins $4");
} else if (match == 2) {
System.out.println(match + " Matched, wins $7");
} else if (match == 3) {
System.out.println(match + " Matched, wins $100");
} else if (match == 4) {
System.out.println(match + " Matched, wins $10,000");
} else if (match == 5) {
System.out.println(match + " Matched, wins $1 Million");
} else if (match == 6) {
System.out.println(match
+ " Matched, wins Grand Prize of $450 Million");
}
}
}
OUTPUT1:
----PowerBall----
Enter value1 :1
Enter value2 :2
Enter value3 :3
Enter value4 :4
Enter value5 :5
Enter value6 :6
0 Matched, no Winnings
OUTPUT1:
----PowerBall----
Enter value1 :1
Enter value2 :2
Enter value3 :3
Enter value4 :4
Enter value5 :5
Enter value6 :6
2 Matched, wins $7