Develop an application that computes a student’s average quiz score for all of the quiz scores entered. The application’s GUI should appear as in Fig. 14.17. Use method Integer.parseInt to convert the user input to an int, and assign that value to a double. [Note: Implicit conversion occurs when you assign an int value to a double.] Use instance variables to keep track of the sum of all the quiz scores entered and the number of quiz scores entered. Each time a score is submitted, your application should recalculate the average.



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

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

c) Adding instance variables. At line 26, add two instance variables. The int instance variable quizzesTaken will keep track of the number of quiz scores entered. The double instance variable totalScore will keep track of the sum of all the quiz scores entered. These two variables will be used to calculate the class average. Add a com- ment before you declare the instance variables.

d) Adding code to the submitJButtonActionPerformed method. Find the sub- mitJButtonActionPerformed method (line 110) located just after method cre- ateUserInterface. The code required in Steps e–j should be placed in this method.

e) Obtaining user input. Use method Integer.parseInt to convert the user input from the quizJTextField to an int which should th


```
1 // QuizAverage.java
2 // Application enables a user to enter grades and have the average
3 // calculated.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.text.*;
7 import javax.swing.*;
8
9 public class QuizAverage extends JFrame
10 {
11 // JLabel and JTextField to display quiz score
12 private JLabel quizJLabel;
13 private JTextField quizJTextField;
14
15 // JButton to enter quiz score
16 private JButton submitJButton;
17
18 // JLabel and JTextField to display number of quizzes taken
19 private JLabel numberJLabel;
20 private JTextField numberJTextField;
21
22 // JLabel and JTextField to display average of all quiz scores
23 private JLabel averageJLabel;
24 private JTextField averageJTextField;
25
26 // declare instance variables
27 private int quizzesTaken = 0;
28 private double totalScore = 0.00;
29
30 // no-argument constructor
31 public QuizAverage()
32 {
33 createUserInterface();
34 }
35
36 // create and position GUI components; register event handlers
37 private void createUserInterface()
38 {
39 // get content pane for attaching GUI components
40 Container contentPane = getContentPane();
41
42 // enable explicit positioning of GUI components
43 contentPane.setLayout( null );
44
45 // set up quizJLabel
46 quizJLabel = new JLabel();
47 quizJLabel.setBounds( 16, 16, 88, 21 );
48 quizJLabel.setText( "Quiz score:" );
49 contentPane.add( quizJLabel );
50
51 // set up quizJTextField
52 quizJTextField = new JTextField();
53 quizJTextField.setBounds( 122, 16, 40, 24 );
54 quizJTextField.setHorizontalAlignment( JTextField.RIGHT );
55 contentPane.add( quizJTextField );
56
57 // set up submitJButton
58 submitJButton = new JButton();
59 submitJButton.setBounds( 183, 16, 116, 24 );
60 submitJButton.setText( "Submit Score" );
61 contentPane.add( submitJButton );
62 submitJButton.addActionListener(
63
64 new ActionListener() // anonymous inner class
65 {
66 // event handler called when submitJButton is clicked
67 public void actionPerformed ( ActionEvent event )
68 {
69 submitJButtonActionPerformed( event );
70 }
71
72 } // end anonymous inner class
73
74 ); // end call to addActionListener
75
76 // set up numberJLabel
77 numberJLabel = new JLabel();
78 numberJLabel.setBounds( 16, 56, 88, 21 );
79 numberJLabel.setText( "Number taken:" );
80 contentPane.add( numberJLabel );
81
82 // set up numberJTextField
83 numberJTextField = new JTextField();
84 numberJTextField.setBounds( 122, 56, 40, 24 );
85 numberJTextField.setHorizontalAlignment( JTextField.CENTER );
86 numberJTextField.setEditable( false );
87 contentPane.add( numberJTextField );
88
89 // set up averageJLabel
90 averageJLabel = new JLabel();
91 averageJLabel.setBounds( 175, 56, 56, 21 );
92 averageJLabel.setText( "Average:" );
93 contentPane.add( averageJLabel );
94
95 // set up averageJTextField
96 averageJTextField = new JTextField();
97 averageJTextField.setBounds( 259, 56, 40, 24 );
98 averageJTextField.setHorizontalAlignment( JTextField.CENTER );
99 averageJTextField.setEditable( false );
100 contentPane.add( averageJTextField );
101
102 // set properties of application’s window
103 setTitle( "Quiz Average" ); // set title bar string
104 setSize( 325, 125 ); // set window size
105 setVisible( true ); // display window
106
107 } // end method createUserInterface
108
109 // calculate and display average score
110 public void submitJButtonActionPerformed( ActionEvent event )
111 {
112 // get score; implicit conversions from int to double
113 double score = Integer.parseInt( quizJTextField.getText() );
114
115 quizzesTaken++; // update number of quizzes taken
116
117 totalScore += score; // update total score
118
119 // calculate average score
120 double average = totalScore / quizzesTaken;
121
122 // specify display format
123 DecimalFormat twoDigits = new DecimalFormat( "0.00" );
124
125 // display number of quizzes taken, average score
126 // and reset score
127 numberJTextField.setText( String.valueOf( quizzesTaken ) );
128 averageJTextField.setText( twoDigits.format( average ) );
129
130 } // end method submitJButtonActionPerformed
131
132 // main method
133 public static void main( String[] args )
134 {
135 QuizAverage application = new QuizAverage();
136 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
137
138 } // end method main
139
140 } // end class QuizAverage
```

Computer Science & Information Technology

You might also like to view...

Which of the following is NOT true when defining multiple properties on the same field?

A) The Format property affects how the data is both displayed and stored. B) If you save an input mask, the input mask is ignored when you define a format in the field's Format property. C) The data in the table does not change, regardless of how you define the Format property. D) When defining an input mask that differs from the Format property, the Format property takes precedence when the data is displayed.

Computer Science & Information Technology

Which of the following video options is NOT included on the Video Tools Format tab?

A) Applying a style B) Cropping C) Sizing D) Looping

Computer Science & Information Technology

The term ____ is used to refer to the raw information processed by computers and is stored in digital form.

A. syntax B. data C. literal D. byte

Computer Science & Information Technology

What is an incremental backup?

What will be an ideal response?

Computer Science & Information Technology