Use Java IDE to write a program that simulates a lottery. The program should hav
ID: 3707344 • Letter: U
Question
Use Java IDE to write a program that simulates a lottery. The program should have array of 6 integers named winning, with a randomly generated number in the range of 1 through 9 for each element in the array. The program should ask user to enter 6 numbers and store them in another integer array named player. The program then compares the numbers in 2 arrays to find out how many numbers match. If the two numbers are on the same position in both arrays, it is a match. (This is using parallel array concept to compare two arrays.) The program output should display the winning numbers, player’s numbers, and how many numbers matched. For instance, if your winning numbers are 3,5,9,1,4,7 and your player’s numbers are 2,5, 7,1, 9,8, then you have two matches (5 and 1). 5 is on the second position and 1 is on the fourth position in both arrays.
Input Validation: Do not accept player’s number out of range of 1-9.
To create this program, you can use the following design tips:
1. Create two integer arrays with 5 elements each.
2. You can create methods such as generateWinningNumber,
getPlayerNumber, getMatches, and printNumber.
3. Use Random class to generate random numbers. Here is the example of
how to create method generateWinningNumber;
}
4. Use for loop to complete each task.
5. Use while loop to validate user input as follows;
6. To compare two arrays to find out the matches, use the following code;
}
Explanation / Answer
//Code to copy
//SimulateLottery.java
import java.util.Random;
import java.util.Scanner;
//SimulateLottery.java
public class SimulateLottery {
private static Scanner input=new Scanner(System.in);
public static void main(String[] args) {
int[] wList={0,0,0,0,0};
int[] pList={0,0,0,0,0};
//calling method generateWinningNumber
generateWinningNumber(wList);
//calling method getPlayerNumber
getPlayerNumber(pList);
System.out.println("Winning Number : ");
//calling method printNumber
printNumber(wList);
System.out.println("Player Number : ");
//calling method printNumber
printNumber(pList);
//calling getMatches
int matches=getMatches(wList, pList);
System.out.printf("# of matches : %d",matches);
}
/**Method to generate winning lottery number
* array in a range of 1 to 9*/
public static void generateWinningNumber(int[] wList)
{
Random rNum = new Random();
final int MIN=1, MAX=9;
for (int i=0; i<wList.length;i++)
{
wList[i]=rNum.nextInt(MAX-MIN+1)+MIN;
}
}
/**Method that takes array and prompt for array element
* of players.
* Input validation for user number in a range of 1 to 9
* */
public static void getPlayerNumber(int[] pList)
{
int num;
for (int i = 0; i < pList.length; i++)
{
System.out.printf("Enter lottery number [%d] : ",(i+1));
num=input.nextInt();
while (num<1 || num>9) //pList is the array of player number
{
System.out.print("Invalid Number. Enter number 1-9: ");
System.out.printf("Enter lottery number [%d] : ",(i+1));
num=input.nextInt();
}
pList[i]=num;
}
}
/**Method that takes array and print the list
* on console*/
public static int getMatches(int[] wList, int []pList)
{
int match=0;
for (int i=0; i<pList.length; i++) //pList is the array of player numbers
{
if (wList[i]==pList[i]) //wList is the array of winning numbers
match++;
}
return match;
}
/**Method that takes array and print the list
* on console*/
public static void printNumber(int[] list)
{
System.out.printf("%-2d,",list[0]);
for (int i = 1; i < list.length-1; i++) {
System.out.printf("%-2d ,",list[i]);
}
System.out.printf("%-2d",list[list.length-1]);
System.out.println();
}
}
------------------------------------------------------------------------------------------
Sample Output:
Enter lottery number [1] : 1
Enter lottery number [2] : 10
Invalid Number. Enter number 1-9:
Enter lottery number [2] :
2
Enter lottery number [3] : 3
Enter lottery number [4] : 1
Enter lottery number [5] : 2
Winning Number :
1 ,8 ,9 ,2 ,6
Player Number :
1 ,2 ,3 ,1 ,2
# of matches : 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.