1 Tutorial 16 Flag Quiz Application Introducing OneDimensional

  • Slides: 60
Download presentation
1 Tutorial 16 – Flag Quiz Application Introducing One-Dimensional Arrays and JCombo. Boxes Outline

1 Tutorial 16 – Flag Quiz Application Introducing One-Dimensional Arrays and JCombo. Boxes Outline 16. 1 16. 2 16. 3 16. 4 16. 5 16. 6 Test-Driving the Flag Quiz Application Introducing Arrays Declaring and Creating Arrays Constructing the Flag Quiz Application Sorting Arrays Wrap-Up © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

2 Objective • In this tutorial, you will learn to: – – – Create

2 Objective • In this tutorial, you will learn to: – – – Create and initialize arrays. Store information in an array. Refer to individual elements of an array. Sort arrays. Use JCombo. Boxes to display options in a drop-down list. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

3 16. 1 Test-Driving the Flag Quiz Application • Data Structures – Group together

3 16. 1 Test-Driving the Flag Quiz Application • Data Structures – Group together and organize related data • Arrays – Consist of data items of the same type – Index • GUI Component JCombo. Box – Presents user options in a drop-down list © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

4 16. 1 Test-Driving the Flag Quiz Application © Copyright 1992 -2004 by Deitel

4 16. 1 Test-Driving the Flag Quiz Application © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 1 Running the completed

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 1 Running the completed Flag Quiz application. JLabel displays a flag © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. JCombo. Box contains answers (country names) 5

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 2 Selecting an answer

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 2 Selecting an answer from the JCombo. Box. Answer being selected © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Scrollbar in JCombo. Box’s drop-down list 6

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 3 Submitting the correct

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 3 Submitting the correct answer. User can select the next flag Feedback provided to the user © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 4 Displaying the next

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 4 Displaying the next flag. Submit JButton is re-enabled output. JText. Field is cleared © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 5 Submitting an incorrect

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 5 Submitting an incorrect answer. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 6 Finishing the quiz.

16. 1 Test-Driving the Flag Quiz Application (Cont. ) Figure 16. 6 Finishing the quiz. JCombo. Box is disabled when the quiz ends © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10

11 16. 2 Introducing Arrays • Position number – A value that indicates a

11 16. 2 Introducing Arrays • Position number – A value that indicates a specific location within an array – Begin at 0 and range as high as one less than the array length • Element – A piece of an array that can hold a single value • Zeroth element – The first element of an array (e. g. units. Sold[0]) • Index or subscript – The position number in brackets © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

12 16. 2 Introducing Arrays (Cont. ) • Indexed array name – The array

12 16. 2 Introducing Arrays (Cont. ) • Indexed array name – The array name followed by an index enclosed in brackets – Can be used on the left side of an assignment statement to place a new value into an array element. • Name • Value • One-dimensional – Array that uses only one index © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

13 16. 2 Introducing Arrays (Cont. ) Figure 16. 7 Array units. Sold consisting of

13 16. 2 Introducing Arrays (Cont. ) Figure 16. 7 Array units. Sold consisting of 13 elements. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

14 16. 3 Declaring and Creating Arrays int units. Sold[]; units. Sold = new

14 16. 3 Declaring and Creating Arrays int units. Sold[]; units. Sold = new int[ 13 ]; int sales. Per. Day[] = { 0, 2, 3, 6, 1, 4, 5, 6 }; • Array bounds – Determines what indices can be used to access an array element • Array initializer – Specifies the initial values of the elements in the array – Java determines the array’s size and bounds from the number elements in the initializer © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

15 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 8 Running the Sum

15 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 8 Running the Sum Array template application. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

16 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 9 Declaring an array

16 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 9 Declaring an array in the sum. Array. JButton. Action. Performed method. Creating an array of ints © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

17 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 10 Summing the values

17 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 10 Summing the values of an array’s elements. Retrieve the value of each element and add it to the total, one at a time • array. Name. length – Contains the number of elements in array. Name © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

18 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 11 Displaying the sum

18 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 11 Displaying the sum of the values of an array’s elements. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

19 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 12 Running the completed

19 16. 3 Declaring and Creating Arrays (Cont. ) Figure 16. 12 Running the completed Sum Array application. Total value of array elements © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

20 16. 4 Constructing the Flag Quiz Application When the user begins the application

20 16. 4 Constructing the Flag Quiz Application When the user begins the application Sort the array of country names alphabetically Place country names in the JCombo. Box Randomly select the first flag and store the correct answer Display the first flag When the user clicks the Submit JButton Retrieve the index of the selected country name from the JCombo. Box If the selected country’s index matches the index of the current flag Display "Correct!" in the feedback JText. Field Else Display "Sorry, incorrect. " in the feedback JText. Field If five images have been displayed Append "Done!" to the feedback. JText. Field’s text Disable the JButtons and JCombo. Box Else Disable the Submit JButton Enable the Next Flag JButton © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

