The Discount Calculator application determines the discount the user will receive based on how much money the user spends. A 15% dis- count is received for purchases of $150 or more, a 10% discount is received for purchases between $100–$149 and a 5% discount is received for purchases between $50–$99. Purchases less than $50 do not receive a discount. While testing your application, you notice that the application is not calculating the discount properly for some values. Use the debugger to find and fix the logic error(s) in the application. Figure 11.22 displays the correct output for values in each range.



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

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

c) Opening the 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:SimplyJavaDiscount- Calculator.

d) Compiling the application for debugging. Compile your application by typing javac

-g DiscountCalculator.java,.

e) Running the application. When your application compiles correctly, run it by typing java DiscountCalculator. To test your application, enter the amounts shown in Fig. 11.22. When you enter the value 75 (or any other value in the range 50–99), notice that the application incorrectly indicates a discount of 15%.

f) Starting the debugger. Close the applica

The original code was missing a case statement. Without case 1: any input between 50-99 would yield a discount of 15% instead of the correct discount of 5%.

```
1 // DiscountCalculator.java
2 // Calculates a the discount the user will receive
3 // based on how much the user spends.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class DiscountCalculator extends JFrame
9 {
10 // JLabel and JTextField allow user to enter amount spent
11 private JLabel spentJLabel;
12 private JTextField spentJTextField;
13
14 // JButton that initiates discount calculation
15 private JButton discountJButton;
16
17 // no-argument constructor
18 public DiscountCalculator()
19 {
20 createUserInterface();
21 }
22
23 // create and position GUI components; register event handlers
24 private void createUserInterface()
25 {
26 // get content pane for attaching GUI components
27 Container contentPane = getContentPane();
28
29 // enable explicit positioning of GUI components
30 contentPane.setLayout( null );
31
32 // set up spentJLabel
33 spentJLabel = new JLabel();
34 spentJLabel.setBounds( 10, 10, 120, 25 );
35 spentJLabel.setText( "Enter amount spent:" );
36 contentPane.add( spentJLabel );
37
38 // set up spentJTextField
39 spentJTextField = new JTextField();
40 spentJTextField.setBounds( 140, 10, 90, 25 );
41 spentJTextField.setHorizontalAlignment( JTextField.RIGHT );
42 contentPane.add( spentJTextField );
43
44 // set up discountJButton
45 discountJButton = new JButton();
46 discountJButton.setBounds( 140, 45, 90, 25 );
47 discountJButton.setText( "Calculate" );
48 contentPane.add( discountJButton );
49 discountJButton.addActionListener(
50
51 new ActionListener() // anonymous inner class
52 {
53 // event handler called when discountJButton is pressed
54 public void actionPerformed( ActionEvent event )
55 {
56 discountJButtonActionPerformed( event );
57 }
58
59 } // end anonymous inner class
60
61 ); // end call to addActionListener
62
63 // set properties of application's window
64 setTitle( "Discount Calculator" ); // set window title
65 setSize( 245, 105 ); // set window size
66 setVisible( true ); // display window
67
68 } // end method createUserInterface
69
70 // display a message stating the users discount
71 private void discountJButtonActionPerformed( ActionEvent event )
72 {
73 int discountRate; // variable to store discount rate
74 int amount = Integer.parseInt( spentJTextField.getText() );
75
76 // determine discount percentage
77 switch ( amount / 50 )
78 {
79 case 0: // values in the range $0-49
80 discountRate = 0;
81 break;
82
83 case 1: // values in the range $50-99
84 discountRate = 5;
85 break;
86
87 case 2: // values in the range $100-149
88 discountRate = 10;
89 break;
90
91 default: // values $150 or greater
92 discountRate = 15;
93
94 } // end switch statement
95
96 // display discount percentage
97 JOptionPane.showMessageDialog( null,
98 "Your Discount is: " + discountRate + " %",
99 "Discount", JOptionPane.INFORMATION_MESSAGE );
100
101 } // end method discountJButtonActionPerformed
102
103 // main method
104 public static void main( String[] args )
105 {
106 DiscountCalculator application = new DiscountCalculator();
107 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
108
109 } // end method main
110
111 } // end class DiscountCalculator
```

Computer Science & Information Technology

You might also like to view...

Level 6 items are more significant in comparison to Level 1 and 2 items

Indicate whether the statement is true or false

Computer Science & Information Technology

To get help with a Windows command-line program, use this syntax: programname /h

Indicate whether the statement is true or false

Computer Science & Information Technology