simplyjava1_17

Download Report

Transcript simplyjava1_17

Tutorial 17 – Student Grades
Application
Introducing Two-Dimensional Arrays and
JRadioButtons
Outline
17.1
17.2
17.3
17.4
17.5
Test-Driving the Student Grades Application
Two-Dimensional Arrays
Using JRadioButtons
Inserting Code into the Student Grades Application
Wrap-Up
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
1
2
Objectives
• In this tutorial, you will learn to:
– Differentiate between one-dimensional and twodimensional arrays.
– Declare and manipulate two-dimensional arrays.
– Understand applications of two-dimensional arrays.
– Use JRadioButtons to enable users to select only one
option out of many.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
3
17.1 Test-Driving the Student Grades
Application
Application Requirements
A teacher issues three tests to a class of ten students. The grades on these tests
are integers in the range from 0 to 100. The teacher has asked you to develop
an application to keep track of each student’s average and the average of the
class as a whole. The teacher has also asked that there be a choice to view the
grades as either numbers or letters. Letter grades should be calculated
according to the grading system:
90-100
A
80-89
B
70-79
C
60-69
D
Below 60
F
The application should allow a user to input the student’s name and three test
grades, then compute each student’s average and the class average. The
application should display number grades by default.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
4
17.1 Test-Driving the Student Grades
Application (Cont.)
• Running the completed application
– Open a Command Prompt
• Change to StudentGrades directory
• Type java StudentGrades
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
5
17.1
Figure 17.1
Test-Driving the Student Grades
Application (Cont.)
Running the completed Student Grades application.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
6
17.1
Test-Driving the Student Grades
Application (Cont.)
Figure 17.2
Inputting data to the Student Grades application.
• Enter test data in application, then click Submit Grades
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
7
17.1
Test-Driving the Student Grades
Application (Cont.)
Figure 17.3
Numeric JRadioButton
selected as the default
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Displaying the student’s numerical grade.
8
17.1
Test-Driving the Student Grades
Application (Cont.)
Figure 17.4
Displaying the student’s resulting letter grade.
Select the Letter
JRadioButton
• Select Letter JRadioButton to make the grades appear in
letter format instead of number format
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
9
17.2
Two-Dimensional Arrays
• Use two indices to identify elements
• Used to represent tables of values
– m-by-n array
• m rows
• n columns
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
10
17.2
Figure 17.5
Two-Dimensional Arrays (Cont.)
Two-dimensional array with three rows and four columns.
Column 0
Row 0
Row 1
Row 2
Column 1
Column 2
Column 3
myArray[0][0]
myArray[0][1]
myArray[0][2]
myArray[0][3]
myArray[1][0]
myArray[1][1]
myArray[1][2]
myArray[1][3]
myArray[2][0]
myArray[2][1]
myArray[2][2]
myArray[2][3]
Column index (or subscript)
Row index (or subscript)
Array name
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
11
17.3
Using JRadioButtons
• JRadioButton
– A small gray circle that is either blank (unselected) or filled
with a smaller black dot (selected)
– State button
• Only has “on” state or “off” state
– Grouped in a ButtonGroups
• Only one button in a group can be on at one time
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
12
17.3
Using JRadioButtons (Cont.)
When the user clicks the Submit Grades JButton
Retrieve the student’s name and grades from the JTextFields
Add the student’s information to the arrays
Display each student’s name, test grades and average in the JTextArea
Display the class’s average in the Class average: JTextField
Clear the student’s name and grades from the JTextFields
If 10 students have been entered, disable the Submit Grades JButton
When the user selects the Numeric JRadioButton
Display each student’s name, numeric test grades and numeric average in the JTextArea
Display the class’s numeric average in the Class average: JTextField
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
13
17.3
Using JRadioButtons (Cont.)
When the user selects the Letter JRadioButton
Display each students’ name, letter test grades and letter average in the JTextArea
Display the class’s letter average in the Class average: JTextField
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
14
17.3
Using JRadioButtons (Cont.)
Action
Label the application’s
components
Retrieve student’s name and
grades from the JTextFields
Add the student’s information
to the arrays
Display each student’s name,
test grades and average in the
JTextArea
Display the class’s average in
the Class average: JTextField
Clear the student’s name and
grades from the JTextFields
Component/Object
Event/Method
studentNameJLabel,
test1JLabel,
test2JLabel,
test3JLabel,
displayJLabel,
classAverageJLabel,
inputGradeJPanel
studentNameJTextField,
test1JTextField,
test2JTextField,
test3JTextField
User clicks
Submit
Grades
JButton
studentNames,
studentGrades
displayJTextArea
classAverageJTextField
studentNameJTextField,
test1JTextField,
test2JTextField,
test3JTextField
submitGradesJButton
If 10 students have been
entered, disable the Submit
Grades JButton
Figure 17.6 ACE table for the Student Grades application. (Part 1 of 2)
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
15
17.3
Using JRadioButtons (Cont.)
Action
Component/Object
Event/Method
displayJTextArea
Display each student’s name,
User selects
numeric test grades and
Numeric
JRadioButton
numeric average in the
JTextArea
classAverageJTextField
Display the class’s numeric
average in the Class average:
JTextField
displayJTextArea
Display each students’ name,
User selects
letter test grades and letter
Letter
JRadioButton
average in the JTextArea
classAverageJTextField
Display the class’s letter
average in the Class average:
JTextField
Figure 17.6 ACE table for the Student Grades application. (Part 2 of 2)
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
16
17.3
Using JRadioButtons (Cont.)
Figure 17.7
Customizing numericJRadioButton.
Setting the properties of
numericJRadioButton
• text property sets the text displayed next to the JRadioButton
• selected property controls whether button is initially on or off
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
17
17.3
Using JRadioButtons (Cont.)
Figure 17.8 Customizing letterJRadioButton.
Setting the properties of
letterJRadioButton
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
18
17.3
Figure 17.9
Using JRadioButtons (Cont.)
Adding numericJRadioButton to the ButtonGroup.
Calling ButtonGroup method
add to add a JRadioButton
• Use method add to add a JRadioButton to a ButtonGroup
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
19
17.3
Figure 17.10
Using JRadioButtons (Cont.)
Adding letterJRadioButton to the ButtonGroup.
Calling ButtonGroup method
add to add a JRadioButton
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
20
17.3
Figure 17.11
Using JRadioButtons (Cont.)
Running the application after adding the JRadioButtons.
• Save the changes to your code
• Compile and run in the Command Prompt
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
21
17.4
Inserting Code into the Student Grades
Application
Figure 17.12
Store the student name and
test grades in the arrays
Increment studentCount
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Storing the input.
22
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.13
Calling method
displayNumericGrades
Calling method
displayLetterGrades
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Displaying the output.
23
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.14 Application does not allow more than ten data entries.
Disable submitGradesJButton
when 10 grades have been entered
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
24
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.15
Adding a header to displayJTextArea.
Displaying a header
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
25
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.16
Appending the student’s name to displayJTextArea.
Outputting the student’s name
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
26
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.17 Outputting each test grade and calculating the student’s total grade.
Output each test grade
Add the test grade to
the student’s total
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
27
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.18
Calculating and displaying the student’s average grade.
Add the student’s total
to the class’s total
Calculate and display
the student’s average
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
28
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.19 Calculating and displaying the class’s average grade.
Calculate and display
the class’s average
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
29
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.20
Displaying a header
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Outputting a header.
30
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.21
Outputting the student’s name
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Outputting each student’s name.
31
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.22 Outputting the student’s letter grades
and calculating the student’s total grade.
Convert the test
grade to a letter
Add the test grade to
the student’s total
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
32
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.23
Calculating and displaying the student’s average letter grade.
Add the student’s total
to the class’s total
Convert student’s average
to a letter grade
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
33
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.24 Calculating and displaying the class’s average letter grade.
Convert class’s average
to a letter grade
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
34
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.25
Running the application after declaring new methods.
• Save the changes to your code
• Compile and run in the Command Prompt
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
35
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.26
numericJRadioButton’s actionPerformed event handler.
Coding an event
handler for the
numericJRadioButton
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
36
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.27
letterJRadioButton’s actionPerformed event handler.
Coding an event
handler for the
letterJRadioButton
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
37
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.28
Method numericJRadioButtonActionPerformed.
Calling method
displayNumericGrades
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
38
17.4
Inserting Code into the Student Grades
Application (Cont.)
Figure 17.29 Method letterJRadioButtonActionPerformed.
Calling method
displayLetterGrades
• Save the changes to your code
• Compile and run in the Command Prompt
© 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
15
16
17
18
19
20
21
22
23
24
25
// Tutorial 17: StudentGrades.java
// This application computes each student's grade average and
// the class average for ten students.
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.border.*;
39
Outline
StudentGrades.java
(1 of 18)
public class StudentGrades extends JFrame
{
// JPanel for user inputs
private JPanel inputGradeJPanel;
// JLabel and JTextField for student name
private JLabel studentNameJLabel;
private JTextField studentNameJTextField;
// JLabel and JTextField for test 1 score
private JLabel test1JLabel;
private JTextField test1JTextField;
// JLabel and JTextField for test 2 score
private JLabel test2JLabel;
private JTextField test2JTextField;
 2004 Prentice Hall, Inc.
All rights reserved.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
40
// JLabel and JTextField for test 3 score
private JLabel test3JLabel;
private JTextField test3JTextField;
// JButton to calculate student and class average
private JButton submitGradesJButton;
Outline
StudentGrades.java
(2 of 18)
// ButtonGroup to control numeric and letter JRadioButtons
private ButtonGroup displayButtonGroup;
// JRadioButtons to choose to display numerically or as letters
private JRadioButton numericJRadioButton;
private JRadioButton letterJRadioButton;
// JLabel, JTextArea and JScrollPane to display students averages
private JLabel displayJLabel;
private JTextArea displayJTextArea;
// JLabel and JTextField to display the class average
private JLabel classAverageJLabel;
private JTextField classAverageJTextField;
// initialize number of students to zero
private int studentCount = 0;
 2004 Prentice Hall, Inc.
All rights reserved.
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
41
// constants
private final
private final
private final
private final
private final
Outline
int
int
int
int
int
NUMBER_OF_TESTS = 3;
MAXIMUM_STUDENTS = 10;
FIRST_TEST = 0;
SECOND_TEST = 1;
THIRD_TEST = 2;
StudentGrades.java
(3 of 18)
// one-dimensional array to store student names
private String studentNames[] = new String[ MAXIMUM_STUDENTS ];
// two-dimensional array to store student grades
private int studentGrades[][] =
new int[ MAXIMUM_STUDENTS ][ NUMBER_OF_TESTS ];
// DecimalFormat for two digits of precision
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
// no-argument constructor
public StudentGrades()
{
createUserInterface();
}
 2004 Prentice Hall, Inc.
All rights reserved.
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
42
Outline
StudentGrades.java
(4 of 18)
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up inputGradeJPanel
inputGradeJPanel = new JPanel();
inputGradeJPanel.setBounds( 16, 16, 208, 218 );
inputGradeJPanel.setBorder(
new TitledBorder( "Input Grade" ) );
inputGradeJPanel.setLayout( null );
contentPane.add( inputGradeJPanel );
// set up studentNameJLabel
studentNameJLabel = new JLabel();
studentNameJLabel.setBounds( 8, 32, 90, 23 );
studentNameJLabel.setText( "Student Name:" );
inputGradeJPanel.add( studentNameJLabel );
 2004 Prentice Hall, Inc.
All rights reserved.
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// set up studentNameJTextField
studentNameJTextField = new JTextField();
studentNameJTextField.setBounds( 104, 32, 88, 21 );
studentNameJTextField.setHorizontalAlignment(
JTextField.RIGHT );
inputGradeJPanel.add( studentNameJTextField );
43
Outline
StudentGrades.java
(5 of 18)
// set up test1JLabel
test1JLabel = new JLabel();
test1JLabel.setBounds( 8, 74, 60, 23 );
test1JLabel.setText( "Test 1:" );
inputGradeJPanel.add( test1JLabel );
// set up test1JTextField
test1JTextField = new JTextField();
test1JTextField.setBounds( 136, 74, 56, 21 );
test1JTextField.setHorizontalAlignment( JTextField.RIGHT );
inputGradeJPanel.add( test1JTextField );
// set up test2JLabel
test2JLabel = new JLabel();
test2JLabel.setBounds( 8, 98, 60, 23 );
test2JLabel.setText( "Test 2:" );
inputGradeJPanel.add( test2JLabel );
 2004 Prentice Hall, Inc.
All rights reserved.
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
// set up test2JTextField
test2JTextField = new JTextField();
test2JTextField.setBounds( 136, 98, 56, 21 );
test2JTextField.setHorizontalAlignment( JTextField.RIGHT );
inputGradeJPanel.add( test2JTextField );
44
Outline
StudentGrades.java
(6 of 18)
// set up test3JLabel
test3JLabel = new JLabel();
test3JLabel.setBounds( 8, 122, 60, 23 );
test3JLabel.setText( "Test 3:" );
inputGradeJPanel.add( test3JLabel );
// set up test3JTextField
test3JTextField = new JTextField();
test3JTextField.setBounds( 136, 122, 56, 21 );
test3JTextField.setHorizontalAlignment( JTextField.RIGHT );
inputGradeJPanel.add( test3JTextField );
// set up submitGradesJButton
submitGradesJButton = new JButton();
submitGradesJButton.setBounds( 72, 182, 120, 24 );
submitGradesJButton.setText( "Submit Grades" );
inputGradeJPanel.add( submitGradesJButton );
submitGradesJButton.addActionListener(
 2004 Prentice Hall, Inc.
All rights reserved.
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
new ActionListener() // anonymous inner class
{
// event handler called when submitGradesJButton is
// pressed
public void actionPerformed( ActionEvent event )
{
submitGradesJButtonActionPerformed( event );
}
45
Outline
StudentGrades.java
(7 of 18)
} // end anonymous inner class
); // end call to addActionListener
// set up displayButtonGroup
displayButtonGroup = new ButtonGroup();
// set up numericJRadioButton
numericJRadioButton = new JRadioButton();
numericJRadioButton.setBounds( 55, 244, 75, 23 );
numericJRadioButton.setText( "Numeric" );
numericJRadioButton.setSelected( true );
displayButtonGroup.add( numericJRadioButton );
contentPane.add( numericJRadioButton );
numericJRadioButton.addActionListener(
Setting the properties of
numericJRadioButton
Adding numericJRadioButton
to the displayButtonGroup
 2004 Prentice Hall, Inc.
All rights reserved.
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
new ActionListener() // anonymous inner class
{
// event handler called when numericJRadioButton is
// selected
public void actionPerformed( ActionEvent event )
{
numericJRadioButtonActionPerformed( event );
}
46
Outline
StudentGrades.java
(8 of 18)
} // end anonymous inner class
); // end call to addActionListener
// set up letterJRadioButton
letterJRadioButton = new JRadioButton();
letterJRadioButton.setBounds( 140, 244, 75, 23 );
letterJRadioButton.setText( "Letter" );
displayButtonGroup.add( letterJRadioButton );
contentPane.add( letterJRadioButton );
letterJRadioButton.addActionListener(
Setting the properties of
letterJRadioButton
Adding letterJRadioButton
to the displayButtonGroup
 2004 Prentice Hall, Inc.
All rights reserved.
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
new ActionListener() // anonymous inner class
{
// event handler called when letterJRadioButton is
// selected
public void actionPerformed( ActionEvent event )
{
letterJRadioButtonActionPerformed( event );
}
47
Outline
StudentGrades.java
(9 of 18)
} // end anonymous inner class
); // end call to addActionListener
// set up displayJLabel
displayJLabel = new JLabel();
displayJLabel.setBounds( 240, 16, 150, 23 );
displayJLabel.setText( "Average of each student:" );
contentPane.add( displayJLabel );
// set up displayJTextArea
displayJTextArea = new JTextArea();
displayJTextArea.setBounds( 240, 48, 402, 184 );
displayJTextArea.setEditable( false );
contentPane.add( displayJTextArea );
 2004 Prentice Hall, Inc.
All rights reserved.
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// set up classAverageJLabel
classAverageJLabel = new JLabel();
classAverageJLabel.setBounds( 490, 244, 96, 23 );
classAverageJLabel.setText( "Class average:" );
contentPane.add( classAverageJLabel );
48
Outline
StudentGrades.java
(10 of 18)
// set up classAverageJTextField
classAverageJTextField = new JTextField();
classAverageJTextField.setBounds( 586, 244, 56, 23 );
classAverageJTextField.setHorizontalAlignment(
JTextField.CENTER );
classAverageJTextField.setEditable( false );
contentPane.add( classAverageJTextField );
// set properties of application’s window
setTitle( "Student Grades" ); // set title-bar string
setSize( 670, 308 );
// set window size
setVisible( true );
// display window
} // end method createUserInterface
 2004 Prentice Hall, Inc.