16. 4 Constructing the Flag Quiz Application (Cont. ) When the user clicks the

16. 4 Constructing the Flag Quiz Application (Cont. ) When the user clicks the Next Flag JButton Randomly select a flag that has not been chosen previously Display the new flag Clear the feedback JText. Field Set the JCombo. Box to display its first item Enable the Submit JButton Disable the Next Flag JButton © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21

16. 4 Constructing the Flag Quiz Application (Cont. ) © Copyright 1992 -2004 by

16. 4 Constructing the Flag Quiz Application (Cont. ) © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22

16. 4 Constructing the Flag Quiz Application (Cont. ) © Copyright 1992 -2004 by

16. 4 Constructing the Flag Quiz Application (Cont. ) © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23

16. 4 Constructing the Flag Quiz Application (Cont. ) © Copyright 1992 -2004 by

16. 4 Constructing the Flag Quiz Application (Cont. ) © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 14 Creating and initializing

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 14 Creating and initializing the String array that stores the country names. Creating an array of Strings to store country names © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 15 Array of type

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 15 Array of type boolean that keeps track of displayed flags. Creating an array of boolean values © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 16 Declaring instance variables.

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 16 Declaring instance variables. Declaring instance variables © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 17 Setting select. Country.

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 17 Setting select. Country. JCombo. Box’s items. Adding data to a JCombo. Box © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 18 Setting select. Country.

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 18 Setting select. Country. JCombo. Box’s bounds. Set the bounds of the select. Country. JCombo. Box © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 19 Setting the maximum

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 19 Setting the maximum number of items displayed in the select. Country. JCombo. Box. Set the number of items displayed in the select. Country. JCombo. Box • Property maximum. Row. Count – Determines how many items in the JCombo. Box are displayed at once – If the total number of items in the JCombo. Box exceeds this number, a vertical scrollbar will be automatically added to the drop -down list © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 21 Flag Quiz application

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 21 Flag Quiz application with select. Country. JCombo. Box displays country names © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 21 Generating a unique

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 21 Generating a unique index. Determining if a country’s flag has been previously displayed © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 22 Updating the flags.

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 22 Updating the flags. Used array for the new flag. Indicate that a flag is now used © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 23 Returning the unique

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 23 Returning the unique index. Returning the value of random. Number © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 24 Choosing a random

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 24 Choosing a random country name. Getting index of unused flag Retrieving the flag’s corresponding country name • Method get. Item. At – Takes an int representing an index – Returns the value at that index © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 25 Creating the path

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 25 Creating the path to the image. Creating the path name of the flag’s image © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 26 Displaying a flag

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 26 Displaying a flag image. Use method set. Icon to display the flag image © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 27 Displaying a flag

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 27 Displaying a flag when your application is run. Displaying a flag when the application is first run © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 28 Flag Quiz application

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 28 Flag Quiz application displaying initial flag. Initial flag is displayed © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 29 Checking the user’s

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 29 Checking the user’s answer. Retrieve user’s answer Displaying proper feedback • Property selected. Index – Stores the index of the JComob. Box’s currently selected item • Method get. Selected. Index © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 30 Testing whether the

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 30 Testing whether the quiz is finished. Actions to perform if quiz is over © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 31 Enable user to

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 31 Enable user to display next flag if quiz is not over. Allow user to continue © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 32 Flag Quiz application

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 32 Flag Quiz application that allows user to submit an answer. © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 33 Displaying the next

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 33 Displaying the next flag. Displaying the next flag for the user to identify © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 34 Allowing the user

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 34 Allowing the user to enter the next answer. Actions to perform for next question • Method set. Selected. Index © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 35 Flag Quiz application

16. 4 Constructing the Flag Quiz Application (Cont. ) Figure 16. 35 Flag Quiz application with working Submit and Next Flag JButtons. Items are not yet alphabetized Entering a correct answer Application when quiz is finished © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46

47 16. 5 Sorting Arrays • Sorting – Arranging data in some particular order

47 16. 5 Sorting Arrays • Sorting – Arranging data in some particular order • Pass-by-value – A copy of the argument’s value is passed to the method – Changes made to the copy of the argument do not affect the original variable’s value – Primitive types are always passed by value • Pass-by-reference – The called method is given access directly to the argument in the caller – The original data in the caller can be modified by the called method – Objects are always passed by reference © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

48 16. 5 Sorting Arrays (Cont. ) Figure 16. 36 Sorting the array of country

