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.
(The interest rate is commonly expressed as a percentage of the principal for a period of
one year. This is known as the annual interest rate .)
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 (1
monthlyInterestRate ) numberOfYears * 12 , which can be obtained using Math.pow(1 +
monthlyInterestRate, numberOfYears * 12) .
Listing 2.9 gives the complete program.
+
Math.pow(a, b) method
L ISTING 2.9
ComputeLoan.java
1 import java.util.Scanner;
2
3 public class ComputeLoan {
4
import class
public static void main(String[] args) {
5
// Create a Scanner
6
Scanner input = new Scanner(System.in);
create a Scanner
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
double annualInterestRate = input.nextDouble();
enter interest rate
11
12
// 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
int numberOfYears = input.nextInt();
enter years
19
20 // Enter loan amount
21 System.out.print( "Enter loan amount, e.g., 120000.95: " );
22
double loanAmount = input.nextDouble();
enter loan amount
23
24 // Calculate payment
25 double monthlyPayment = loanAmount * monthlyInterestRate / ( 1
26 - 1 / Math.pow( 1 + monthlyInterestRate, numberOfYears * 12 ));
monthlyPayment
 
 
Search WWH ::




Custom Search