(Modified Class Average Application) The Class Average application has a deficiency. The Average JButton is never disabled after the average is calculated the first time. Modify the Class Average application, so that the Get Grades JButton is disabled after the user enters 10 grades. The user’s only option then will be to click the Average JButton to display the average. Once the average is displayed, have the application disable the Average JButton, enable the Get Grades JButton and give the Get Grades JButton the application’s focus, so that the user may enter 10 new grades. See figure 9.23 below:



```

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

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

c) Modifying the getGradesJButtonActionPerformed event handler. Add code in lines 139–140 to disable the Get Grades JButton. Use line 139 for a comment, and line 140 to perform the disabling.

d) Modifying the averageJButtonActionPerformed event handler. Add code starting in line 150 that will disable the Average JButton, enable the Get Grades JButton and give the application’s focus to the Get Grades JButton.

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

f) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJavaModified- ClassAverage.

g) Compiling the


```
1 // ClassAverage.java
2 // Application enables user to have the average of grades calculated.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class ClassAverage extends JFrame
8 {
9 // JLabel and JTextArea for list of grades
10 private JLabel gradeListJLabel;
11 private JTextArea gradeListJTextArea;
12
13 // JButton initiates retrieving grades
14 private JButton getGradesJButton;
15
16 // JButton initiates calculating average
17 private JButton averageJButton;
18
19 // JLabel and JTextField used to display average
20 private JLabel classAverageJLabel;
21 private JTextField classAverageJTextField;
22
23 private int total = 0; // holds value of the grade total
24
25 // no-argument constructor
26 public ClassAverage()
27 {
28 createUserInterface();
29 }
30
31 // create and position GUI components; register event handlers
32 private void createUserInterface()
33 {
34 // get content pane for attaching GUI components
35 Container contentPane = getContentPane();
36
37 // enable explicit positioning of GUI components
38 contentPane.setLayout( null );
39
40 // set up gradeListJLabel
41 gradeListJLabel = new JLabel();
42 gradeListJLabel.setBounds( 16, 8, 70, 23 );
43 gradeListJLabel.setText( "Grade list:" );
44 contentPane.add( gradeListJLabel );
45
46 // set up gradeListJTextArea
47 gradeListJTextArea = new JTextArea();
48 gradeListJTextArea.setBounds( 16, 32, 88, 180 );
49 contentPane.add( gradeListJTextArea );
50
51 // set up getGradesJButton
52 getGradesJButton = new JButton();
53 getGradesJButton.setBounds( 128, 50, 100, 26 );
54 getGradesJButton.setText( "Get Grades" );
55 contentPane.add( getGradesJButton );
56 getGradesJButton.addActionListener(
57
58 new ActionListener() // anonymous inner class
59 {
60 // event handler called when getGradesJButton is clicked
61 public void actionPerformed( ActionEvent event )
62 {
63 getGradesJButtonActionPerformed( event );
64 }
65
66 } // end anonymous inner class
67
68 ); // end call to addActionListener
69
70 // set up averageJButton
71 averageJButton = new JButton();
72 averageJButton.setBounds( 128, 90, 100, 26 );
73 averageJButton.setText( "Average" );
74 averageJButton.setEnabled( false );
75 contentPane.add( averageJButton );
76 averageJButton.addActionListener(
77
78 new ActionListener() // anonymous inner class
79 {
80 // event handler called when averageJButton is clicked
81 public void actionPerformed( ActionEvent event )
82 {
83 averageJButtonActionPerformed( event );
84 }
85
86 } // end anonymous inner class
87
88 ); // end call to addActionListener
89
90 // set up classAverageJLabel
91 classAverageJLabel = new JLabel();
92 classAverageJLabel.setBounds( 128, 132, 90, 23 );
93 classAverageJLabel.setText( "Class average:" );
94 contentPane.add( classAverageJLabel );
95
96 // set up classAverageJTextField
97 classAverageJTextField = new JTextField();
98 classAverageJTextField.setBounds( 128, 156, 100, 21 );
99 classAverageJTextField.setEditable( false );
100 classAverageJTextField.setHorizontalAlignment(
101 JTextField.CENTER );
102 contentPane.add( classAverageJTextField );
103
104 // set properties of application’s window
105 setTitle( "Class Average" ); // set title bar text
106 setSize( 250, 250 ); // set window size
107 setVisible( true ); // display window
108
109 } // end method createUserInterface
110
111 // method retrieves, totals, and displays grades from user
112 private void getGradesJButtonActionPerformed( ActionEvent event )
113 {
114 total = 0; // stores total of grades entered
115 int counter = 1; // counter controls do...while statement
116 String input; // stores data entered into input dialog
117 int grade; // stores int value converted from input
118
119 // clear previous grades and calculation result
120 gradeListJTextArea.setText( "" );
121 classAverageJTextField.setText( "" );
122
123 do
124 {
125 // get user input
126 input = JOptionPane.showInputDialog( null, "Enter Grade" );
127 grade = Integer.parseInt( input );
128
129 // add text to output
130 gradeListJTextArea.append( grade + "\n" );
131 total += grade; // add input to total
132 counter++; // increment counter
133 }
134 while ( counter <= 10 ); // end do...while
135
136 averageJButton.setEnabled( true ); // enable averageJButton
137 averageJButton.requestFocusInWindow(); // transfer focus
138
139
140
141

// disable getGradesJButton getGradesJButton.setEnabled( false );
142 } // end method getGradesJButtonActionPerformed
143
144 // method calculates average of grades entered
145 private void averageJButtonActionPerformed( ActionEvent event )
146 {
147 double average = ( double ) total / 10; // calculate average
148 classAverageJTextField.setText( String.valueOf( average ) );
149
150
151
152
153
154
155
156
157
158

// disable averageJButton averageJButton.setEnabled( false );

// enable getGradesJButton getGradesJButton.setEnabled( true );

// transfer focus to getGradesJButton getGradesJButton.requestFocusInWindow();
159 } // end method averageJButtonActionPerformed
160
161 // main method
162 public static void main( String[] args )
163 {
164 ClassAverage application = new ClassAverage();
165 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
166
167 } // end method main
168
169 } // end class ClassAverage
```

Computer Science & Information Technology

You might also like to view...

e) The file produced by the Java compiler contains _________ that are executed by the Java Virtual Machine.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

A ____ is a darkened shade in an image.

a. shadow b. highlight c. gamma d. midtone

Computer Science & Information Technology

To open the Research task pane, click the Review tab on the Ribbon, and then, in the ____ group, click the Research button.

A. Search B. Editing C. Proofing D. Help

Computer Science & Information Technology

In older laptop screens, the backlight is powered by which of the following?

A. A CCFL lamp B. A gas C. LEDs D. OLED

Computer Science & Information Technology