48 16. 5 Sorting Arrays (Cont. ) Figure 16. 36 Sorting the array of country names. Alphabetizing the country names in the array • Method Arrays. sort – Sorts the values in the array into ascending alphabetical order © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

49 16. 5 Sorting Arrays (Cont. ) Figure 16. 37 Flag Quiz application running. Items

49 16. 5 Sorting Arrays (Cont. ) Figure 16. 37 Flag Quiz application running. Items now listed in alphabetical order © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Tutorial 16: Flag. Quiz. java // Quiz the user on their knowledge of flags. The user must try to // match five flags to their countries. import java. util. *; import java. awt. event. *; import javax. swing. border. *; public class Flag. Quiz extends JFrame { // array of country names private String[] countries = { "Russia", "China", "United States", "Italy", "Australia", "South Africa", "Brazil", "Spain" }; // boolean array tracks displayed flags private boolean[] flags. Used = new boolean[ countries. length ]; private int current. Index; // contains the index of current flag // tracks the number of flags that have been displayed private int count = 1; Outline 50 Creating an array of Strings to store country names Creating a Flag. Quiz. java boolean array (1 of 11) Variable current. Index stores the index of the flag being displayed Variable count stores the number of flags being displayed 2004 Prentice Hall, Inc. All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // JPanel and JLabel for displaying a flag image private JPanel flag. JPanel; private JLabel flag. Icon. JLabel; Outline // JLabel and JCombo. Box for choosing a country private JLabel select. Country. JLabel; private JCombo. Box select. Country. JCombo. Box; // JText. Field for giving the user feedback private JText. Field feedback. JText. Field; // JButton to submit an answer private JButton submit. JButton; Flag. Quiz. java (2 of 11) // JButton to display the next flag private JButton next. Flag. JButton; // no-argument constructor public Flag. Quiz() { create. User. Interface(); } 2004 Prentice Hall, Inc. All rights reserved. 51

47 48 49 50 51 52 53 54 55 56 57 58 59 60

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 // create and position GUI components; register event handlers private void create. User. Interface() { // get content pane for attaching GUI components Container content. Pane = get. Content. Pane(); Outline // enable explicit positioning of GUI components content. Pane. set. Layout( null ); // set up flag. JPanel = new JPanel(); flag. JPanel. set. Bounds( 16, 8, 100, 90 ); flag. JPanel. set. Layout( null ); flag. JPanel. set. Border( new Titled. Border( "Flag" ) ); content. Pane. add( flag. JPanel ); Flag. Quiz. java (3 of 11) // set up flag. Icon. JLabel = new JLabel(); flag. Icon. JLabel. set. Bounds( 10, 14, 80 ); flag. Icon. JLabel. set. Horizontal. Alignment( JLabel. CENTER ); flag. JPanel. add( flag. Icon. JLabel ); 2004 Prentice Hall, Inc. All rights reserved. 52

69 70 71 72 73 74 75 76 77 78 79 80 81 82

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 // set up select. Country. JLabel = new JLabel(); select. Country. JLabel. set. Bounds( 136, 8, 88, 21 ); select. Country. JLabel. set. Text( "Select country: " ); content. Pane. add( select. Country. JLabel ); Outline Arrays. sort( countries ); // sort the array // set up select. Country. JCombo. Box = new JCombo. Box( countries ); select. Country. JCombo. Box. set. Bounds( 136, 32, 135, 21 ); select. Country. JCombo. Box. set. Maximum. Row. Count( 3 ); content. Pane. add( select. Country. JCombo. Box ); display. Flag(); // display first flag // set up feedback. JText. Field = new JText. Field(); feedback. JText. Field. set. Bounds( 136, 64, 135, 32 ); feedback. JText. Field. set. Horizontal. Alignment( JText. Field. CENTER ); feedback. JText. Field. set. Editable( false ); content. Pane. add( feedback. JText. Field ); Alphabetizing country names in the array Customizing the Flag. Quiz. java select. Country (4 of 11) JCombo. Box Displaying the initial flag 2004 Prentice Hall, Inc. All rights reserved. 53

93 94 95 96 97 98 99 100 101 102 103 104 105 106

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 // set up submit. JButton = new JButton(); submit. JButton. set. Bounds( 287, 8, 88, 32 ); submit. JButton. set. Text( "Submit" ); content. Pane. add( submit. JButton ); submit. JButton. add. Action. Listener( new Action. Listener() // anonymous inner class { // event handler called when submit. JButton is pressed public void action. Performed( Action. Event event ) { submit. JButton. Action. Performed( event ); } Outline Flag. Quiz. java (5 of 11) } // end anonymous inner class ); // end call to add. Action. Listener 2004 Prentice Hall, Inc. All rights reserved. 54

