Write an Anagram Game application that contains an array of preset words. The game should randomly select a word and scramble its letters (Fig. 23.19). The first letter is extracted and placed back in the String at a random location. This process is repeated 20 times to ensure that the String is sufficiently scrambled. A JLa- bel displays the scrambled word for the user to guess. If the user guesses correctly, display a message and repeat the process with a different word. If the guess is incorrect, display a mes- sage and let the user try again.
a) Copying the template to your working directory. Copy the C:Examples Tutorial23ExercisesAnagram directory to your C:SimplyJava directory.
b) Opening the template file. Open the Anagram.java file in your text editor.
c) Adding a for statement to the generateAnagram method. The generateAnagram method (lines 133–145) selects a String from a predefined array and scrambles this String for the user. The String to be scrambled is stored in variable scrambled for you. A random index in this String has also been generated for you and stored in variable randomIndex. In this exercise, you will be completing only the generate- Anagram method—the rest of the application has been provided for you. After the variable declarations inside the generateAnagram method, add an empty for state- ment that will loop 20 times.
d) Generating the scrambled word. Inside the for statement you added in Step c, declare char firstCharacter and assign to it the first character in scrambled. Use the su
```
1 // Anagram.java
2 // Application provides an anagram game to users.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import java.util.Random;
7 import javax.swing.*;
8
9 public class Anagram extends JFrame
10 {
11 // JTextField for scrambled word
12 private JTextField anagramJTextField;
13
14 // JLabel and JTextField for guess from user
15 private JLabel guessJLabel;
16 private JTextField guessJTextField;
17
18 // JButton to allow user to submit guess
19 private JButton submitJButton;
20
21 // JTextField for correct or incorrect message
22 private JTextField resultJTextField;
23
24 // array of words to be scrambled
25 private String anagramList[] = { "components", "events",
26 "properties", "visual", "program", "application", "basic",
27 "debugger", "database", "files", "inheritance", "assembly",
28 "multimedia", "procedures", "functions", "arrays", "strings",
29 "collections", "integration", "structures" };
30
31 private int randomNumber; // random index variable
32 Random randomGenerator = new Random(); // instance of class Random
33
34 // no-argument constructor
35 public Anagram()
36 {
37 createUserInterface();
38 }
39
40 // create and position GUI components; register event handlers
41 private void createUserInterface()
42 {
43 // get content pane for attaching GUI components
44 Container contentPane = getContentPane();
45
46 // enable explicit positioning of GUI components
47 contentPane.setLayout( null );
48
49 // set up anagramJTextField
50 anagramJTextField = new JTextField();
51 anagramJTextField.setBounds( 16, 16, 148, 21 );
52 anagramJTextField.setEditable( false );
53 anagramJTextField.setHorizontalAlignment( JTextField.CENTER );
54 contentPane.add( anagramJTextField );
55
56 // set up guessJLabel
57 guessJLabel = new JLabel();
58 guessJLabel.setBounds( 16, 72, 72, 21 );
59 guessJLabel.setText( "Your guess:" );
60 contentPane.add( guessJLabel );
61
62 // set up guessJTextField
63 guessJTextField = new JTextField();
64 guessJTextField.setBounds( 16, 96, 148, 21 );
65 contentPane.add( guessJTextField );
66
67 // set up submitJButton
68 submitJButton = new JButton();
69 submitJButton.setBounds( 48, 136, 74, 23 );
70 submitJButton.setText( "Submit" );
71 contentPane.add( submitJButton );
72 submitJButton.addActionListener(
73
74 new ActionListener() // anonymous inner class
75 {
76 // event handler called when submitJButton is pressed
77 public void actionPerformed( ActionEvent event )
78 {
79 submitJButtonActionPerformed( event );
80 }
81
82 } // end anonymous inner class
83
84 ); // end call to addActionListener
85
86 // set up resultJTextField
87 resultJTextField = new JTextField();
88 resultJTextField.setBounds( 16, 176, 148, 21 );
89 resultJTextField.setEditable( false );
90 resultJTextField.setHorizontalAlignment( JTextField.CENTER );
91 contentPane.add( resultJTextField );
92
93 // set properties of application’s window
94 setTitle( "Anagram Game" ); // set title bar string
95 setSize( 188, 240 ); // set window size
96 setVisible( true ); // display window
97
98 generateAnagram(); // generate first word
99
100 } // end method createUserInterface
101
102 // check if guess is correct
103 private void submitJButtonActionPerformed( ActionEvent event )
104 {
105 // get text from guessJTextField
106 String text = guessJTextField.getText();
107
108 // if answer is correct
109 if ( text.equals( anagramList[ randomNumber ] ) )
110 {
111 resultJTextField.setText( "You are Correct!" );
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 } // end method submitJButtonActionPerformed
131
132 // scramble random word and display it in anagramJTextField
133 private void generateAnagram()
134 {
135 // generate new random number
136 randomNumber = randomGenerator.nextInt( 20 );
137
138 // select new word from array with random index
139 String scrambled = anagramList[ randomNumber ];
140
141 // generate new random index
142 int randomIndex =
143 randomGenerator.nextInt( scrambled.length() );
144
145 // loop to generate scrambled word
146 for ( int counter = 0; counter < 20; counter++ )
147 {
148 // extract first character to be added at a new location
149 char firstCharacter = scrambled.charAt( 0 );
150
151 // remove character from beginning of String
152 scrambled = scrambled.substring( 1 );
153
154 // temporary Strings used to swap characters
155 String temporary1 = scrambled.substring( 0, randomIndex );
156 String temporary2 = scrambled.substring( randomIndex );
157 temporary1 += String.valueOf( firstCharacter );
158 scrambled = temporary1 + temporary2;
159
160 // new random index
161 randomIndex = randomGenerator.nextInt( scrambled.length() );
162 }
163
164 // display scrambled word
165 anagramJTextField.setText( scrambled );
166
167 } // end method generateAnagram
168
169 //
170 public static void main( String[] args )
171 {
172 Anagram application = new Anagram();
173 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
168
169 //
170 public static void main( String[] args )
171 {
172 Anagram application = new Anagram();
173 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
168
169 //
170 public static void main( String[] args )
171 {
172 Anagram application = new Anagram();
173 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
174
175 } // end method main
176
177 } // end class Anagram
```
You might also like to view...
In simple terms, cryptography involves a(n) ____ algorithm.
A. conversion B. diversion C. communication D. encryption
When you select a Windows-based device within Microsoft Intune, you can…
A) View properties, link users, create groups from the selection, and run a full malware scan. B) Increase policy use for the users within your deployment groups. C) Increase bandwidth for the Wi-Fi and networking throughput. D) Remotely lock and reset the console. E) Restart the computer and deploy Office and Microsoft Dynamics CRM Online. F) A and E
?Wrapping refers to the practice of storing backup media away from the main business location, in order to mitigate the risk of a catastrophic disaster, such as a flood, fire, or earthquake.
Answer the following statement true (T) or false (F)
What steps are involved in TCP's "three-way handshake"?
What will be an ideal response?