Below is the code for the model of a simple game and an illustration of a user i
ID: 3841346 • Letter: B
Question
Below is the code for the model of a simple game and an illustration of a user interface for that game. The game requires the user to guess "higher", lower" or "same number" by pressing one of three buttons. The "same number- button also displays the current number. A score is maintained: plus 1 for correct, minus 1 for incorrect; and a correct/wrong message displayed as appropriate. You must write the code to create a Swing GUI that looks like the figure below and connect it to the model in the way suggested by the names of the controls and methods, and the description above. You will also need to define the minimal interfaces to connect GUI to model and model to GUI. 1. initial state 2. after correctly guessing "lower" than 15 3. after wrongly guessing "higher" than 9Explanation / Answer
//This application implements a guessing game. // //The user is expected to enter his/her guess in a //textfield and then press the Submit button. // //The application keeps track of a score from 10 down to //1 and the computer's number. If the user guesses the //computer's number then the application prints a message //of congratulations. If the user's guess is wrong, then //the score is decreased by 1 and a hint is printed to //help the user with the next guess. // //The game ends when the user has guessed the correct number. import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.Color; import java.util.Random; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class GuessGame extends JFrame { //labels for notes to the user, label for the //guess textfield and the score textfield private JLabel guessLabel1, guessLabel2; private JLabel scoreLabel; //textfields for entering the user's guess //and for displaying the user's score private JTextField guessField; private JTextField scoreField; //the button to submit the user's guess private JButton guessButton; //panels for the GUI private JPanel northPanel; private JPanel southPanel; //random is used for the Computer's guess private Random random = new Random(); //number is the user's guess private int number; //score contains the user's score private int score=10; //This is the default constructor for the GuessGame class. //It creates the GUI and the ActionListener for the //submit button. public GuessGame() { //set the text on the title bar and the layout manager super ("Guessing Game"); setLayout (new BorderLayout(1,10)); //create a listener for the submit button ButtonListener submitListener = new ButtonListener(); //create a panel to be added to the north northPanel = new JPanel(new FlowLayout()); //add the label and textfield for the user's guess to //the north panel and then add the north panel to the JFrame guessLabel1 = new JLabel ("Guess a # between 1 and 10"); northPanel.add (guessLabel1); guessField = new JTextField (3); northPanel.add(guessField); add(northPanel, BorderLayout.NORTH); //create a south panel for the rest of the GUI //the layout manager for the south panel should be //the 2 X 2 grid layout southPanel = new JPanel(new GridLayout(2,2)); //create the submit label and button and add these to //the south panel guessLabel2 = new JLabel("Press SUBMIT to check"); guessButton = new JButton ("SUBMIT"); guessButton.setBackground (Color.green); guessButton.setToolTipText("Submit your guess"); guessButton.addActionListener (submitListener); southPanel.add (guessLabel2); southPanel.add (guessButton); //create the label and textfield for the user's score //and add them to the south panel scoreLabel = new JLabel ("SCORE"); scoreField = new JTextField (3); scoreField.setText("10"); southPanel.add (scoreLabel); southPanel.add (scoreField); //add the south panel to the JFrame add (southPanel, BorderLayout.SOUTH); //make the computer guess an integer from 1 to 10 inclusive number= 1 + random.nextInt(10); } //The main method creates an instance of the class //and opens the application. public static void main (String args []) { GuessGame aGame = new GuessGame(); aGame.setTitle ("Guessing Game"); aGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aGame.setSize (300,200); aGame.setVisible (true); } //This is a private inner class that is used to handle //the submit button for the GuessGame. private class ButtonListener implements ActionListener { //This method is the event handler for the submit button. //It gets the user's guess and compares it to the computer's //number and then takes an appropriate action. public void actionPerformed (ActionEvent event) { String guessString; //used to get the user's guess int guessNumber; //this is the user's guess String output=" " + score; //this is the output string //if the event was a button click, handle it if (event.getSource() == guessButton) { //get the user's guess from the textfield //and change it into an integer guessString = guessField.getText(); guessNumber = Integer.parseInt (guessString); //is the user's guess the same as the computer's number? if (guessNumber == number) { guessField.setVisible(false); guessLabel1.setText("Congratulations! You guessed it! Game Over"); scoreField.setText(output); } else { //the user's guess was wrong so decrease the score //and give the user a hint for whether the guess //was too high or too low score--; if (guessNumber > number) guessLabel1.setText("Wrong! Too High!"); else guessLabel1.setText ("Wrong! Too Low!"); output =" " + score; scoreField.setText(output); } } } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.