Java Reference
In-Depth Information
Stage 3: System Design
During system design, you identify the steps in the program.
Step 1.
Prompt the user to enter the annual interest rate, the number of years, and the
loan amount.
Step 2.
The input for the annual interest rate is a number in percent format, such as
4.5%. The program needs to convert it into a decimal by dividing it by 100 . To
obtain the monthly interest rate from the annual interest rate, divide it by 12 ,
since a year has 12 months. So, to obtain the monthly interest rate in decimal
format, you need to divide the annual interest rate in percentage by 1200 . For
example, if the annual interest rate is 4.5%, then the monthly interest rate is
4.5/1200
=
0.00375.
Step 3.
Compute the monthly payment using the preceding formula.
Step 4.
Compute the total payment, which is the monthly payment multiplied by 12 and
multiplied by the number of years.
Step 5.
Display the monthly payment and total payment.
Stage 4: Implementation
Implementation is also known as coding (writing the code). In the formula, you have to com-
pute which can be obtained using Math.pow(1 +
monthlyInterestRate, numberOfYears * 12) .
Listing 2.8 gives the complete program.
monthlyInterestRate ) numberOfYears * 12 ,
(1
+
Math.pow(a, b) method
L ISTING 2.8 ComputeLoan.java
1
2
3 public class ComputeLoan {
4
import java.util.Scanner;
import class
public static void main(String[] args) {
5
// Create a Scanner
6
7
8 // Enter annual interest rate in percentage, e.g., 7.25%
9 System.out.print( "Enter annual interest rate, e.g., 7.25%: " );
10
11
12
Scanner input = new Scanner(System.in);
create a Scanner
double annualInterestRate = input.nextDouble();
enter interest rate
// Obtain monthly interest rate
13
double monthlyInterestRate = annualInterestRate / 1200 ;
14
15 // Enter number of years
16 System.out.print(
17
"Enter number of years as an integer, e.g., 5: " );
18
19
20 // Enter loan amount
21 System.out.print( "Enter loan amount, e.g., 120000.95: " );
22
23
24 // Calculate payment
25 double = loanAmount * monthlyInterestRate / ( 1
26 - 1 / Math.pow( 1 + monthlyInterestRate, numberOfYears * 12 ));
27
int numberOfYears = input.nextInt();
enter years
double loanAmount = input.nextDouble();
enter loan amount
monthlyPayment
monthlyPayment
double
totalPayment
= monthlyPayment * numberOfYears * 12 ;
totalPayment
28
29
// Display results
 
Search WWH ::




Custom Search