Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application 17.2 Two-Dimensional Arrays 17.3 Using JRadioButton s 17.4 Inserting Code into the Student Grades Application 17.5 Wrap-Up Tutorial 17 – Student Grades Application Introducing Two-Dimensional Arrays and JRadioButton s

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Differentiate between one-dimensional and two- dimensional arrays. –Declare and manipulate two-dimensional arrays. –Understand applications of two-dimensional arrays. –Use JRadioButton s to enable users to select only one option out of many.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 17.1 Test-Driving the Student Grades Application

4 © 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

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 17.1 Test-Driving the Student Grades Application (Cont.) Figure 17.1 Running the completed Student Grades application.

6 © 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

7 © 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 Displaying the student’s numerical grade. Numeric JRadioButton selected as the default

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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

9 © 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

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 17.2 Two-Dimensional Arrays (Cont.) Figure 17.5 Two-dimensional array with three rows and four columns. Column index (or subscript) Row index (or subscript) Array name Column 0Column 1Column 2Column 3 Row 0 Row 1 Row 2 myArray[0][0] myArray[1][0] myArray[2][0] myArray[0][1] myArray[1][1] myArray[2][1] myArray[0][2] myArray[1][2] myArray[2][2] myArray[0][3] myArray[1][3] myArray[2][3]

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 17.3 Using JRadioButton s 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 ButtonGroup s Only one button in a group can be on at one time

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 17.3 Using JRadioButton s (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

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 17.3 Using JRadioButton s (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

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 17.3 Using JRadioButton s (Cont.)

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 17.3 Using JRadioButton s (Cont.)

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 17.3 Using JRadioButton s (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

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 17.3 Using JRadioButton s (Cont.) Figure 17.8 Customizing letterJRadioButton. Setting the properties of letterJRadioButton

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 17.3 Using JRadioButton s (Cont.) Figure 17.9 Adding numericJRadioButton to the ButtonGroup. Use method add to add a JRadioButton to a ButtonGroup Calling ButtonGroup method add to add a JRadioButton

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 17.3 Using JRadioButton s (Cont.) Figure 17.10 Adding letterJRadioButton to the ButtonGroup. Calling ButtonGroup method add to add a JRadioButton

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 17.3 Using JRadioButton s (Cont.) Figure 17.11 Running the application after adding the JRadioButton s. Save the changes to your code Compile and run in the Command Prompt

21 © 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 Storing the input. Increment studentCount Store the student name and test grades in the arrays

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 17.4 Inserting Code into the Student Grades Application (Cont.) Figure 17.13 Displaying the output. Calling method displayNumericGrades Calling method displayLetterGrades

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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

24 © 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

25 © 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

26 © 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 gradeAdd the test grade to the student’s total

27 © 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

28 © 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

29 © 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 Outputting a header. Displaying a header

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 17.4 Inserting Code into the Student Grades Application (Cont.) Figure 17.21 Outputting each student’s name. Outputting the student’s name

31 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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

32 © 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

33 © 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

34 © 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

35 © 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

36 © 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

37 © 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

38 © 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

39  2004 Prentice Hall, Inc. All rights reserved. Outline 39 1 // Tutorial 17: StudentGrades.java 2 // This application computes each student's grade average and 3 // the class average for ten students. 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.text.*; 7 import javax.swing.*; 8 import javax.swing.border.*; 9 10 public class StudentGrades extends JFrame 11 { 12 // JPanel for user inputs 13 private JPanel inputGradeJPanel; 14 15 // JLabel and JTextField for student name 16 private JLabel studentNameJLabel; 17 private JTextField studentNameJTextField; 18 19 // JLabel and JTextField for test 1 score 20 private JLabel test1JLabel; 21 private JTextField test1JTextField; 22 23 // JLabel and JTextField for test 2 score 24 private JLabel test2JLabel; 25 private JTextField test2JTextField; StudentGrades.java (1 of 18)

40  2004 Prentice Hall, Inc. All rights reserved. Outline 40 26 27 // JLabel and JTextField for test 3 score 28 private JLabel test3JLabel; 29 private JTextField test3JTextField; 30 31 // JButton to calculate student and class average 32 private JButton submitGradesJButton; 33 34 // ButtonGroup to control numeric and letter JRadioButtons 35 private ButtonGroup displayButtonGroup; 36 37 // JRadioButtons to choose to display numerically or as letters 38 private JRadioButton numericJRadioButton; 39 private JRadioButton letterJRadioButton; 40 41 // JLabel, JTextArea and JScrollPane to display students averages 42 private JLabel displayJLabel; 43 private JTextArea displayJTextArea; 44 45 // JLabel and JTextField to display the class average 46 private JLabel classAverageJLabel; 47 private JTextField classAverageJTextField; 48 49 // initialize number of students to zero 50 private int studentCount = 0; StudentGrades.java (2 of 18)

41  2004 Prentice Hall, Inc. All rights reserved. Outline 41 51 52 // constants 53 private final int NUMBER_OF_TESTS = 3; 54 private final int MAXIMUM_STUDENTS = 10; 55 private final int FIRST_TEST = 0; 56 private final int SECOND_TEST = 1; 57 private final int THIRD_TEST = 2; 58 59 // one-dimensional array to store student names 60 private String studentNames[] = new String[ MAXIMUM_STUDENTS ]; 61 62 // two-dimensional array to store student grades 63 private int studentGrades[][] = 64 new int[ MAXIMUM_STUDENTS ][ NUMBER_OF_TESTS ]; 65 66 // DecimalFormat for two digits of precision 67 DecimalFormat twoDigits = new DecimalFormat( "0.00" ); 68 69 // no-argument constructor 70 public StudentGrades() 71 { 72 createUserInterface(); 73 } 74 StudentGrades.java (3 of 18)

42  2004 Prentice Hall, Inc. All rights reserved. Outline 42 75 // create and position GUI components; register event handlers 76 private void createUserInterface() 77 { 78 // get content pane for attaching GUI components 79 Container contentPane = getContentPane(); 80 81 // enable explicit positioning of GUI components 82 contentPane.setLayout( null ); 83 84 // set up inputGradeJPanel 85 inputGradeJPanel = new JPanel(); 86 inputGradeJPanel.setBounds( 16, 16, 208, 218 ); 87 inputGradeJPanel.setBorder( 88 new TitledBorder( "Input Grade" ) ); 89 inputGradeJPanel.setLayout( null ); 90 contentPane.add( inputGradeJPanel ); 91 92 // set up studentNameJLabel 93 studentNameJLabel = new JLabel(); 94 studentNameJLabel.setBounds( 8, 32, 90, 23 ); 95 studentNameJLabel.setText( "Student Name:" ); 96 inputGradeJPanel.add( studentNameJLabel ); 97 StudentGrades.java (4 of 18)

43  2004 Prentice Hall, Inc. All rights reserved. Outline 43 98 // set up studentNameJTextField 99 studentNameJTextField = new JTextField(); 100 studentNameJTextField.setBounds( 104, 32, 88, 21 ); 101 studentNameJTextField.setHorizontalAlignment( 102 JTextField.RIGHT ); 103 inputGradeJPanel.add( studentNameJTextField ); 104 105 // set up test1JLabel 106 test1JLabel = new JLabel(); 107 test1JLabel.setBounds( 8, 74, 60, 23 ); 108 test1JLabel.setText( "Test 1:" ); 109 inputGradeJPanel.add( test1JLabel ); 110 111 // set up test1JTextField 112 test1JTextField = new JTextField(); 113 test1JTextField.setBounds( 136, 74, 56, 21 ); 114 test1JTextField.setHorizontalAlignment( JTextField.RIGHT ); 115 inputGradeJPanel.add( test1JTextField ); 116 117 // set up test2JLabel 118 test2JLabel = new JLabel(); 119 test2JLabel.setBounds( 8, 98, 60, 23 ); 120 test2JLabel.setText( "Test 2:" ); 121 inputGradeJPanel.add( test2JLabel ); 122 StudentGrades.java (5 of 18)

44  2004 Prentice Hall, Inc. All rights reserved. Outline 44 123 // set up test2JTextField 124 test2JTextField = new JTextField(); 125 test2JTextField.setBounds( 136, 98, 56, 21 ); 126 test2JTextField.setHorizontalAlignment( JTextField.RIGHT ); 127 inputGradeJPanel.add( test2JTextField ); 128 129 // set up test3JLabel 130 test3JLabel = new JLabel(); 131 test3JLabel.setBounds( 8, 122, 60, 23 ); 132 test3JLabel.setText( "Test 3:" ); 133 inputGradeJPanel.add( test3JLabel ); 134 135 // set up test3JTextField 136 test3JTextField = new JTextField(); 137 test3JTextField.setBounds( 136, 122, 56, 21 ); 138 test3JTextField.setHorizontalAlignment( JTextField.RIGHT ); 139 inputGradeJPanel.add( test3JTextField ); 140 141 // set up submitGradesJButton 142 submitGradesJButton = new JButton(); 143 submitGradesJButton.setBounds( 72, 182, 120, 24 ); 144 submitGradesJButton.setText( "Submit Grades" ); 145 inputGradeJPanel.add( submitGradesJButton ); 146 submitGradesJButton.addActionListener( 147 StudentGrades.java (6 of 18)

45  2004 Prentice Hall, Inc. All rights reserved. Outline 45 148 new ActionListener() // anonymous inner class 149 { 150 // event handler called when submitGradesJButton is 151 // pressed 152 public void actionPerformed( ActionEvent event ) 153 { 154 submitGradesJButtonActionPerformed( event ); 155 } 156 157 } // end anonymous inner class 158 159 ); // end call to addActionListener 160 161 // set up displayButtonGroup 162 displayButtonGroup = new ButtonGroup(); 163 164 // set up numericJRadioButton 165 numericJRadioButton = new JRadioButton(); 166 numericJRadioButton.setBounds( 55, 244, 75, 23 ); 167 numericJRadioButton.setText( "Numeric" ); 168 numericJRadioButton.setSelected( true ); 169 displayButtonGroup.add( numericJRadioButton ); 170 contentPane.add( numericJRadioButton ); 171 numericJRadioButton.addActionListener( 172 StudentGrades.java (7 of 18) Setting the properties of numericJRadioButton Adding numericJRadioButton to the displayButtonGroup

46  2004 Prentice Hall, Inc. All rights reserved. Outline 46 173 new ActionListener() // anonymous inner class 174 { 175 // event handler called when numericJRadioButton is 176 // selected 177 public void actionPerformed( ActionEvent event ) 178 { 179 numericJRadioButtonActionPerformed( event ); 180 } 181 182 } // end anonymous inner class 183 184 ); // end call to addActionListener 185 186 // set up letterJRadioButton 187 letterJRadioButton = new JRadioButton(); 188 letterJRadioButton.setBounds( 140, 244, 75, 23 ); 189 letterJRadioButton.setText( "Letter" ); 190 displayButtonGroup.add( letterJRadioButton ); 191 contentPane.add( letterJRadioButton ); 192 letterJRadioButton.addActionListener( 193 StudentGrades.java (8 of 18) Setting the properties of letterJRadioButton Adding letterJRadioButton to the displayButtonGroup

47  2004 Prentice Hall, Inc. All rights reserved. Outline 47 194 new ActionListener() // anonymous inner class 195 { 196 // event handler called when letterJRadioButton is 197 // selected 198 public void actionPerformed( ActionEvent event ) 199 { 200 letterJRadioButtonActionPerformed( event ); 201 } 202 203 } // end anonymous inner class 204 205 ); // end call to addActionListener 206 207 // set up displayJLabel 208 displayJLabel = new JLabel(); 209 displayJLabel.setBounds( 240, 16, 150, 23 ); 210 displayJLabel.setText( "Average of each student:" ); 211 contentPane.add( displayJLabel ); 212 213 // set up displayJTextArea 214 displayJTextArea = new JTextArea(); 215 displayJTextArea.setBounds( 240, 48, 402, 184 ); 216 displayJTextArea.setEditable( false ); 217 contentPane.add( displayJTextArea ); 218 StudentGrades.java (9 of 18)

48  2004 Prentice Hall, Inc. All rights reserved. Outline 48 219 // set up classAverageJLabel 220 classAverageJLabel = new JLabel(); 221 classAverageJLabel.setBounds( 490, 244, 96, 23 ); 222 classAverageJLabel.setText( "Class average:" ); 223 contentPane.add( classAverageJLabel ); 224 225 // set up classAverageJTextField 226 classAverageJTextField = new JTextField(); 227 classAverageJTextField.setBounds( 586, 244, 56, 23 ); 228 classAverageJTextField.setHorizontalAlignment( 229 JTextField.CENTER ); 230 classAverageJTextField.setEditable( false ); 231 contentPane.add( classAverageJTextField ); 232 233 // set properties of application’s window 234 setTitle( "Student Grades" ); // set title-bar string 235 setSize( 670, 308 ); // set window size 236 setVisible( true ); // display window 237 238 } // end method createUserInterface 239 StudentGrades.java (10 of 18)

49  2004 Prentice Hall, Inc. All rights reserved. Outline 49 240 // convert a number to a letter grade 241 private String convertToLetterGrade( double grade ) 242 { 243 if ( grade >= 90 ) 244 { 245 return "A"; 246 } 247 else if ( grade >= 80 ) 248 { 249 return "B"; 250 } 251 else if ( grade >= 70 ) 252 { 253 return "C"; 254 } 255 else if ( grade >= 60 ) 256 { 257 return "D"; 258 } 259 else 260 { 261 return "F"; 262 } 263 264 } // end method convertToLetterGrade StudentGrades.java (11 of 18)

50  2004 Prentice Hall, Inc. All rights reserved. Outline 50 265 266 // calculate and display the student and class average 267 private void submitGradesJButtonActionPerformed( 268 ActionEvent event ) 269 { 270 // get user input 271 String nameOfStudent = studentNameJTextField.getText(); 272 int test1 = Integer.parseInt( test1JTextField.getText() ); 273 int test2 = Integer.parseInt( test2JTextField.getText() ); 274 int test3 = Integer.parseInt( test3JTextField.getText() ); 275 276 // add user input to arrays 277 studentNames[ studentCount ] = nameOfStudent; 278 studentGrades[ studentCount ][ FIRST_TEST ] = test1; 279 studentGrades[ studentCount ][ SECOND_TEST ] = test2; 280 studentGrades[ studentCount ][ THIRD_TEST ] = test3; 281 282 studentCount++; // increment studentCount 283 284 if ( numericJRadioButton.isSelected() ) 285 { 286 displayNumericGrades(); 287 } StudentGrades.java (12 of 18)

51  2004 Prentice Hall, Inc. All rights reserved. Outline 51 288 else 289 { 290 displayLetterGrades(); 291 } 292 293 // clear other JTextFields for new data 294 studentNameJTextField.setText( "" ); 295 test1JTextField.setText( "" ); 296 test2JTextField.setText( "" ); 297 test3JTextField.setText( "" ); 298 299 // if ten student grades have been entered 300 if ( studentCount == MAXIMUM_STUDENTS ) 301 { 302 // disable submitGradesJButton 303 submitGradesJButton.setEnabled( false ); 304 } 305 306 } // end method submitGradesJButtonActionPerformed 307 StudentGrades.java (13 of 18)

52  2004 Prentice Hall, Inc. All rights reserved. Outline 52 308 // display student grades and averages as numbers 309 private void displayNumericGrades() 310 { 311 // add a header to displayJTextArea 312 displayJTextArea.setText( 313 "Name\tTest 1\tTest 2\tTest 3\tAverage\n" ); 314 315 int studentTotal = 0; // store the student's total grades 316 int classTotal = 0; // store the class's total grades 317 318 for ( int student = 0; student < studentCount; student++ ) 319 { 320 // display student grades 321 displayJTextArea.append( studentNames[ student ] + "\t" ); 322 323 studentTotal = 0; // initialize the student's total grades 324 325 for ( int test = 0; test < NUMBER_OF_TESTS; test++ ) 326 { 327 // append each test grade to displayJTextArea 328 displayJTextArea.append( 329 studentGrades[ student ][ test ] + "\t" ); 330 StudentGrades.java (14 of 18)

53  2004 Prentice Hall, Inc. All rights reserved. Outline 53 331 // add the test grade to the student's total 332 studentTotal += studentGrades[ student ][ test ]; 333 334 } // end for 335 336 // add the student's total grade to the class's total 337 classTotal += studentTotal; 338 339 // calculate the student average and display it 340 double studentAverage = 341 ( double ) studentTotal / NUMBER_OF_TESTS; 342 displayJTextArea.append( 343 twoDigits.format( studentAverage ) + "\n" ); 344 345 } // end for 346 347 // calculate the class average and display it 348 double classAverage = 349 ( double ) classTotal / studentCount / NUMBER_OF_TESTS; 350 classAverageJTextField.setText( 351 twoDigits.format( classAverage ) ); 352 353 } // end method displayNumericGrades 354 StudentGrades.java (15 of 18)

54  2004 Prentice Hall, Inc. All rights reserved. Outline 54 355 // display student grades and averages as letters 356 private void displayLetterGrades() 357 { 358 // add a header to displayJTextArea 359 displayJTextArea.setText( 360 "Name\tTest 1\tTest 2\tTest 3\tAverage\n" ); 361 362 int studentTotal = 0; // store the student's total grades 363 int classTotal = 0; // store the class's total grades 364 365 for ( int student = 0; student < studentCount; student++ ) 366 { 367 // display student grades 368 displayJTextArea.append( studentNames[ student ] + "\t" ); 369 370 studentTotal = 0; // initialize the student's total grades 371 372 for ( int test = 0; test < NUMBER_OF_TESTS; test++ ) 373 { 374 // append each test grade to displayJTextArea 375 displayJTextArea.append( convertToLetterGrade( 376 studentGrades[ student ][ test ] ) + "\t" ); 377 378 // add the test grade to the student's total 379 studentTotal += studentGrades[ student ][ test ]; StudentGrades.java (16 of 18)

55  2004 Prentice Hall, Inc. All rights reserved. Outline 55 380 381 } // end for 382 383 // add the student's total grade to the class's total 384 classTotal += studentTotal; 385 386 // calculate the student average and display it 387 double studentAverage = 388 ( double ) studentTotal / NUMBER_OF_TESTS; 389 displayJTextArea.append( 390 convertToLetterGrade( studentAverage ) + "\n" ); 391 392 } // end for 393 394 // calculate the class average and display it 395 double classAverage = 396 ( double ) classTotal / studentCount / NUMBER_OF_TESTS; 397 classAverageJTextField.setText( 398 convertToLetterGrade( classAverage ) ); 399 400 } // end method displayLetterGrades 401 StudentGrades.java (17 of 18)

56  2004 Prentice Hall, Inc. All rights reserved. Outline 56 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 StudentGrades.java (18 of 18)


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application."

Similar presentations


Ads by Google