I need this done in Java. I need help in the GUI portion of this, I\'m having a
ID: 3697071 • 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
Input/Output User's Lottery Numbers: Winning Lottery Numbers: OE131 11 Number of Matches: Prizes: 0 match Sorry, no prize! 1 match-$2 2 matches = $5 3 matches = $10 4 matches = $5,000 5 matches =$50,000 Prize: S2 Reset PlayExplanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class lottery{
private int lotteryNumbers[];
private int userLottery[];
private int match;
lottery()
{
Random num = new Random();
for ( int i=0; i<5; i++ )
{
int randomNum = num.nextInt(20);
lotteryNumbers[i] = randomNum;
}
}
public void user_input()
{
System.out.println("Please enter user lottery ticket numbers (separated by spaces):");
BufferedReader lottery_user = new BufferedReader(new InputStreamReader(System.in));
int values;
values = lottery_user.readLine();
String[] split = values.split(" ");
for (int i =0 ; i<5; i++)
userLottery[i]= Integer.parseInt(split[i]);
}
public int compare()
{
for(int i=0; i<5; i++)
{
if( lotteryNumbers[i] == userLottery[i])
match++;
}
return match;
}
public void displayLottery()
{
for (int i =0; i<5;i ++)
{
textArea = new JTextArea(lotteryNumbers[i]);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
}
}
public void displayUserLottery()
{
for (int i =0; i<5;i ++)
{
textArea = new JTextArea(userLottery[i]);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
}
}
}
public static void main(String args[]){
lottery newLottery = new lottery();
newLottery.user_input();
String prize = null;
int match = newLottery.compare();
if(match == 0)
prize = "sorry, no prize;
else if(match == 1)
prize = "$2";
else if(match == 2)
prize = "$5";
else if(match == 3)
prize = "$10";
else if(match == 4)
prize = "$5,000";
else if(match == 5)
prize = "$50,000";
System.out.println("User's lottery numbers:");
newLottery.displayUserLottery();
System.out.println("Winning lottery");
newLottery.displayLottery();
System.out.println("0 matches = sorry no prize"+
"1 match = $2"+
"2 matces =$5"+
"3 matches = $10"+
"4 matches = $5,000"+
"5 matches = $50,000");
System.out.println("Number of matches:");
textArea = new JTextArea(match);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
System.out.println("Prize");
textArea = new JTextArea(prize);
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setEditable(false);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.