CS 110: Introduction to Computing with Java
No Pre-lab for Lab 7.
We continue looking at the Othello game. This time, we work with a GUI version of the game. The code we provide almost is complete. In this case, you are to finish off the implementation of the java event model to control the interaction between the Application and a component.
This lab gives you some experience working with the Java event model.
Copy the New Othello project. The Java source files for the Othello game can be found in lab7.zip. Extract these files to folder of your choosing on the student drive. Open Dr. Java, create a new project, open the files you just unzipped into the project, and save the project as always.
Run the Application. The Game class has the main method. Play the game (and have fun). Notice that the radio buttons, while appearing to work, do not affect the way the game behaves. The human always goes first no matter what radio button is selected.
Edit the WhoFirstListener class. Objects of this class listen for the radio buttons being selected. Notice the following two facts:
The class is constructed with a reference to a game object. This is called a callback. The idea is that the game object passed to the constructor will be manipulated when the radio buttons are selected. This makes sense since when the user selects a radio button, we need to tell the game to set which player goes first.
Currently, the actionPerformed method does not do anything. This is the method we will write.
Write the actionPerformed method of the WhoFirstListener class. Basically, you are implementing the following logic:
If the user selected the “HumanFirst” button, call the Game objects’s setHumanFirst method with the value true.
Else, call the Game objects’s setHumanFirst method with the value false.
In either case, call the Game objects’s newGame method in order to start over.
The hard part here is figuring out which radio button is selected. The best way to do this is to call the getActionCommand method of the ActionEvent object passes to the actionPerformed method. This method will return a String that will have one of the values “Human First” or “Computer First”.
Test your implementation
Look at the requirements for the lab report. Work with your partner to find the answers to question 1.
There is no sample output for this assignment.
Before you leave, have your TA check off that you completed the lab. Make sure each person saves a copy of your work.
Write a document describing your experiences. Your lab must be printed (not handwritten).
Answer the following questions related to what you did in this week’s lab. You may complete the code on your own, but the TA must certify that most of your work was done in the lab.
Answer each of the following questions about the game class:
What is the name of the variable storing references to each JRadioButton object?
What do you think the setMnemonic method does on a JRadioButton object?
How many WhoFirstListener objects are created?
What line(s) of code establish the connection between the JRadioButton objects and the WhoFirstListener object(s)?
What line(s) of code sets the value that is returned by getActionCommand method call to the ActionEvent object in the WhoFirstListener class?
What is the role of a ButtonGroup object?
Describe what you learned doing this lab. Explain what was difficult and what was easy.
Attach a listing of your completed WhoFirstListener classes.
Note: You should work alone on writing the lab report.
Note: The assignment is due at the BEGINNING of your next lab. No late assignments will be accepted. Emailed assignments will not be accepted. If you are not going to be in lab on the due date, you can turn the assignment ahead of time to the CS110 TA box in the CS department office.