LoanCalculator.java
Input:
amount of a loan (double)
annual rate of interest (double)
loan period
–
i.e. the number of years to repay the loan (int)
Output:
echo print the user input values (always)
the mont
h
ly payment
the total payment over the len
gth of the loan
Let: A = the amount of the loan
R = the monthly rate of interest
N = the number of monthly payments
monthlyPayment =
N
R
1
1
1
R
*
A
All files are at:
http://www.mscs.mu.edu/~marian/60/ClassExamples/03LoanCalculator
TheLoanCalculator.java
Modifies the existing
LoanCalculator.java
by introducing a
java.util.Scanner
, which is
new to Java 1.5.0.
The final version introduces
error
–
checking of the input using an if
LoanCalculator1.java
/* Author: Wu (revised)
*Purpose: User inputs amount of loan, annual interest rate and
* time in years (integer) for repayment. Output is
* the monthly payment and the
total repayment.
*/
import javax.swing.*;
class LoanCalculator
{
public static void main (String[] args)
{
double loanAmount,
annualInterestRate;
int loanPeriod;
String inputStr;
//get inp
ut values
inputStr = JOptionPane.showInputDialog(null,
"Loan Amount (Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDial
og(null,
"Annual Interest Rate (e.g., 9.5):");
annualInterestRate = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Loan Period

# of years:");
loanPeriod = Integer.parseInt(inputStr);
//echo print the input values
System.out.println("Loan Amount: $" + loanAmount);
System.out.println("Annual Interest Rate: " + annualInterestRate +
"%");
System.out.println("Loan Period (years): " + loanPeriod);
}
}
/********* The output *********
Loan Amount: $100000.0
Annual Interest Rate: 6.0%
Loan Period (years): 30
*/
LoanCalculator2.java
import javax.swing.*;
clas
s LoanCalculator
{
public static void main (String[] args)
{
double loanAmount,
annualInterestRate,
monthlyPayment,
totalPayment;
int loanPeriod;
String inputStr;
//get input values
inputStr = JOptionPane.showInputDialog(null,
"Loan Amount (Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.
showInputDialog(null,
"Annual Interest Rate (e.g., 9.5):");
annualInterestRate = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"
Loan Period

# of years:");
loanPeriod = Integer.parseInt(inputStr);
//compute the monthly and total payments
monthlyPayment = 132.15;
totalPayment = 15858.10;
//display the result
System.out.pri
ntln("Loan Amount: $" + loanAmount);
System.out.println("Annual Interest Rate: " + annualInterestRate + "%");
System.out.println("Loan Period (years): " + loanPeriod);
System.out.println("
\
n"); //skip two lines
System.out.println("Monthly payment is $ " + monthlyPayment);
System.out.println(" TOTAL payment is $ " + totalPayment);
}
}
/********** Output **********
Loan Amount: $100000.0
Annual Interest Rate: 6.0%
Loan Period (years):
30
Monthly payment is $ 132.15
TOTAL payment is $ 15858.1
*/
LoanCalculator3.java
import javax.swing.*;
class LoanCalculator
{
public static void main (String[] args)
{
final int MONTHS_IN_YEAR = 12;
double loanAmo
unt,
annualInterestRate,
monthlyPayment,
totalPayment,
monthlyInterestRate;
int loanPeriod,
numberOfPayments;
String inputStr;
//get input value
s
inputStr = JOptionPane.showInputDialog("Loan Amount (Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(
"Annual Inter
est Rate (e.g., 9.5):");
annualInterestRate = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog("Loan Period

# of years:");
loanPeriod = Integer.parseInt(inputStr);
//compute the mon
thly and total payments
monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / 100;
numberOfPayments = loanPeriod * MONTHS_IN_YEAR;
monthlyPayment = (loanAmount * monthlyInterestRate) /
(1

Math.pow(1/(1 + mont
hlyInterestRate), numberOfPayments ) );
totalPayment = monthlyPayment * numberOfPayments;
//display the result
System.out.println("Loan Amount: $" + loanAmount);
System.out.println("Annual Interest Rate: " + an
nualInterestRate + "%");
System.out.println("Loan Period (years): " + loanPeriod);
System.out.println(
"
\
nMonthly payment is $ " + monthlyPayment +
"
\
n TOTAL payment is $ " + totalPayment);
}
}
/******
**** Outout **********
Loan Amount: $100000.0
Annual Interest Rate: 6.0%
Loan Period (years): 30
Monthly payment is $ 599.5505251527582
TOTAL payment is $ 215838.18905499292
*/
LoanCalculator.java
/* Author: Wu (revised)
*Purpose:
User inputs amount of loan, annual interest rate and
* time in years (integer) for repayment. Output is
* the monthly payment and the total repayment.
*/
import javax.swing.*;
import java.text.*;
class LoanCalculator
{
public sta
tic void main (String[] args)
{
final int MONTHS_IN_YEAR = 12;
double loanAmount,
annualInterestRate,
monthlyPayment,
totalPayment,
monthlyInterestRate;
int
loanPeriod,
numberOfPayments;
String inputStr;
DecimalFormat df = new DecimalFormat("0.00");
//describe the program
System.out.println(
"
\
nThis program computes the monthly and total" +
"
\
npayments for a given loan amount, annual " +
"
\
ninterest rate, and loan period." +
"
\
nLoan amount in dollars and cents, e.g., 12345.50" +
"
\
nAnnual interest rate in percentage, e.g., 12.75" +
"
\
nLoan per
iod in number of years, e.g., 15");
//get input values
inputStr = JOptionPane.showInputDialog("Loan Amount (Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(
"Annual Interest Rate (e.g., 9.5):");
annualInterestRate = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog("Loan Period

# of years:");
loanPeriod = Integer.parseInt(inputStr);
//compute th
e monthly and total payments
monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / 100;
numberOfPayments = loanPeriod * MONTHS_IN_YEAR;
monthlyPayment = (loanAmount * monthlyInterestRate) /
(1

Math.pow(1/(1
+ monthlyInterestRate), numberOfPayments));
totalPayment = monthlyPayment * numberOfPayments;
//display the result
System.out.println(
"
\
nLoan Amount: $" +
df.format(loanAmount)
+
"
\
nAnnual Inter
est Rate: " +
df.format(annualInterestRate)
+ "%" +
"
\
nLoan Period (years): " + loanPeriod +
"
\
n" + //skip a line
"
\
nMonthly payment is $ " +
df.format(monthlyPayment)
+
"
\
n TOTAL payment is
$ " +
df.format(totalPayment))
;
}
}
/****************** Output ******************
This program computes the monthly and total
payments for a given loan amount, annual
interest rate, and loan period.
Loan amount in dollars and cents, e.g., 12345.50
An
nual interest rate in percentage, e.g., 12.75
Loan period in number of years, e.g., 15
Loan Amount:
$100000.00
Annual Interest Rate:
6.00%
Loan Period (years): 30
Monthly payment is
$ 599.55
TOTAL payment is
$ 215838.19
*/
TheLoanCa
lculator
1
.java
Uses
a
java.util.Scanner
object
instead of an InputDialog
Only the code that has been changed is shown.
...
indicates missing code that
is
identical to the final version of
LoanCalculator.java
import java.util.*;
import java.text.*;
cla
ss TheLoanCalculator
{
public static void main (String[] args)
{
// ... variable declarations ...
Scanner scanner = new Scanner(System.in);
//
...
describe the program
...
//get input values
System.out.pr
int("Loan Amount (Dollars+Cents): ");
loanAmount = scanner.nextDouble();
System.out.print("Annual Interest Rate (e.g., 9.5): ");
annualInterestRate = scanner.nextDouble();
System.out.print("Loan Period

# of years: ");
loanPeriod = scanner.nextInt();
//
...
compute the monthly and total payments
...
//
...
display the result
...
}
}
/****************** Output ******************
This program computes the monthly and total
payments for a given lo
an amount, annual
interest rate, and loan period.
Loan amount in dollars and cents, e.g., 12345.50
Annual interest rate in percentage, e.g., 12.75
Loan period in number of years, e.g., 15
Loan Amount (Dollars+Cents): 100000
Annual Interest Rate (e.g., 9.
5): 5.8
Loan Period

# of years: 20
Loan Amount: $100000.00
Annual Interest Rate: 5.8%
Loan Period (years): 20
Monthly payment is $ 704.94
TOTAL payment is $ 169185.83
*/
TheLoanCalculator.java
–
Add Error Checking to the Input St
atements
New code that is added to the existing TheLoanCaluclator.java is shown and highlighted.
//get input values
System.out.print("Loan Amount (Dollars+Cents): ");
loanAmount = scanner.nextDouble();
if(loanAmount <= 0)
{
System.out.println(
"
\
nERROR

The loan amount must be greater than or equal to 0");
System.exit(0);
}
System.out.print("Annual Interest Rate (e.g., 9.5): ");
annualInterestRate = s
canner.nextDouble();
if(annualInterestRate <= 0)
{
System.out.println(
"
\
nERROR

The interest rate must be greater than or equal to 0");
System.exit(0);
}
System.out.print("Loan Peri
od

# of years: ");
loanPeriod = scanner.nextInt();
if(loanPeriod <= 0)
{
System.out.println(
"
\
nERROR

The loan period must be greater than or equal to 0");
System.exit(0);
}
/****
****************** Output ********************
This program computes the monthly and total
payments for a given loan amount, annual
interest rate, and loan period.
Loan amount in dollars and cents, e.g., 12345.50
Annual interest rate in percentage, e.g.,
12.75
Loan period in number of years, e.g., 15
Loan Amount (Dollars+Cents):

10000
ERROR

The loan amount must be greater than or equal to 0
***************************
This program computes the monthly and total
payments for a given loa
n amount, annual
interest rate, and loan period.
Loan amount in dollars and cents, e.g., 12345.50
Annual interest rate in percentage, e.g., 12.75
Loan period in number of years, e.g., 15
Loan Amount (Dollars+Cents): 2000
Annual Interest Rate (e.g., 9.5):

1
ERROR

The interest rate must be greater than or equal to 0
*/
Enter the password to open this PDF file:
File name:

File size:

Title:

Author:

Subject:

Keywords:

Creation Date:

Modification Date:

Creator:

PDF Producer:

PDF Version:

Page Count:

Preparing document for printing…
0%
Comments 0
Log in to post a comment