Hey all! My teacher offered and extra credit program in which we are supposed to create a simple Monty Hall Problem GUI program. Here is the assignment:
"Write a simple GUI program that simulates this game. Use a random number (1, 2, or 3) to select the winning door (see the end of this document for a brief description of Javaʼs Random class). The GUI should display three buttons, one for each door. The user can click on a button to select that door. After the user chooses a door, the program should disable one of the other door buttons and change its label to reflect that it hid a goat (Hint: use JButtonʼs setEnabled() and setText() methods for this part). The user can then click on one of the two remaining buttons to open that door, at which point those door buttons are relabeled with their contents. Your GUI should also include a “reset” button that will allow the player to try again. Include JLabels that show how many times the game has been played, how many times the contestant switched doors, how many times the player won the car, and how many times the player won a goat. Note that JLabel also has a setText() method."
The code that i have so far is below.. it has many errors, i simply went through it bit by bit trying to put together whatever I could.. PLEASE HELP me with this, I really need the credit. I've searched through textbooks, and watched online videos on GUI programming and I'm still fuzzy on it.. if anyone could look at the code and give a suggestion here or there I would greatly appreciate it!!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyGUI implements ActionListener {
private JFrame window;
private JButton doorOneButton;
private JButton doorTwoButton;
private JButton doorThreeButton;
private JButton resetButton;
Random r = new Random();
public MyGUI()
{
}
public void display()
{
window = new JFrame("Monty Hall Problem");
window.setSize(500,500);
window.setLayout(new BorderLayout());
doorOneButton = new JButton("Door One");
doorOneButton.addActionListener(this);
doorTwoButton = new JButton("Door Two");
doorTwoButton.addActionListener(this);
doorThreeButton = new JButton("Door Three");
doorThreeButton.addActionListener(this);
resetButton = new JButton("Reset");
resetButton.addActionListener(this);
resetButton.setEnabled(false);
}
public void actionPerformed (ActionEvent e)
{
r.nextInt(3);
if(e.getSource() == resetButton)
{
stop();
}
else if(e.getSource() == doorOneButton)
{
doorTwoButton.setEnabled(false);
doorTwoButton.setText("");
doorThreeButton.setEnabled(false);
doorThreeButton.setText("");
}
else if(e.getSource() == doorTwoButton)
{
doorOneButton.setEnabled(false);
doorOneButton.setText("");
doorThreeButton.setEnabled(false);
doorThreeButton.setText("");
}
else if(e.getSource() == doorThreeButton)
{
doorOneButton.setEnabled(false);
doorOneButton.setText("");
doorTwoButton.setEnabled(false);
doorTwoButton.setText("");
}