(Mortgage Calculator Application) A bank offers mortgages that can be repaid in 10, 15, 20, 25 or 30 years. Write an application that allows a user to enter the amount of the mort- gage and the annual interest rate. When the user clicks a JButton, the application displays a table of the mortgage length in years together with the monthly payment as shown in Fig. 8.24.



```

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

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

c) Customizing a JTextArea. After line 109, insert a statement that sets the bounds property of outputJTextArea to 16, 96, 298, 110. On the next line, add a statement that sets the editable property of outputJTextArea to false.

d) Adding code to the Calculate JButton event handler. Add the code specified in the steps e through m to the calculateJButtonActionPerformed event handler, which begins at line 134.

e) Declaring variables required to determine monthly payments. In calculateJBut- tonActionPerformed, declare int variable years and initialize its value to 10. Also, declare int variable months and double variable monthlyPayment.

f) Obtaining the user input and converting the annual interest rate to the monthly interest

```
1 // Exercise 8.12: MortgageCalculator.java
2 // Calculates mortgage rate based on user inputs
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7
8 public class MortgageCalculator extends JFrame
9 {
10 // JLabel and JTextField for the mortgage amount
11 private JLabel amountJLabel;
12 private JTextField amountJTextField;
13
14 // JLabel and JTextField for interest percentage rate
15 private JLabel interestJLabel;
16 private JTextField interestJTextField;
17
18 // JTextArea for displaying results
19 private JTextArea outputJTextArea;
20
21 // JButton to initiate calculations
22 private JButton calculateJButton;
23
24 // no-argument constructor
25 public MortgageCalculator()
26 {
27 createUserInterface();
28 }
29
30 // create and position the GUI components; register event handlers
31 private void createUserInterface()
32 {
33 // get content pane for attaching GUI components
34 Container contentPane = getContentPane();
35
36 // enable explicit positioning of GUI components
37 contentPane.setLayout( null );
38
39 // set up amountJLabel
40 amountJLabel = new JLabel();
41 amountJLabel.setBounds( 16, 16, 104, 26 );
42 amountJLabel.setText( "Mortgage amount:" );
43 contentPane.add( amountJLabel );
44
45 // set up amountJTextField
46 amountJTextField = new JTextField();
47 amountJTextField.setBounds( 144, 16, 56, 26 );
48 amountJTextField.setHorizontalAlignment( JTextField.RIGHT );
49 contentPane.add( amountJTextField );
50 amountJTextField.addKeyListener(
51
52 new KeyAdapter() // anonymous inner class
53 {
54 // event handler for key pressed in amountJTextField
55 public void keyPressed( KeyEvent event )
56 {
57 amountJTextFieldKeyPressed( event );
58 }
59
60 } // end anonymous inner class
61
62 ); // end call to addKeyListener
63
64 // set up interestJLabel
65 interestJLabel = new JLabel( "Annual interest rate:" );
66 interestJLabel.setBounds( 16, 56, 115, 26 );
67 interestJLabel.setText( "Annual interest rate:" );
68 contentPane.add( interestJLabel);
69
70 // set up interestJTextField
71 interestJTextField = new JTextField();
72 interestJTextField.setBounds( 144, 56, 56, 26 );
73 interestJTextField.setHorizontalAlignment( JTextField.RIGHT );
74 contentPane.add( interestJTextField );
75 interestJTextField.addKeyListener(
76
77 new KeyAdapter() // anonymous inner class
78 {
79 // evend handler for key pressed in interestJTextField
80 public void interestJTextField( KeyEvent event )
81 {
82 interestJTextFieldKeyPressed( event );
83 }
84
85 } // end anonymous inner class
86
87 ); // end call to addKeyListener
88
89 // set up calculateJButton
90 calculateJButton = new JButton();
91 calculateJButton.setBounds( 224, 16, 90, 26 );
92 calculateJButton.setText( "Calculate" );
93 contentPane.add( calculateJButton );
94 calculateJButton.addActionListener(
95
96 new ActionListener() // anonymous inner class
97 {
98 // event handler called when calculateJButton is pressed
99 public void actionPerformed ( ActionEvent event )
100 {
101 calculateJButtonActionPerformed( event );
102 }
103
104 } // end anonymous inner class
105
106 ); // end call to addActionListener
107
108 // set up outputJTextArea
109 outputJTextArea = new JTextArea();
110 outputJTextArea.setBounds( 16, 96, 298, 110 );
111 outputJTextArea.setEditable( false );
112 contentPane.add( outputJTextArea );
113
114 // set properties of application’s window
115 setTitle( "Mortgage Calculator" ); // set title bar text
116 setSize( 338, 250 ); // set window's size
117 setVisible( true ); // display window
118
119 } // end method createUserInterface
120
121 // called when user presses key in amountJTextField
122 private void amountJTextFieldKeyPressed( KeyEvent event )
123 {
124 outputJTextArea.setText( "" ); // clear outputJTextArea
125 }
126
127 // called when user presses key in interestJTextField
128 private void interestJTextFieldKeyPressed( KeyEvent event )
129 {
130 outputJTextArea.setText( "" ); // clear outputJTextArea
131 }
132
133 // method called when user clicks calculateJButton
134 private void calculateJButtonActionPerformed( ActionEvent event )
135 {
136 int years = 10; // repetition counter
137 int months; // payment period
138 double monthlyPayment; // monthly payments
139
140 // retrieve user input
141 int amount = Integer.parseInt( amountJTextField.getText() );
142 double monthlyRate = Double.parseDouble(
143 interestJTextField.getText() ) / 100 / 12;
144
145 // format to display monthlyPayment in currency format
146 DecimalFormat currency = new DecimalFormat( "$0.00" );
147
148 // display header
149 outputJTextArea.setText
150 (
151 "Mortgage Length (Years)\tMonthly Payment" );
152 // while years is less than or equal to 30
153 while ( years <= 30 )
154 {
155 // calculate payment period
156 months = 12 * years;
157
158 // get monthlyPayment
159 monthlyPayment = calculateMonthlyPayment(
160 monthlyRate, months, amount );
161
162 // insert result into outputJTextArea
163 outputJTextArea.append( "\n" + years + "\t\t" +
164 currency.format( monthlyPayment ) );
165
166 years += 5; // increment counter
167
168 // end while
169
170 } // end method calculateJButtonActionPerformed
171
172 //
173 private double calculateMonthlyPayment( double monthlyInterest,
174 int months, int loanAmount )
175 {
176 double base = Math.pow( 1 + monthlyInterest, months );
177 return loanAmount * monthlyInterest / ( 1 - ( 1 / base ) );
178 }
179
180 //
181 public static void main( String args[] )
182 {
183 MortgageCalculator application = new MortgageCalculator();
184 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
185
186 } // end method main
187
188 } // end class MortgageCalculator
```

Computer Science & Information Technology

You might also like to view...

The Document Inspector checks for all of the information that displays in Slide Show view

Indicate whether the statement is true or false

Computer Science & Information Technology

The items in a ListBox can be listed in alphabetical order by setting the property ___________ to true.

a) Sorted b) AlphaSort c) ListSort d) None of the above.

Computer Science & Information Technology