All rights reserved.
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// convert a number to a letter grade
private String convertToLetterGrade( double grade )
{
if ( grade >= 90 )
{
return "A";
}
else if ( grade >= 80 )
{
return "B";
}
else if ( grade >= 70 )
{
return "C";
}
else if ( grade >= 60 )
{
return "D";
}
else
{
return "F";
}
49
Outline
StudentGrades.java
(11 of 18)
} // end method convertToLetterGrade
 2004 Prentice Hall, Inc.
All rights reserved.
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
50
// calculate and display the student and class average
private void submitGradesJButtonActionPerformed(
ActionEvent event )
{
// get user input
String nameOfStudent = studentNameJTextField.getText();
int test1 = Integer.parseInt( test1JTextField.getText() );
int test2 = Integer.parseInt( test2JTextField.getText() );
int test3 = Integer.parseInt( test3JTextField.getText() );
// add user input to arrays
studentNames[ studentCount ] =
studentGrades[ studentCount ][
studentGrades[ studentCount ][
studentGrades[ studentCount ][
Outline
StudentGrades.java
(12 of 18)
nameOfStudent;
FIRST_TEST ] = test1;
SECOND_TEST ] = test2;
THIRD_TEST ] = test3;
studentCount++; // increment studentCount
if ( numericJRadioButton.isSelected() )
{
displayNumericGrades();
}
 2004 Prentice Hall, Inc.
All rights reserved.
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
else
{
displayLetterGrades();
}
// clear other JTextFields for new data
studentNameJTextField.setText( "" );
test1JTextField.setText( "" );
test2JTextField.setText( "" );
test3JTextField.setText( "" );
51
Outline
StudentGrades.java
(13 of 18)
// if ten student grades have been entered
if ( studentCount == MAXIMUM_STUDENTS )
{
// disable submitGradesJButton
submitGradesJButton.setEnabled( false );
}
} // end method submitGradesJButtonActionPerformed
 2004 Prentice Hall, Inc.