112 113 114 115 116 117 118 119 120 121 122 123 124 125

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 // set up next. Flag. JButton = new JButton(); next. Flag. JButton. set. Bounds( 287, 48, 88, 32 ); next. Flag. JButton. set. Text( "Next Flag" ); next. Flag. JButton. set. Enabled( false ); content. Pane. add( next. Flag. JButton ); next. Flag. JButton. add. Action. Listener( new Action. Listener() // anonymous inner class { // event handler called when next. Flag. JButton is pressed public void action. Performed( Action. Event event ) { next. Flag. JButton. Action. Performed( event ); } Outline Flag. Quiz. java (6 of 11) } // end anonymous inner class ); // end call to add. Action. Listener // set properties of application’s window set. Title( "Flag Quiz" ); // set title bar string set. Size( 390, 135 ); // set window size set. Visible( true ); // display window 2004 Prentice Hall, Inc. All rights reserved. 55

137 138 139 140 141 142 143 144 145 146 147 148 149 150

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 } // end method create. User. Interface // return an unused random number private int get. Unique. Random. Number() { Random generator = new Random(); int random. Number; // generate random numbers until unused flag is found do { random. Number = generator. next. Int( 8 ); } while ( flags. Used[ random. Number ] == true ); // indicate that flag has been used flags. Used[ random. Number ] = true; return random. Number; } // end method get. Unique. Random. Number Outline Object used to create random values Determining if a country’s flag has been displayed Flag. Quiz. java previously (7 of 11) Indicating that an unused flag will be displayed and returning the flag’s index for use 2004 Prentice Hall, Inc. All rights reserved. 56

159 160 161 162 163 164 165 166 167 168 169 170 171 172

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 // choose a flag and display it in the JLabel private void display. Flag() { current. Index = get. Unique. Random. Number(); // get an unused flag // create the path for that flag String country = ( String ) select. Country. JCombo. Box. get. Item. At( current. Index ); String country. Path = "images/" + country + ". png"; // set the flag. Icon. JLabel to display the flag. Icon. JLabel. set. Icon( new Image. Icon( country. Path ) ); } // end method display. Flag Outline Getting index of unused flag Retrieving the flag’s corresponding Flag. Quiz. java country name (8 of 11) Path name of flag images Displaying an unused flag 2004 Prentice Hall, Inc. All rights reserved. 57

174 175 176 177 178 179 180 181 182 183 184 185 186 187

174 175 176 177 178 179 180 181 182 183 184 185 186 187 // check the answer and update the quiz private void submit. JButton. Action. Performed( Action. Event event ) { // determine whether the answer was correct if ( select. Country. JCombo. Box. get. Selected. Index() == current. Index ) { feedback. JText. Field. set. Text( "Correct!" ); } else // if an incorrect answer is given { feedback. JText. Field. set. Text( "Sorry, incorrect. " ); } Outline Retrieving the user’s answer and displaying feedback Flag. Quiz. java (9 of 11) 2004 Prentice Hall, Inc. All rights reserved. 58

188 189 190 191 192 193 194 195 196 197 198 199 200 201

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 // inform user if quiz is over if ( count == 5 ) { feedback. JText. Field. set. Text( feedback. JText. Field. get. Text() + " Done!" ); next. Flag. JButton. set. Enabled( false ); submit. JButton. set. Enabled( false ); select. Country. JCombo. Box. set. Enabled( false ); } else // if less than 5 flags have been displayed { submit. JButton. set. Enabled( false ); next. Flag. JButton. set. Enabled( true ); } Outline Determining if the quiz is over Flag. Quiz. java (10 of 11) } // end method submit. JButton. Action. Performed // display next flag in the quiz private void next. Flag. JButton. Action. Performed( Action. Event event ) { display. Flag(); // display next flag count++; Displaying the next flag for the user to identify 2004 Prentice Hall, Inc. All rights reserved. 59

211 // reset GUI components to initial states 212 feedback. JText. Field. set. Text(

211 // reset GUI components to initial states 212 feedback. JText. Field. set. Text( "" ); 213 select. Country. JCombo. Box. set. Selected. Index( 0 ); 214 submit. JButton. set. Enabled( true ); 215 next. Flag. JButton. set. Enabled( false ); 216 217 } // end method next. Flag. JButton. Action. Performed 218 219 // main method 220 public static void main( String args[] ) 221 { 222 Flag. Quiz application = new Flag. Quiz(); 223 application. set. Default. Close. Operation( JFrame. EXIT_ON_CLOSE ); 224 225 } // end method main 226 227 } // end class Flag. Quiz Outline Setting the JCombo. Box to display its first item Flag. Quiz. java (11 of 11) 2004 Prentice Hall, Inc. All rights reserved. 60