**I already posted this question once. PLEASE make sure to name the file FirstNa
ID: 3721748 • Letter: #
Question
**I already posted this question once. PLEASE make sure to name the file FirstName_LastName_ A7. Otherwise the answer will not be useful to me and I will not rate it with a thumbs up.***
IMPORTANT: Name this program: FirstName_LastName_A7.
Write a JAVA program for NetBeans 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, ,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;
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;
}
}
4. Use for loop to complete each task.
5. Use while loop to validate user input as follows;
while (pList[i] <1 || pList[i]>9) //pList is the array of player number
{
System.out.print("Invalid Number. Enter number 1-9:");
pList[i]=input.nextInt();
}
6. To compare two arrays to find out the matches, use the following code;
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++;
}
Explanation / Answer
I have completed the implementation of the required class FirstName_LastName_A7.java as per the requirements. Completed all the methods necessary. Drop a comment if you have doubts. Thanks.
// FirstName_LastName_A7.java
import java.util.Random;
import java.util.Scanner;
public class FirstName_LastName_A7 {
public static void main(String[] args) {
/**
* initializing two arrays of length 6
*/
int wList[] = new int[6];
int pList[] = new int[6];
/**
* generating winning numbers
*/
generateWinningNumber(wList);
/**
* getting and storing player numbers
*/
getPlayerNumber(pList);
/**
* finding the number of matches
*/
int matches = getMatches(wList, pList);
/**
* displaying the stats
*/
System.out.println("Player Number: ");
printNumber(pList);
System.out.println("Winning Number: ");
printNumber(wList);
System.out.println("Matches: " + matches);
if (matches >= 2) {
// win
System.out.println("You win!");
} else {
// lost
System.out.println("You didn't win!");
}
}
/**
* method to generate and fill the winning list
*/
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 to get player numbers from the user and fill the player numbers
* list
*/
public static void getPlayerNumber(int[] pList) {
Scanner scanner = new Scanner(System.in);// to read user input
int counter = 0;
/**
* looping until all 6 elements are filled
*/
while (counter < pList.length) {
System.out.print("Enter number (1-9): ");
int num = scanner.nextInt();
if (num < 1 || num > 9) {
// invalid
System.out.print("Invalid Number, ");
} else {
// valid, adding to the array and updating the index
pList[counter] = num;
counter++;
}
}
}
/**
* method to find and return the number of matches
*
* @param wList
* - winning numbers
* @param pList
* - player numbers
* @return - how many elements are on the same position in both arrays
*/
public static int getMatches(int[] wList, int[] pList) {
int matches = 0;
for (int i = 0; i < wList.length; i++) {
if (wList[i] == pList[i]) {
matches++;
}
}
return matches;
}
/**
* method to print an array of numbers
*/
public static void printNumber(int num[]) {
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
System.out.println();
}
}
/*OUTPUT1*/
Enter number (1-9): 2
Enter number (1-9): 12
Invalid Number, Enter number (1-9): -1
Invalid Number, Enter number (1-9): 5
Enter number (1-9): 7
Enter number (1-9): 8
Enter number (1-9): 4
Enter number (1-9): 9
Player Number:
2 5 7 8 4 9
Winning Number:
3 8 1 7 5 8
Matches: 0
You didn't win!
/*OUTPUT 2*/
Enter number (1-9): 7
Enter number (1-9): 7
Enter number (1-9): 5
Enter number (1-9): 5
Enter number (1-9): 4
Enter number (1-9): 3
Player Number:
7 7 5 5 4 3
Winning Number:
9 7 7 5 9 7
Matches: 2
You win!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.