All rights reserved.
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// display student grades and averages as numbers
private void displayNumericGrades()
{
// add a header to displayJTextArea
displayJTextArea.setText(
"Name\tTest 1\tTest 2\tTest 3\tAverage\n" );
52
Outline
StudentGrades.java
(14 of 18)
int studentTotal = 0; // store the student's total grades
int classTotal = 0;
// store the class's total grades
for ( int student = 0; student < studentCount; student++ )
{
// display student grades
displayJTextArea.append( studentNames[ student ] + "\t" );
studentTotal = 0; // initialize the student's total grades
for ( int test = 0; test < NUMBER_OF_TESTS; test++ )
{
// append each test grade to displayJTextArea
displayJTextArea.append(
studentGrades[ student ][ test ] + "\t" );
 2004 Prentice Hall, Inc.
All rights reserved.
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// add the test grade to the student's total
studentTotal += studentGrades[ student ][ test ];
} // end for
// add the student's total grade to the class's total
classTotal += studentTotal;
53
Outline
StudentGrades.java
(15 of 18)
// calculate the student average and display it
double studentAverage =
( double ) studentTotal / NUMBER_OF_TESTS;
displayJTextArea.append(
twoDigits.format( studentAverage ) + "\n" );
} // end for
// calculate the class average and display it
double classAverage =
( double ) classTotal / studentCount / NUMBER_OF_TESTS;
classAverageJTextField.setText(
twoDigits.format( classAverage ) );
} // end method displayNumericGrades
 2004 Prentice Hall, Inc.
