Enhance the Flag Quiz application by counting the number of questions that were answered correctly. After all the questions have been answered, display a message in a JTextField that describes how well the user performed (Fig. 16.39). The JTextField, called commentJTextField, has already been added in the exercise template. The following table (Fig. 16.40) shows which messages to display:



a) Copying the template to your working directory. Copy the C:Examples Tutorial16ExercisesEnhancedFlagQuiz directory to your C:SimplyJava directory.

b) Opening the template file. Open the FlagQuiz.java file in your text editor.

c) Adding a variable to count the number of correct answers. On line 24, add a com- ment indicating that the variable you are about to create will be used to store the number of correct answers from the user. On line 25, declare int variable correct and initialize it to 0.

d) Counting the correct answers. On line 196, increment correct. This statement causes correct to be incremented each time a correct answer is submitted.

e) Displaying the message. Add a switch statement on lines 209–227 that displays the proper message in commentJTextField depending on the value of correct.

f) Saving the application. Save your modified source code file.

g) Opening the Command Prompt window and changing directories. Open the Com- mand Pro

```
1 // FlagQuiz.java
2 // Quiz the user on their knowledge of flags. The user must try to
3 // match five flags to their countries.
4 import java.util.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import javax.swing.border.*;
9
10 public class FlagQuiz extends JFrame
11 {
12 // array of country names
13 private String[] countries = { "Russia", "China", "United States",
14 "Italy", "Australia", "South Africa", "Brazil", "Spain" };
15
16 // boolean array tracks displayed flags
17 private boolean[] flagsUsed = new boolean[ countries.length ];
18
19 private int currentIndex; // contains the index of current flag
20
21 // tracks the number of flags that have been displayed
22 private int count = 1;
23
24 // tracks the number of correct responses from the user
25 private int correct = 0;
26
27 // JPanel and JLabel for displaying a flag image
28 private JPanel flagJPanel;
29 private JLabel flagIconJLabel;
30
31 // JLabel and JComboBox for choosing a country
32 private JLabel selectCountryJLabel;
33 private JComboBox selectCountryJComboBox;
34
35 // JTextField for giving the user feedback
36 private JTextField feedbackJTextField;
37
38 // JButton to submit an answer
39 private JButton submitJButton;
40
41 // JButton to display the next flag
42 private JButton nextFlagJButton;
43
44 // JTextField for final comment
45 private JTextField commentJTextField;
46
47 // no-argument constructor
48 public FlagQuiz()
49 {
50 createUserInterface();
51 }
52
53 // create and position GUI components; register event handlers
54 private void createUserInterface()
55 {
56 // get content pane for attaching GUI components
57 Container contentPane = getContentPane();
58
59 // enable explicit positioning of GUI components
60 contentPane.setLayout( null );
61
62 // set up flagJPanel
63 flagJPanel = new JPanel();
64 flagJPanel.setBounds( 16, 8, 100, 90 );
65 flagJPanel.setLayout( null );
66 flagJPanel.setBorder( new TitledBorder( "Flag" ) );
67 contentPane.add( flagJPanel );
68
69 // set up flagIconJLabel
70 flagIconJLabel = new JLabel();
71 flagIconJLabel.setBounds( 10, 14, 80, 80 );
72 flagIconJLabel.setHorizontalAlignment( JLabel.CENTER );
73 flagJPanel.add( flagIconJLabel );
74
75 // set up selectCountryJLabel
76 selectCountryJLabel = new JLabel();
77 selectCountryJLabel.setBounds( 136, 8, 88, 21 );
78 selectCountryJLabel.setText( "Select country:" );
79 contentPane.add( selectCountryJLabel );
80
81 Arrays.sort( countries ); // sort the array
82
83 // set up selectCountryJComboBox
84 selectCountryJComboBox = new JComboBox( countries );
85 selectCountryJComboBox.setBounds( 136, 32, 135, 21 );
86 selectCountryJComboBox.setMaximumRowCount( 3 );
87 contentPane.add( selectCountryJComboBox );
88
89 displayFlag(); // display first flag
90
91 // set up feedbackJTextField
92 feedbackJTextField = new JTextField();
93 feedbackJTextField.setBounds( 136, 64, 135, 32 );
94 feedbackJTextField.setHorizontalAlignment(
95 JTextField.CENTER );
96 feedbackJTextField.setEditable( false );
97 contentPane.add( feedbackJTextField );
98
99 // set up submitJButton
100 submitJButton = new JButton();
101 submitJButton.setBounds( 287, 8, 88, 32 );
102 submitJButton.setText( "Submit" );
103 contentPane.add( submitJButton );
104 submitJButton.addActionListener(
105
106 new ActionListener() // anonymous inner class
107 {
108 // event handler called when submitJButton is pressed
109 public void actionPerformed( ActionEvent event )
110 {
111 submitJButtonActionPerformed( event );
112 }
113
114 } // end anonymous inner class
115
116 ); // end call to addActionListener
117
118 // set up nextFlagJButton
119 nextFlagJButton = new JButton();
120 nextFlagJButton.setBounds( 287, 48, 88, 32 );
121 nextFlagJButton.setText( "Next Flag" );
122 nextFlagJButton.setEnabled( false );
123 contentPane.add( nextFlagJButton );
124 nextFlagJButton.addActionListener(
125
126 new ActionListener() // anonymous inner class
127 {
128 // event handler called when nextFlagJButton is pressed
129 public void actionPerformed( ActionEvent event )
130 {
131 nextFlagJButtonActionPerformed( event );
132 }
133
134 } // end anonymous inner class
135
136 ); // end call to addActionListener
137
138 // set up commentJTextField
139 commentJTextField = new JTextField();
140 commentJTextField.setBounds( 287, 88, 88, 24 );
141 commentJTextField.setHorizontalAlignment( JLabel.CENTER );
142 commentJTextField.setEditable( false );
143 contentPane.add( commentJTextField );
144
145 // set properties of application's window
146 setTitle( "Flag Quiz" ); // set title bar string
147 setSize( 390, 145 ); // set window size
148 setVisible( true ); // display window
149
150 } // end method createUserInterface
151
152 // return an unused random number
153 private int getUniqueRandomNumber()
154 {
155 Random generator = new Random();
156 int randomNumber;
157
158 // generate random numbers until unused flag is found
159 do
160 {
161 // generate a number between 0-7
162 randomNumber = generator.nextInt( 8 );
163 }
164 while ( flagsUsed[ randomNumber ] == true );
165
166 // indicate that flag has been used
167 flagsUsed[ randomNumber ] = true;
168
169 return randomNumber;
170
171 } // end method getUniqueRandomNumber
172
173 // choose a flag and display it in the JLabel
174 private void displayFlag()
175 {
176 currentIndex = getUniqueRandomNumber(); // get an unused flag
177
178 // create the path for that flag
179 String country =
180 ( String ) selectCountryJComboBox.getItemAt( currentIndex );
181 String countryPath = "images/" + country + ".png";
182
183 // set the flagIconJLabel to display the flag
184 flagIconJLabel.setIcon( new ImageIcon( countryPath ) );
185
186 } // end method displayFlag
187
188 // check the answer and update the quiz
189 private void submitJButtonActionPerformed( ActionEvent event )
190 {
191 // determine whether the answer was correct
192 if ( selectCountryJComboBox.getSelectedIndex()
193 == currentIndex )
194 {
195 feedbackJTextField.setText( "Correct!" );
196 correct++;
197 }
198 else // if an incorrect answer is given
199 {
200 feedbackJTextField.setText( "Sorry, incorrect." );
201 }
202
203 // inform user if quiz is over
204 if ( count == 5 )
205 {
206 feedbackJTextField.setText(
207 feedbackJTextField.getText() + " Done!" );
208
209 switch ( correct )
210 {
211 case 5:
212 commentJTextField.setText( "Excellent!" );
213 break;
214 case 4:
215 commentJTextField.setText( "Very good!" );
216 break;
217 case 3:
218 commentJTextField.setText( "Good." );
219 break;
220 case 2:
221 commentJTextField.setText( "Poor." );
222 break;
223 default:
224 commentJTextField.setText( "Fail." );
225 break;
226
227 } // end switch
228
229 nextFlagJButton.setEnabled( false );
230 submitJButton.setEnabled( false );
231 selectCountryJComboBox.setEnabled( false );
232 }
233 else // if less than 5 flags have been displayed
234 {
235 submitJButton.setEnabled( false );
236 nextFlagJButton.setEnabled( true );
237 }
238
239 } // end method submitJButtonActionPerformed
240
241 // display next flag in the quiz
242 private void nextFlagJButtonActionPerformed( ActionEvent event )
243 {
244 displayFlag(); // display next flag
245 count++;
246
247 // reset GUI components to initial states
248 feedbackJTextField.setText( "" );
249 selectCountryJComboBox.setSelectedIndex( 0 );
250 submitJButton.setEnabled( true );
251 nextFlagJButton.setEnabled( false );
252
253 } // end method nextFlagJButtonActionPerformed
254
255 // main method
256 public static void main( String[] args )
257 {
258 FlagQuiz application = new FlagQuiz();
259 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
260
261 } // end method main
262
263 } // end class FlagQuiz
```

Computer Science & Information Technology

You might also like to view...

PivotTables ________ or summarize large amounts of data for analysis

Fill in the blank(s) with correct word

Computer Science & Information Technology

A text box can have ____, which allow you to view any information not currently showing in the control.

A. scroll bars B. view bars C. scroll handles D. highlight bars

Computer Science & Information Technology