(Class Average Application That Handles Any Number of Grades) Modify the original Class Average application to handle any number of grades. When the user clicks the Get Grades JButton, an input dialog will ask the user to input the number of grades to be entered. The application should execute as it did in the test-drive , except that each time the Get Grades JButton is clicked the input dialog will appear as many times as the number of grades the user wishes to enter.



```

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

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

c) Declaring an instance variable. Add code in line 26 that declares int variable

limit. This variable will store the number of grades the user wishes to enter.

d) Modifying the getGradesJButtonActionPerformed event handler. Add code in lines 130–131 (before the do…while statement) that will display an input dialog for the user. Use two lines for readability. Set the text displayed in the input dialog to “How many grades will be entered?”. Assign the result of this input dialog to vari- able input. Add code in line 132 that will convert the value of input to an integer, and assign the result to the instance variable limit.

e) Modifying the do…while statement in getGradesJButtonActionPerformed. Mod- ify th


```
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 // JScrollPane adds scrollbars to JTextArea
11 private JLabel gradeListJLabel;
12 private JTextArea gradeListJTextArea;
13 private JScrollPane gradeListJScrollPane;
14
15 // JButton initiates retrieving grades
16 private JButton getGradesJButton;
17
18 // JButton initiates calculating average
19 private JButton averageJButton;
20
21 // JLabel and JTextField used to display average
22 private JLabel classAverageJLabel;
23 private JTextField classAverageJTextField;
24
25 private int total = 0; // holds value of the grade total
26 private int limit; // holds the number of grades
27
28 // no-argument constructor
29 public ClassAverage()
30 {
31 createUserInterface();
32 }
33
34 // create and position GUI components; register event handlers
35 private void createUserInterface()
36 {
37 // get content pane for attaching GUI components
38 Container contentPane = getContentPane();
39
40 // enable explicit positioning of GUI components
41 contentPane.setLayout( null );
42
43 // set up gradeListJLabel
44 gradeListJLabel = new JLabel();
45 gradeListJLabel.setBounds( 16, 8, 70, 23 );
46 gradeListJLabel.setText( "Grade list:" );
47 contentPane.add( gradeListJLabel );
48
49 // set up gradeListJTextArea
50 gradeListJTextArea = new JTextArea();
51 contentPane.add( gradeListJTextArea );
52
53 // set up gradeListJScrollPane
54 gradeListJScrollPane = new JScrollPane( gradeListJTextArea );
55 gradeListJScrollPane.setBounds( 16, 32, 88, 180 );
56 contentPane.add( gradeListJScrollPane );
57
58 // set up getGradesJButton
59 getGradesJButton = new JButton();
60 getGradesJButton.setBounds( 128, 50, 100, 26 );
61 getGradesJButton.setText( "Get Grades" );
62 contentPane.add( getGradesJButton );
63 getGradesJButton.addActionListener(
64
65 new ActionListener() // anonymous inner class
66 {
67 // event handler called when getGradesJButton is clicked
68 public void actionPerformed( ActionEvent event )
69 {
70 getGradesJButtonActionPerformed( event );
71 }
72
73 } // end anonymous inner class
74
75 ); // end call to addActionListener
76
77 // set up averageJButton
78 averageJButton = new JButton();
79 averageJButton.setBounds( 128, 90, 100, 26 );
80 averageJButton.setText( "Average" );
81 averageJButton.setEnabled( false );
82 contentPane.add( averageJButton );
83 averageJButton.addActionListener(
84
85 new ActionListener() // anonymous inner class
86 {
87 // event handler called when averageJButton is clicked
88 public void actionPerformed( ActionEvent event )
89 {
90 averageJButtonActionPerformed( event );
91 }
92
93 } // end anonymous inner class
94
95 ); // end call to addActionListener
96
97 // set up classAverageJLabel
98 classAverageJLabel = new JLabel();
99 classAverageJLabel.setBounds( 128, 132, 90, 23 );
100 classAverageJLabel.setText( "Class average:" );
101 contentPane.add( classAverageJLabel );
102
103 // set up classAverageJTextField
104 classAverageJTextField = new JTextField();
105 classAverageJTextField.setBounds( 128, 156, 100, 21 );
106 classAverageJTextField.setEditable( false );
107 classAverageJTextField.setHorizontalAlignment(
108 JTextField.CENTER );
109 contentPane.add( classAverageJTextField );
110
111 // set properties of application’s window
112 setTitle( "Class Average" ); // set title bar text
113 setSize( 250, 250 ); // set window size
114 setVisible( true ); // display window
115
116 } // end method createUserInterface
117
118 // method retrieves, totals, and displays grades from user
119 private void getGradesJButtonActionPerformed( ActionEvent event )
120 {
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 } // end method getGradesJButtonActionPerformed
154
155 // method calculates average of grades entered
156 private void averageJButtonActionPerformed( ActionEvent event )
157 {
158 double average = ( double ) total / limit; // calculate average
159 classAverageJTextField.setText( String.valueOf( average ) );
160
161
162 // disable averageJButton averageJButton.setEnabled( false );
163
164
165 // enable getGradesJButton getGradesJButton.setEnabled( true );
166
167
168 // transfer focus to getGradesJButton getGradesJButton.requestFocusInWindow();
169
170 } // end method averageJButtonActionPerformed
171
172 //
173 public static void main( String[] args )
174 {
175 ClassAverage application = new ClassAverage();
176 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
177
178 } // end method main
179
180 } // end class ClassAverage
```

Computer Science & Information Technology

You might also like to view...

How is monitoring achieved by the SCO?

What will be an ideal response?

Computer Science & Information Technology

What happens to Windows Command Processor and Console Window Host when the command prompt window is closed?

What will be an ideal response?

Working in the Processes Tab
a. Open a command prompt and a web browser.
Microsoft Edge is used in this lab; however, any web browser will work. Just substitute your browser name whenever you see Microsoft Edge.
b. Right-click the Task bar to open Task Manager. Another way to open the Task Manager is to press Ctrl-Alt-Delete to access the Windows Security screen and select Task Manager.
c. Click More details to see all the processes that are listed in the Processes tab.

d. Expand the Windows Command Processor heading.
e. There are three categories of processes listed in the Processes tab: Apps, Background processes, and Windows processes.
? The Apps are the applications that you have opened, such as Microsoft Edge, Task Manager, and Windows Command Processor, as shown in the figure above. Other applications that are opened by the users, such as web browsers and email clients, will also be listed here.
? The Background processes are execut

Computer Science & Information Technology

To indicate possible grammar errors, Word flags text with:

a. blue wavy underlines b. green wavy underlines c. red wavy underlines

Computer Science & Information Technology

Ken is helping Mary troubleshoot her printer problems. Mary wants to stop the document from being printed but leave it in the print queue. Ken tells her to use the ____ command.

A. Resume B. Restart C. Pause D. Cancel

Computer Science & Information Technology