Here is the problem: Create a console (standalone) application. This program wil
ID: 3634733 • Letter: H
Question
Here is the problem:Create a console (standalone) application. This program will allow you to select MegaBall lottery numbers. For the first 5 numbers you will be requested to enter a number that is greater than zero and less than or equal to 56. However, there’s a catch! Each number must be different.
When any of the first five numbers is less than 0 or greater than 56 the user will receive a message to this effect and will be asked to reenter the number. You will create the code that will display one message when a number less than 1 is entered and a different message if a number greater than 56 is entered.
If the number is the same as any number entered before it (with the exception of the megaball number) the user will receive a message to this effect and asked to reenter the number. This is the same for the second through fifth numbers. I typed in the same number a couple of times so you will see what occurs when I do.
When entering the megaball number, if the number entered IS NOT between 1 and 46 then the user will receive a message to this effect and asked to reenter the number. One message will display if the number is less than 1, and a different if the number is greater than 46.
The following MUST be included in the program:
labels and text fields for the lottery balls
a JTextArea for the instructions
the menu item shown in the below figures
the labels, there are two, at the top of the frame
the button to download the numbers
the pop-up messages as shown in the figures
the borders shown in the figure. The panel holding the JTextArea is using a
titled border.
You must have multiple classes. One that contains the accessor and mutator methods, a readInput() method and a writeOutput() method. You will not use the mutator methods but include them anyway to show that you know how to write them. Name this first program “Megaball.java”.
The second program is to be named “MegaballTest.java” and will be responsible for creating a Megaball object and calling the writeOutput() and readInput() methods from the Megaball class.
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
import java.awt.BorderLayout;
import javax.swing.border.TitledBorder;
import java.util.*;
public class Megaball extends JFrame implements ActionListener
{
private int rangeMax = 56;
private JCheckBox[] checkboxes = new JCheckBox[rangeMax];
private JButton button = new JButton("View Results!");
private JLabel megaballNumbers = new JLabel("Lottery is luck");
private JPanel wrapper = new JPanel();
private JMenuBar mainBar = new JMenuBar();
private JMenu menu1 = new JMenu("About Megaball");
private JMenuItem link1 = new JMenuItem("Rules");
/**
* Constructor.
*/
public Megaball()
{
super("Megaball Lottery");
setSize(590, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(mainBar);
mainBar.add(menu1);
menu1.add(link1);
link1.addActionListener(this);
// Wrapper and padding.
wrapper.setLayout(new BorderLayout());
wrapper.setBorder(new EmptyBorder(15, 15, 15, 15));
JLabel instructions = new JLabel("Choose 5 distinct numbers!");
JPanel panel = new JPanel( new BorderLayout());
panel.setBorder(new TitledBorder("Lottery luck"));//creating titled border for JPanel
// Create the checkboxes.
for (int i = 0; i < =rangeMax; ++i)
{
checkboxes[i] = new JCheckBox(String.valueOf(i + 1), false);
checkboxes[i].addActionListener(this);
panel.add(checkboxes[i]);
}
button.addActionListener(this);
// Add components.
wrapper.add(instructions, BorderLayout.WEST);
wrapper.add(panel, BorderLayout.CENTER);
wrapper.add(button, BorderLayout.SOUTH);
wrapper.add(megaballNumbers, BorderLayout.NORTH);
add(wrapper);
setVisible(true);
}
/**
* Checks the selected numbers against the random numbers.
*/
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == link1)
{
JOptionPane.showMessageDialog(null, "Enter numbers in range 1 to 56");
}
else
{
JCheckBox checkbox;
int counter = 0;
int max = 5;
int i, j = max;
int numMatches = 0;
int randomWinner = 0;
int[] selected = new int[max];
int[] winners = new int[max];
Random random = new Random();
String message = "";
String winnings = "";
String selectedNumbers = "";
// Make sure all 5 checkboxes are selected.
if (e.getSource() != button)
{
for (i = 0; i < rangeMax; ++i)
{
if (checkboxes[i].isSelected())
{
++counter;
}
}
if (counter > max)
{
checkbox = (JCheckBox) e.getSource();
checkbox.setSelected(false);
JOptionPane.showMessageDialog(null, "Only select " + max + " numbers.");
}
}
// If the button is clicked.
else
{
// First, check to make sure that 5 numbers are selected.
for (i = 0; i <= rangeMax; ++i)
{
if (checkboxes[i].isSelected())
{
++counter;
}
}
// If not selected,display message to the user.
if (counter < max)
{
JOptionPane.showMessageDialog(null, "Please select all " + max + " numbers.");
}
// Now, generate winning numbers randomly and match with selected numbers of user
else
{
// Generate 5 random numbers.
j = max;
for (i = 0; i < max; ++i)
{
// Make sure the random number has not been selected already.
randomWinner = 0;
while (randomWinner == 0)
{
randomWinner = random.nextInt(rangeMax) + 1;
for (int k = 0; k < winners.length; ++k)
{
if (winners[k] == randomWinner)
{
randomWinner = 0;
break;
}
}
}
// Add the winning number to the winners array
--j;
winners[j] = randomWinner;
}
// Collect the selected numbers of the user
j = max;
for (i = 0; i < rangeMax; ++i)
{
if (checkboxes[i].isSelected())
{
--j;
selected[j] = Integer.parseInt(checkboxes[i].getText());
checkboxes[i].setSelected(false);
selectedNumbers += selected[j] + " ";
}
}
// Compare!
for (i = 0; i < max; ++i)
{
message += " " + String.valueOf(winners[i]);
for (j = 0; j < max; ++j)
{
if (selected[j] == winners[i])
{
System.out.println(selected[j]);
++numMatches;
}
}
}
// Display selected numbers,winning numbers of megaball and number of matches to user
megaballNumbers.setText("<html><body>Winning Numbers: " + message + "<br>Your Numbers: " + selectedNumbers + "<br>Matches: " + numMatches + " + "</strong></body></html>");
}
}
}
}
class MegaballTest
{
//Initialize MegaBall object
public static void main(String[] args)
{
int NUM_DIGITS = 5;
int[] userNumbers = new int[NUM_DIGITS];
int[] lotteryNumbers = new int[NUM_DIGITS];
int sameNum;
generateNumbers(lotteryNumbers);
readInput(userNumbers);
sameNum = compareArrays(lotteryNumbers, userNumbers);
public static void generateNumbers(int[] lotteryNumbers) {
Random randNum = new Random();
lotteryNumbers[0] = randNum.nextInt(10);
lotteryNumbers[1] = randNum.nextInt(10);
lotteryNumbers[2] = randNum.nextInt(10);
lotteryNumbers[3] = randNum.nextInt(10);
lotteryNumbers[4] = randNum.nextInt(10);
return lotteryNumbers[4];
}
public static void readInput(int[] userDigits) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your 1st number which is between 0 and 56");
System.out.print("Enter digit 1: ");
userDigits[0] = keyboard.nextInt();
System.out.print("Enter digit 2: ");
userDigits[1] = keyboard.nextInt();
System.out.print("Enter digit 3: ");
userDigits[2] = keyboard.nextInt();
System.out.print("Enter digit 4: ");
userDigits[3] = keyboard.nextInt();
System.out.print("Enter digit 5: ");
userDigits[4] = keyboard.nextInt();
keyboard.close();
return userDigits[4];
}
for (i=0;i<=5;i++)
{
if (userDigits[i]< 1)
System.out.println("Number cant be less than 1");
else if(userDigits[i]>56)
System.out.println("Number cant be greater than 56");
else
System.out.println("Number in permissible range 1-56");
}
}
public static int compareArrays(int[] userDigits, int[] lotteryNumbers) {
int sameNum = 0;
for (int i = 0; i < 5; i++) {
for (int x = 0; x < 5; x++) {
if (lotteryNumbers[i] == userDigits[x]) {
sameNum++;
}
}
}
return sameNum;
}
public static void writeOutput()
{
System.out.println("Lottery numbers: " + lotteryNumbers[0] + " "
+ lotteryNumbers[1] + " " + lotteryNumbers[2] + " "
+ lotteryNumbers[3] + " " + lotteryNumbers[4] + " ");
System.out.println("Player's selected numbers: " + userNumbers[0] + " "
+ userNumbers[1] + " " + userNumbers[2] + " " + userNumbers[3]
+ " " + userNumbers[4] + " ");
System.out.println("Number of matching digits: " + sameNum);
}
Megaball frame = new Megaball(); // use this statement to create megaball console
}
}