(Grade Converter Application) The Grade Converter application is supposed to input an integer grade between 0 and 100 from the user and display the corresponding letter grade. For values 90–100 the application should display A; for 80–89 the application should display B; for 70–79 the application should display C; for 60–69 the application should display D; and for grades from 0–59, the application should display F. However, when you run the appli- cation you will notice that the application incorrectly displays B for all values in the range 90–100; the application should display A for these values. Follow the steps below to locate and fix the logic error. Figure 6.34 shows the incorrect output when the value 95 is input.
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial06ExercisesDebuggerGradeConverter to your C:SimplyJava directory.
b) Opening a Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJavaGradeCon- verter.
c) Creating the .class file for the Grade Converter application. Compile the Grade Converter application with the -g command-line option by typing javac -g Grade- Converter.java.
d) Starting debugging. In the Command Prompt, type jdb. This command will start the Java debugger.
e) Setting breakpoints. Use the stop command to set breakpoints at lines 95 and 99.
f) Running the application in the debugger. Run the Grade Converter application in the debugger by typing run GradeConverter. Type 95 in the Enter grade (0-100): JTextField and press the Convert JButton.
g) Locating the
```
1 // GradeConverter.java
2 // Input a numeric grade from the user and
3 // display the corresponding letter grade.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class GradeConverter extends JFrame
9 {
10 // JLabel and JTextField for numeric grade
11 private JLabel numberGradeJLabel;
12 private JTextField numberGradeJTextField;
13
14 // JLabel and JTextField for equivalent letter grade
15 private JLabel letterGradeJLabel;
16 private JTextField letterGradeJTextField;
17
18 // JButton to initiate conversion
19 private JButton convertJButton;
20
21 // no-argument constructor
22 public GradeConverter()
23 {
24 createUserInterface();
25 }
26
27 // create and position GUI components; register event handlers
28 public void createUserInterface()
29 {
30 // get content pane and set its layout to null
31 Container contentPane = getContentPane();
32 contentPane.setLayout( null );
33
34 // set up numberGradeJLabel
35 numberGradeJLabel = new JLabel();
36 numberGradeJLabel.setBounds( 16, 16, 130, 20 );
37 numberGradeJLabel.setText( "Enter grade (0-100):" );
38 contentPane.add( numberGradeJLabel );
39
40 // set up numberGradeJTextField
41 numberGradeJTextField = new JTextField();
42 numberGradeJTextField.setBounds( 140, 16, 32, 20 );
43 numberGradeJTextField.setHorizontalAlignment(
44 JTextField.CENTER );
45 contentPane.add( numberGradeJTextField );
46
47 // set up letterGradeJLabel
48 letterGradeJLabel = new JLabel();
49 letterGradeJLabel.setBounds( 16, 56, 130, 20);
50 letterGradeJLabel.setText( "Letter grade:" );
51 contentPane.add( letterGradeJLabel );
52
53 // set up letterGradeJTextField
54 letterGradeJTextField = new JTextField();
55 letterGradeJTextField.setBounds( 140, 56, 32, 20 );
56 letterGradeJTextField.setHorizontalAlignment(
57 JTextField.CENTER );
58 letterGradeJTextField.setEditable( false );
59 contentPane.add( letterGradeJTextField );
60
61 // set up convertJButton and register its event handler
62 convertJButton = new JButton();
63 convertJButton.setBounds( 57, 96, 90, 24 );
64 convertJButton.setText( "Calculate" );
65 contentPane.add( convertJButton );
66 convertJButton.addActionListener(
67
68 new ActionListener() // anonymous inner class
69 {
70 // event handler called when convertJButton is pressed
71 public void actionPerformed ( ActionEvent event )
72 {
73 convertJButtonActionPerformed( event );
74 }
75
76 } // end anonymous inner class
77
78 ); // end call to addActionListener
79
80 // set properties of Application’s window
81 setTitle( "Grade Converter" ); // set title bar text
82 setSize( 195, 165 ); // set window size
83 setVisible( true ); // display window
84
85 } // end method createUserInterface
86
87 // method called when user clicks convertJButton
88 public void convertJButtonActionPerformed( ActionEvent event )
89 {
90 int grade = Integer.parseInt(
91 numberGradeJTextField.getText() );
92
93 if ( grade >= 90 )
94 {
95 letterGradeJTextField.setText( "A" );
96 }
97 else if ( grade >= 80 )
98 {
99 letterGradeJTextField.setText( "B" );
100 }
101 else if ( grade >= 70 )
102 {
103 letterGradeJTextField.setText( "C" );
104 }
105 else if ( grade >= 60 )
106 {
107 letterGradeJTextField.setText( "D" );
108 }
109 else
110 {
111 letterGradeJTextField.setText( "F" );
112 }
113
114 } // end method convertJButtonActionPerformed
115
116 // main method
117 public static void main( String args[] )
118 {
119 GradeConverter application = new GradeConverter();
120 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
121
122 } // end method main
123
124 } // end class GradeConverter
```
You might also like to view...
Chrome, Opera and Safari are examples of what type of program or app?
A. browser B. hotspot C. LMS D. file sharing
The function prototype for malloc() is contained in the stdio.h header file.
Answer the following statement true (T) or false (F)
To which of the following does W3C refer?
A. World 3 Communications B. World 3 Consortium C. World Wide Web Consortium D. World Wide Web Communications
Word automatically numbers notes sequentially by placing a(n) ____________________ in the body of the document and also to the left of the note text.
Fill in the blank(s) with the appropriate word(s).