I need this done in Java. I need help in the GUI portion of this, I\'m having a
ID: 3697531 • Letter: I
Question
I need this done in Java.
I need help in the GUI portion of this, I'm having a har dtime with understanding putting a GUI into a code.
Write a lottery application that simulates a lottery. The class should hava an array of five integers nammed lotteryNumbers. The constructor should use the Random class (from tha Java API) to generate a random number in the range of 0 through 9 for each element in the array. The class should also have a method that accepts an array of five integers that represents a persons's lottery picks. The method is to comapre the corresponding elements in the two arrays and return the number of digits that match. For example, the following shows the lotteryNumbers array an the user's array with sample numbers stored in each. There are two matching digits (elements 2 and 4).
lotteryNumbers array
7 4 9 1 3
User's array:
4 2 9 7 3
In addition, the class should have a method that returns a copy of the lotteryNumbers array. Demonstrate the class in a program that utilizes GUI and asks the user to enter five numbers. The program should dispaly the numbers of the digits that matches the randomly generated lottery number. If all of the digits match, dispaly a message proclaming the user a grand prize winner. Prices are categorized as following:
0 mathces = sorry no prize
1 match = $2
2 matces =$5
3 matches = $10
4 matches = $5,000
5 matches = $50,000
The GUI should look something like this
The play button should start the random lottery number drawings and the reset button should start a new game
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
const int lotteryDigits = 10;
const int SIZE = 5;
int generateLottery(int[], int, int);
int userInput(int[], int);
int matchCounter(int[], int[], int);
void displayNumbers(int[], int[]);
void winnerOrLoser(int);
using namespace std;
int main()
{
int lottery[5] = {0, 0, 0, 0, 0};
int user[5] = {0, 0, 0, 0, 0};
int matches = 0;
generateLottery(lottery, SIZE, lotteryDigits);
userInput(user, SIZE);
matches = matchCounter(lottery, user, matches);
displayNumbers(lottery, user);
winnerOrLoser(matches);
system("pause");
return 0;
}
int generateLottery(int lottery[], int, int)
{
unsigned seed = time(0);
srand(seed);
for (int y=0; y<SIZE; y++)
{
lottery[y] = rand() % lotteryDigits;
}
return lottery[0], lottery[1], lottery[2], lottery[3], lottery[4];
}
int userInput(int user[], int)
{
cout << "This program will simulate a lottery. ";
for (int y=0; y<SIZE; y++)
{
cout << "Enter a digit between 0 and 9:---> ";
cin >> user[y];
while (user[y]<0 || user[y]>9)
{
cout << "Error! Entry must be between 0 and 9:---> ";
cin >> user[y];
}
}
return user[0], user[1], user[2], user[3], user[4];
}
int matchCounter(int lotto[], int input[], int)
{
int match = 0;
for (int x = 0; x < SIZE; ++x)
{
if (lotto[x] == input[x])
match = match + 1;
}
return match;
}
void displayNumbers(int lottery[], int user[])
{
cout << " The winning lottery numbers are: " << lottery[0] << " " << lottery[1] << " " << lottery[2] << " " << lottery[3] << " " << lottery[4] << endl;
cout << "Your lottery numbers are: " << user[0] << " " << user[1] << " " << user[2] << " " << user[3] << " " << user[4] << endl;
}
void winnerOrLoser(int matches)
{
cout << "You matched " << matches << " numbers";
if (matches != SIZE)
cout << " Sorry--you lose. Better luck next time. ";
else
cout << " Congratulations--you WIN!!!! ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.