All rights reserved.
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// display student grades and averages as letters
private void displayLetterGrades()
{
// add a header to displayJTextArea
displayJTextArea.setText(
"Name\tTest 1\tTest 2\tTest 3\tAverage\n" );
54
Outline
StudentGrades.java
(16 of 18)
int studentTotal = 0; // store the student's total grades
int classTotal = 0;
// store the class's total grades
for ( int student = 0; student < studentCount; student++ )
{
// display student grades
displayJTextArea.append( studentNames[ student ] + "\t" );
studentTotal = 0; // initialize the student's total grades
for ( int test = 0; test < NUMBER_OF_TESTS; test++ )
{
// append each test grade to displayJTextArea
displayJTextArea.append( convertToLetterGrade(
studentGrades[ student ][ test ] ) + "\t" );
// add the test grade to the student's total
studentTotal += studentGrades[ student ][ test ];
 2004 Prentice Hall, Inc.
All rights reserved.
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
55
} // end for
// add the student's total grade to the class's total
classTotal += studentTotal;
Outline
StudentGrades.java
(17 of 18)
// calculate the student average and display it
double studentAverage =
( double ) studentTotal / NUMBER_OF_TESTS;
displayJTextArea.append(
convertToLetterGrade( studentAverage ) + "\n" );
} // end for
// calculate the class average and display it
double classAverage =
( double ) classTotal / studentCount / NUMBER_OF_TESTS;
classAverageJTextField.setText(
convertToLetterGrade( classAverage ) );
} // end method displayLetterGrades
 2004 Prentice Hall, Inc.
All rights reserved.
402
// user selected numeric display
403
private void numericJRadioButtonActionPerformed(
404
ActionEvent event )
405
{
406
displayNumericGrades();
407
408
} // end method numericJRadioButtonActionPerformed
409
410
// user selected letter display
411
private void letterJRadioButtonActionPerformed(
412
ActionEvent event )
413
{
414
displayLetterGrades();
415
416
} // end method letterJRadioButtonActionPerformed
417
418
// main method
419
public static void main( String[] args )
420
{
421
StudentGrades application = new StudentGrades();
422
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
423
424
} // end method main
425
426 } // end class StudentGrades
56
Outline
StudentGrades.java
(18 of 18)
 2004 Prentice Hall, Inc.
All rights reserved.