Develop an application that allows a user to decrypt a secret message (a string of numbers). The user should enter each number of the message one at a time in a JTextField. When the Decrypt JButton is clicked, the number should be decrypted to a letter. That letter should then be appended to the Decrypted message: JTextField. When the user enters the numbers 39, 79, 79, 68, 0, 55, 79, 82, 75, 1 in order, your application should appear as in Fig. 14.19.



a) Copying the template to your working directory. Copy the C:Exam- plesTutorial14ExercisesDecryption directory to your C:SimplyJava direc- tory.

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

c) Adding an instance variable. After the last GUI component is declared (line 18), add a declaration for a private String named message which will hold the decrypted message. Initialize message to the empty string. Use one line for a com- ment.

d) Storing the user input. Add code in the decryptJButtonActionPerformed method that will store the user input in an int variable named encryptedLetter. The rest of the code you will add in this exercises should be placed in the decryptJButtonAc- tionPerformed method.

e) Testing the user input. Your application should only accept user input in the range 0 to 94. Add an if statement that will test whether the user’s input is in the accepted range of values.

f) Decrypting the input. Add

```
1 // Decryption.java
2 // This application decrypts encrypted data one character at a time.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class Decryption extends JFrame
8 {
9 // JLabel and JTextField for encrypted letter
10 private JLabel encryptedLetterJLabel;
11 private JTextField encryptedLetterJTextField;
12
13 // JButton to decrypt letter
14 private JButton decryptJButton;
15
16 // JLabel and JTextField for message
17 private JLabel messageJLabel;
18 private JTextField messageJTextField;
19
20 // String to hold decrypted message
21 private String message = "";
22
23 // no-argument constructor
24 public Decryption()
25 {
26 createUserInterface();
27 }
28
29 // create and position GUI components; register event handlers
30 private void createUserInterface()
31 {
32 // get content pane for attaching GUI components
33 Container contentPane = getContentPane();
34
35 // enable explicit positioning of GUI components
36 contentPane.setLayout( null );
37
38 // set up encryptedLetterJLabel
39 encryptedLetterJLabel = new JLabel();
40 encryptedLetterJLabel.setBounds( 16, 16, 150, 21 );
41 encryptedLetterJLabel.setText( "Encrypted letter:" );
42 contentPane.add( encryptedLetterJLabel );
43
44 // set up encryptedLetterJTextField
45 encryptedLetterJTextField = new JTextField();
46 encryptedLetterJTextField.setBounds( 142, 16, 43, 24 );
47 encryptedLetterJTextField.setHorizontalAlignment(
48 JTextField.RIGHT );
49 contentPane.add( encryptedLetterJTextField );
50
51 // set up decryptJButton
52 decryptJButton = new JButton();
53 decryptJButton.setBounds( 229, 16, 86, 24 );
54 decryptJButton.setText( "Decrypt" );
55 contentPane.add( decryptJButton );
56 decryptJButton.addActionListener(
57
58 new ActionListener() // anonymous inner class
59 {
60 // event handler called when decryptJButton is clicked
61 public void actionPerformed ( ActionEvent event )
62 {
63 decryptJButtonActionPerformed( event );
64 }
65
66 } // end anonymous inner class
67
68 ); // end call to addActionListener
69
70 // set up messageJLabel
71 messageJLabel = new JLabel();
72 messageJLabel.setBounds( 16, 56, 150, 21 );
73 messageJLabel.setText( "Decrypted message:" );
74 contentPane.add( messageJLabel );
75
76 // set up messageJTextField
77 messageJTextField = new JTextField();
78 messageJTextField.setBounds( 142, 56, 173, 24 );
79 messageJTextField.setHorizontalAlignment( JTextField.CENTER );
80 messageJTextField.setEditable( false );
81 contentPane.add( messageJTextField );
82
83 // set properties of application’s window
84 setTitle( "Decryption" ); // set title bar string
85 setSize( 335, 125 ); // set window size
86 setVisible( true ); // display window
87
88 } // end method createUserInterface
89
90 // decrypt letter and append to output
91 public void decryptJButtonActionPerformed( ActionEvent event )
92 {
93 // get user input
94 int encryptedLetter = Integer.parseInt(
95 encryptedLetterJTextField.getText() );
96
97 // if the encrypted letter is valid
98 if ( encryptedLetter >= 0 && encryptedLetter <= 94 )
99 {
100 // decrypt encryptedLetter
101 message += ( char ) ( encryptedLetter + 32 );
102
103 // append letter to messageJTextField messageJTextField.setText( message );
104
105 } // end if
106
107
108 // clear user input
109 encryptedLetterJTextField.setText( "" );
110
111 } // end method decryptJButtonActionPerformed
112
113 // main method
114 public static void main( String[] args )
115 {
116 Decryption application = new Decryption();
117 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
118
119 } // end method main
120
121 } // end class Decryption
```

Computer Science & Information Technology

You might also like to view...

What is a pro and a con of using the ADT list to represent a queue?

What will be an ideal response?

Computer Science & Information Technology

Excel worksheets are saved in a(n) _____-dimensional workbook

A. three B. two C. one D. multi

Computer Science & Information Technology