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

Slides:



Advertisements
Similar presentations
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Chapter 121 Window Interfaces Using Swing Chapter 12.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Objectives –Display a date and time. –Use a JPasswordField.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
8/9: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 27 - Phone Book Application Introducing Multimedia.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
A simple swing example GETTING STARTED WITH WIND CHILL.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 14 – Exception Handling
A First Look at GUI Applications
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

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

© Copyright 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.

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Student Grades Application (Cont.) Running the completed application –Open a Command Prompt Change to StudentGrades directory Type java StudentGrades

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Student Grades Application (Cont.) Figure 17.3 Displaying the student’s numerical grade. Numeric JRadioButton selected as the default

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Two-Dimensional Arrays Use two indices to identify elements Used to represent tables of values –m-by-n array m rows n columns

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

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

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

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

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

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

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

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

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

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application Figure Storing the input. Increment studentCount Store the student name and test grades in the arrays

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Application does not allow more than ten data entries. Disable submitGradesJButton when 10 grades have been entered

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Adding a header to displayJTextArea. Displaying a header

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Appending the student’s name to displayJTextArea. Outputting the student’s name

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Outputting each test grade and calculating the student’s total grade. Output each test gradeAdd the test grade to the student’s total

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Calculating and displaying the class’s average grade. Calculate and display the class’s average

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Outputting a header. Displaying a header

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Calculating and displaying the class’s average letter grade. Convert class’s average to a letter grade

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Running the application after declaring new methods. Save the changes to your code Compile and run in the Command Prompt

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure numericJRadioButton ’s actionPerformed event handler. Coding an event handler for the numericJRadioButton

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure letterJRadioButton ’s actionPerformed event handler. Coding an event handler for the letterJRadioButton

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Method numericJRadioButtonActionPerformed. Calling method displayNumericGrades

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inserting Code into the Student Grades Application (Cont.) Figure Method letterJRadioButtonActionPerformed. Calling method displayLetterGrades Save the changes to your code Compile and run in the Command Prompt

 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; // JLabel and JTextField for student name 16 private JLabel studentNameJLabel; 17 private JTextField studentNameJTextField; // JLabel and JTextField for test 1 score 20 private JLabel test1JLabel; 21 private JTextField test1JTextField; // JLabel and JTextField for test 2 score 24 private JLabel test2JLabel; 25 private JTextField test2JTextField; StudentGrades.java (1 of 18)

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

 2004 Prentice Hall, Inc. All rights reserved. Outline // 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; // one-dimensional array to store student names 60 private String studentNames[] = new String[ MAXIMUM_STUDENTS ]; // two-dimensional array to store student grades 63 private int studentGrades[][] = 64 new int[ MAXIMUM_STUDENTS ][ NUMBER_OF_TESTS ]; // DecimalFormat for two digits of precision 67 DecimalFormat twoDigits = new DecimalFormat( "0.00" ); // no-argument constructor 70 public StudentGrades() 71 { 72 createUserInterface(); 73 } 74 StudentGrades.java (3 of 18)

 2004 Prentice Hall, Inc. All rights reserved. Outline // 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(); // enable explicit positioning of GUI components 82 contentPane.setLayout( null ); // 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 ); // 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)

 2004 Prentice Hall, Inc. All rights reserved. Outline // set up studentNameJTextField 99 studentNameJTextField = new JTextField(); 100 studentNameJTextField.setBounds( 104, 32, 88, 21 ); 101 studentNameJTextField.setHorizontalAlignment( 102 JTextField.RIGHT ); 103 inputGradeJPanel.add( studentNameJTextField ); // set up test1JLabel 106 test1JLabel = new JLabel(); 107 test1JLabel.setBounds( 8, 74, 60, 23 ); 108 test1JLabel.setText( "Test 1:" ); 109 inputGradeJPanel.add( test1JLabel ); // set up test1JTextField 112 test1JTextField = new JTextField(); 113 test1JTextField.setBounds( 136, 74, 56, 21 ); 114 test1JTextField.setHorizontalAlignment( JTextField.RIGHT ); 115 inputGradeJPanel.add( test1JTextField ); // 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)

 2004 Prentice Hall, Inc. All rights reserved. Outline // set up test2JTextField 124 test2JTextField = new JTextField(); 125 test2JTextField.setBounds( 136, 98, 56, 21 ); 126 test2JTextField.setHorizontalAlignment( JTextField.RIGHT ); 127 inputGradeJPanel.add( test2JTextField ); // set up test3JLabel 130 test3JLabel = new JLabel(); 131 test3JLabel.setBounds( 8, 122, 60, 23 ); 132 test3JLabel.setText( "Test 3:" ); 133 inputGradeJPanel.add( test3JLabel ); // set up test3JTextField 136 test3JTextField = new JTextField(); 137 test3JTextField.setBounds( 136, 122, 56, 21 ); 138 test3JTextField.setHorizontalAlignment( JTextField.RIGHT ); 139 inputGradeJPanel.add( test3JTextField ); // 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)

 2004 Prentice Hall, Inc. All rights reserved. Outline 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 } } // end anonymous inner class ); // end call to addActionListener // set up displayButtonGroup 162 displayButtonGroup = new ButtonGroup(); // 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

 2004 Prentice Hall, Inc. All rights reserved. Outline 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 } } // end anonymous inner class ); // end call to addActionListener // 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

 2004 Prentice Hall, Inc. All rights reserved. Outline 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 } } // end anonymous inner class ); // end call to addActionListener // 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 ); // 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)

 2004 Prentice Hall, Inc. All rights reserved. Outline // set up classAverageJLabel 220 classAverageJLabel = new JLabel(); 221 classAverageJLabel.setBounds( 490, 244, 96, 23 ); 222 classAverageJLabel.setText( "Class average:" ); 223 contentPane.add( classAverageJLabel ); // 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 ); // 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 } // end method createUserInterface 239 StudentGrades.java (10 of 18)

 2004 Prentice Hall, Inc. All rights reserved. Outline // 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 } } // end method convertToLetterGrade StudentGrades.java (11 of 18)

 2004 Prentice Hall, Inc. All rights reserved. Outline // 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() ); // 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; studentCount++; // increment studentCount if ( numericJRadioButton.isSelected() ) 285 { 286 displayNumericGrades(); 287 } StudentGrades.java (12 of 18)

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

 2004 Prentice Hall, Inc. All rights reserved. Outline // 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" ); int studentTotal = 0; // store the student's total grades 316 int classTotal = 0; // store the class's total grades for ( int student = 0; student < studentCount; student++ ) 319 { 320 // display student grades 321 displayJTextArea.append( studentNames[ student ] + "\t" ); studentTotal = 0; // initialize the student's total grades 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)

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

 2004 Prentice Hall, Inc. All rights reserved. Outline // 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" ); int studentTotal = 0; // store the student's total grades 363 int classTotal = 0; // store the class's total grades for ( int student = 0; student < studentCount; student++ ) 366 { 367 // display student grades 368 displayJTextArea.append( studentNames[ student ] + "\t" ); studentTotal = 0; // initialize the student's total grades 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" ); // add the test grade to the student's total 379 studentTotal += studentGrades[ student ][ test ]; StudentGrades.java (16 of 18)

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

 2004 Prentice Hall, Inc. All rights reserved. Outline // user selected numeric display 403 private void numericJRadioButtonActionPerformed( 404 ActionEvent event ) 405 { 406 displayNumericGrades(); } // end method numericJRadioButtonActionPerformed // user selected letter display 411 private void letterJRadioButtonActionPerformed( 412 ActionEvent event ) 413 { 414 displayLetterGrades(); } // end method letterJRadioButtonActionPerformed // main method 419 public static void main( String[] args ) 420 { 421 StudentGrades application = new StudentGrades(); 422 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class StudentGrades StudentGrades.java (18 of 18)