Outline
9.1 Test Driving the Class Average Application 9.2 do…while Repetition Statement
9.3 Creating the Class Average Application 9.4 Wrap-Up
Tutorial 9 – Class Average Application
Introducing the do … while Repetition Statement
Objectives
• In this tutorial, you will learn to:
– Use the
do…whilestatement.
– Understand counter-controlled repetition.
– Display an input dialog.
– Enable and disable
JButtons.
9.1 Test Driving the Class Average Application
Application Requirements
A teacher gives quizzes to a class of 10 students. The grades on these quizzes are integers in the range from 0 to 100, inclusive (0 and 100 are each valid grades). The teacher would like you to develop an application that computes the class average for a quiz. Your application should use an input dialog to enable the teacher to enter the grades.
Application (Cont.)
Figure 9.1 Running the completed Class Average application.
JTextArea Disabled JButton
Output JTextField
• Display input dialog
– Click Get Grades JButton
9.1 Test Driving the Class Average Application (Cont.)
Figure 9.2 Entering grades in the Class Average application.
JTextField of input dialog
• Entering grades
– Enter 85 as the first grade – Click the OK JButton
Application (Cont.)
Figure 9.3 Class Average application after 10 grades have been entered.
Ten quiz grades entered
• Entering more grades
– Complete entering the students’ grades
• Focus is set on Average JButton
Enabled JButton
Blue rectangle indicates that this component has the focus
9.1 Test Driving the Class Average Application (Cont.)
Figure 9.4 Class Average application after calculating the average.
• Calculating average
– Press the Space Bar to calculate average
9.2 do … while Repetition Statement
• do … while Repetition statement
– Similar to the
whilestatement
– Iterates while loop-continuation condition is
true– Loop-continuation condition checked after the body of the loop is performed
– Body always executes at least once
• Off-by-one errors
– Occur when the loop executes one too many or one less
iteration than is necessary
9.2 do … while Repetition Statement (Cont.)
Figure 9.5 do…while repetition statement UML activity diagram.
9.3 Creating the Class Average Application
Retrieve grades from user when the user clicks the Get Grades JButton:
Set total to zero
Set grade counter to one Clear the JTextArea
Clear the output JTextField Do
Get the next grade from the input dialog Append the grade to the JTextArea Add the grade to the total
Add one to the grade counter
While the grade counter is less than or equal to 10
Enable Average JButton
Give focus to Average JButton
Calculate average when the user clicks the Average JButton:
Calculate the class average by dividing the total by 10 Display the class average in the output JTextField Disable Average JButton
Give focus to Get Grades JButton
9.3 Creating the Class Average Application (Cont.)
Action Component Event
Label the application’s components
gradeListJLabel, classAverageJLabel
Retrieve grades from user getGradesJButton User clicks Get Grades JButton
Clear the JTextArea gradeListJTextArea
Clear the output JTextField classAverageJTextField
Get the next grade from the input dialog
JOptionPane
Append the grade to the JTextArea
gradeListJTextArea
Enable Average JButton averageJButton
Give focus to Average JButton
averageJButton
Calculate average averageJButton User clicks Average JButton
Display the class average classAverageJTextField
Disable Average JButton averageJButton
Give focus to Get Grades JButton
getGradesJButton
Figure 9.6 ACE table for the Class Average application.
(Cont.)
• Local variables
– Can only be used in the body of the method in which they are declared
• Instance variables
– Defined within a class, but outside any methods
– Can be accessed from any method
9.3 Creating the Class Average Application (Cont.)
Figure 9.7 Variable total declared outside a method.
Figure 9.8 Initializing your application’s variables.
Declaring local variables
(Cont.)
• Retrieving data from an input dialog
– Call the
JOptionPane.showInputDialogmethod to display an input dialog
– Returns the input entered by the user as a
Stringwhen the user clicks the
OK JButton– Use
Integer.parseIntto convert the
Stringto an
int
9.3 Creating the Class Average Application (Cont.)
Figure 9.9 Clearing the output components.
Figure 9.10 Getting the grade input using an input dialog.
Clearing the grade list and class average
Retrieving data from an input dialog
(Cont.)
Figure 9.11 Adding the grade input to the gradeListJTextArea.
Appending the input value to the gradeListJTextArea
9.3 Creating the Class Average Application (Cont.)
Figure 9.12 Running the updated application.
(Cont.)
Figure 9.13 Defining the do…while loop.
Start of do…while statement
Incrementing counter for next iteration Condition of do…
while statement
9.3 Creating the Class Average Application (Cont.)
Figure 9.14 Running the updated application.
(Cont.)
Figure 9.15 Summing the grades.
Figure 9.16 Disabling a JButton.
Add input to total
Use method setEnabled to enable or disable a JButton
9.3 Creating the Class Average Application (Cont.)
• setEnabled method
– Pass the argument
trueto allow the user to press the
JButton
(enable the
JButton)
– Pass the argument
falseto prevent the user from pressing
the
JButton(disable the
JButton)
(Cont.)
Figure 9.17 Enabling a JButton.
Figure 9.18 Transferring focus to a JButton.
To enable a JButton or other GUI component, pass true to method setEnabled
Transfer focus to a GUI component by using method
requestFocusInWindow
• Transferring the focus
– Call the JButton’s requestFocusInWindow method
9.3 Creating the Class Average Application (Cont.)
Figure 9.19 Calculating and displaying the class average.
Calculating and displaying the class average
• Cast operator
– Convert the operand (in this case total) to the type placed within the parentheses of the cast.
(Cont.)
Figure 9.20 Completed Class Average application.
Outline
ClassAverage.java (1 of 7)
2 // Application enables user to have the average of grades calculated.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class ClassAverage extends JFrame 8 {
9 // JLabel and JTextArea for list of grades 10 private JLabel gradeListJLabel;
11 private JTextArea gradeListJTextArea;
12
13 // JButton initiates retrieving grades 14 private JButton getGradesJButton;
15
16 // JButton initiates calculating average 17 private JButton averageJButton;
18
19 // JLabel and JTextField used to display average 20 private JLabel classAverageJLabel;
21 private JTextField classAverageJTextField;
22
23 private int total = 0; // holds value of the grade total
24 Declaring an
instance variable
ClassAverage.java (2 of 7)
27 {
28 createUserInterface();
29 } 30
31 // create and position GUI components; register event handlers 32 private void createUserInterface()
33 {
34 // get content pane for attaching GUI components 35 Container contentPane = getContentPane();
36
37 // enable explicit positioning of GUI components 38 contentPane.setLayout( null );
39
40 // set up gradeListJLabel
41 gradeListJLabel = new JLabel();
42 gradeListJLabel.setBounds( 16, 8, 70, 23 );
43 gradeListJLabel.setText( "Grade list:" );
44 contentPane.add( gradeListJLabel );
45
46 // set up gradeListJTextArea
47 gradeListJTextArea = new JTextArea();
48 gradeListJTextArea.setBounds( 16, 32, 88, 180 );
49 contentPane.add( gradeListJTextArea );
Outline
ClassAverage.java (3 of 7)
51 // set up getGradesJButton
52 getGradesJButton = new JButton();
53 getGradesJButton.setBounds( 128, 50, 100, 26 );
54 getGradesJButton.setText( "Get Grades" );
55 contentPane.add( getGradesJButton );
56 getGradesJButton.addActionListener(
57
58 new ActionListener() // anonymous inner class 59 {
60 // event handler called when getGradesJButton is clicked 61 public void actionPerformed( ActionEvent event )
62 {
63 getGradesJButtonActionPerformed( event );
64 } 65
66 } // end anonymous inner class 67
68 ); // end call to addActionListener 69
ClassAverage.java (4 of 7)
72 averageJButton.setBounds( 128, 90, 100, 26 );
73 averageJButton.setText( "Average" );
74 averageJButton.setEnabled( false );
75 contentPane.add( averageJButton );
76 averageJButton.addActionListener(
77
78 new ActionListener() // anonymous inner class 79 {
80 // event handler called when averageJButton is clicked 81 public void actionPerformed( ActionEvent event )
82 {
83 averageJButtonActionPerformed( event );
84 } 85
86 } // end anonymous inner class 87
88 ); // end call to addActionListener 89
90 // set up classAverageJLabel
91 classAverageJLabel = new JLabel();
92 classAverageJLabel.setBounds( 128, 132, 90, 23 );
93 classAverageJLabel.setText( "Class average:" );
94 contentPane.add( classAverageJLabel );
Disabling the
Average JButton
Outline
ClassAverage.java (5 of 7)
96 // set up classAverageJTextField
97 classAverageJTextField = new JTextField();
98 classAverageJTextField.setBounds( 128, 156, 100, 21 );
99 classAverageJTextField.setEditable( false );
100 classAverageJTextField.setHorizontalAlignment(
101 JTextField.CENTER );
102 contentPane.add( classAverageJTextField );
103
104 // set properties of application’s window
105 setTitle( "Class Average" ); // set title bar text 106 setSize( 250, 250 ); // set window size 107 setVisible( true ); // display window 108
109 } // end method createUserInterface 110
111 // method retrieves, totals, and displays grades from user
112 private void getGradesJButtonActionPerformed( ActionEvent event ) 113 {
114 total = 0; // stores total of grades entered 115 int counter = 1; // counter controls do...while statement 116 String input; // stores data entered into input dialog 117 int grade; // stores int value converted from input 118
Declaring local variables
ClassAverage.java (6 of 7)
121 classAverageJTextField.setText( "" );
122
123 do 124 { 125 // get user input 126 input = JOptionPane.showInputDialog( null, "Enter Grade" );
127 grade = Integer.parseInt( input );
128
129 // add text to output 130 gradeListJTextArea.append( grade + "\n" );
131 total += grade; // add input to total 132 counter++; // increment counter 133 } 134 while ( counter <= 10 ); // end do...while 135
136 averageJButton.setEnabled( true ); // enable averageJButton 137 averageJButton.requestFocusInWindow(); // transfer focus 138
139 } // end method getGradesJButtonActionPerformed 140
Clearing a JTextArea
and JTextField
Enabling the
Average JButton
and giving it the focus
Using a do…while
statement to calculate the class average
Outline
ClassAverage.java (7 of 7)
142 private void averageJButtonActionPerformed( ActionEvent event ) 143 {
144 double average = ( double ) total / 10; // calculate average 145 classAverageJTextField.setText( String.valueOf( average ) );
146
147 } // end method averageJButtonActionPerformed 148
149 // main method
150 public static void main( String[] args ) 151 {
152 ClassAverage application = new ClassAverage();
153 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
154
155 } // end method main 156
157 } // end class ClassAverage
Calculating and displaying class average