CS1101X: Programming Methodology Recitation 2 Classes - PowerPoint PPT Presentation

About This Presentation
Title:

CS1101X: Programming Methodology Recitation 2 Classes

Description:

Write a loan calculator program that computes both monthly and total payments ... Gets three input values--loan amount, interest rate, and ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 32
Provided by: aaro3
Category:

less

Transcript and Presenter's Notes

Title: CS1101X: Programming Methodology Recitation 2 Classes


1
CS1101X Programming MethodologyRecitation 2
Classes
2
Problem Statement
  • Problem statement
  • Write a loan calculator program that computes
    both monthly and total payments for a given loan
    amount, annual interest rate, and loan period.

3
Overall Plan
  • Tasks
  • Get three input values loanAmount, interestRate,
    and loanPeriod.
  • Compute the monthly and total payments.
  • Output the results.

4
Required Classes
5
Development Steps
  • We will develop this program in five steps
  • Start with the main class LoanCalculator. Define
    a temporary placeholder Loan class.
  • Implement the input routine to accept three input
    values.
  • Implement the output routine to display the
    results.
  • Implement the computation routine to compute the
    monthly and total payments.
  • Finalize the program.

6
Step 1 Design
  • The methods of the LoanCalculator class

7
Step 1 Code (1/5)
Directory Chapter4/Step1 Source Files
LoanCalculator.java Loan.java
8
Step 1 Code (2/5)
// Loan Class (Step 1) / Chapter 4 Sample
Development Loan Calculation (Step 1) File
Step1/Loan.java This class handles the loan
computation. / class Loan //-----------------
----------------- // Data Members //-----------
----------------------- // Default contructor.
public Loan( )
9
Step 1 Code (3/5)
/ Chapter 4 Sample Development Loan
Calculation (Step 1) File Step1/LoanCalculator.
java This class controls the input,
computation, and output of loan / class
LoanCalculator // Data Members // This
object does the actual loan computation
private Loan loan //----------------------------
------ // Main Method //-------------------------
--------- public static void main(String
arg) LoanCalculator calculator
new LoanCalculator()
calculator.start()
10
Step 1 Code (4/5)
//---------------------------------- //
Constructors //----------------------------------
public LoanCalculator() loan new
Loan() // Public Methods void start (
) // Top-level method that calls other
private methods public void start()
describeProgram() //tell what the program does
getInput() //get three input
values computePayment() //compute the
monthly payment and total
displayOutput() //diaply the results
11
Step 1 Code (5/5)
// Private Methods // Computes the monthly
and total loan payments. private void
computePayment() System.out.println("ins
ide computePayment") //TEMP //
Provides a brief explaination of the program to
the user. private void describeProgram()
System.out.println("inside
describeProgram") //TEMP //Display the
input values and monthly and total payments.
private void displayOutput()
System.out.println("inside displayOutput")
//TEMP // Gets three input values--loan
amount, interest rate, and // loan
period--using an InputBox object private void
getInput() System.out.println("inside
getInput") //TEMP
12
Step 1 Test
  • In the testing phase, we run the program multiple
    times and verify that we get the following output

13
Step 2 Design
  • Design the input routines
  • LoanCalculator will handle the user interaction
    of prompting and getting three input values
  • LoanCalculator calls the setAmount, setRate and
    setPeriod of a Loan object.

14
Step 2 Code (1/5)
// Loan Class (Step 2) class Loan // Data
Members // Constant for the number of months
in a year private final int MONTHS_IN_YEAR
12 // The amount of the loan private
double loanAmount // The monthly
interest rate private double
monthlyInterestRate // The number of
monthly payments private int
numberOfPayments
15
Step 2 Code (2/5)
//Creates a new Loan object with passed
values. public Loan(double amount, double
rate, int period) setAmount(amount)
setRate (rate )
setPeriod(period) //Returns the loan
amount. public double getAmount( )
return loanAmount //Returns the loan
period in the number of years. public int
getPeriod( ) return numberOfPayments /
MONTHS_IN_YEAR
16
Step 2 Code (3/5)
//Returns the annual interest rate.
public double getRate( ) return
monthlyInterestRate 100.0 MONTHS_IN_YEAR
//Sets the loan amount of this loan.
public void setAmount(double amount)
loanAmount amount //Sets the
interest rate of this loan. public void
setRate(double annualRate)
monthlyInterestRateannualRate/100.0/MONTHS_IN_YEA
R //Sets the loan period of this
loan. public void setPeriod(int
periodInYears) numberOfPayments
periodInYears MONTHS_IN_YEAR
17
Step 2 Code (4/5)
// File Step2/LoanCalculator.java //
Gets three input values--loan amount, interest
rate, // and loan period private void
getInput() double loanAmount,
annualInterestRate int loanPeriod
String inputStr inputStr
JOptionPane.showInputDialog(null,
"Loan Amount (DollarsCents)")
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)
18
Step 2 Code (5/5)
//create a new loan with the input
values loan new Loan(loanAmount,
annualInterestRate,
loanPeriod) //TEMP
System.out.println("Loan Amount "
loan.getAmount())
System.out.println("Annual Interest Rate"
loan.getRate() "")
System.out.println("Loan Period (years)"
loan.getPeriod())
19
Step 2 Test
  • We run the program numerous times with different
    input values
  • Check the correctness of input values by echo
    printing

20
Step 3 Design
  • We will implement the displayOutput method.
  • We will reuse the same design we adopted in
    Chapter 3 sample development.

21
Step 3 Code (1/2)
// Loan Class (Step 3) class Loan
//Returns the monthly payment public double
getMonthlyPayment( ) return 132.15
//TEMP //Returns the total
payment public double getTotalPayment( )
return 15858.10 //TEMP
22
Step 3 Code (2/2)
// File Step3/LoanCalculator.java //
Gets three input values--loan amount, interest
rate, // and loan period // Displays the
input values and monthly and total payments.
private void displayOutput()
System.out.println("Loan Amount "
loan.getAmount())
System.out.println("Annual Interest Rate"
loan.getRate() "")
System.out.println("Loan Period (years) "
loan.getPeriod())
System.out.println("Monthly payment is
"
loan.getMonthlyPayment())
System.out.println(" TOTAL payment is "
loan.getTotalPayment())

23
Step 3 Test
  • We run the program numerous times with different
    input values and check the output display format.
  • Adjust the formatting as appropriate

24
Step 4 Design
  • Two methods getMonthlyPayment and getTotalPayment
    are defined for the Loan class
  • We will implement them so that they work
    independent of each other.
  • It is considered a poor design if the clients
    must call getMonthlyPayment before calling
    getTotalPayment.

25
Step 4 Code (1/2)
// Loan Class (Step 4) class Loan
//Returns the monthly payment public double
getMonthlyPayment( ) double
monthlyPayment monthlyPayment
(loanAmount monthlyInterestRate)
/ (1 - Math.pow(1/(1 monthlyInterestRate),
numberOfPayments ) )
return monthlyPayment
26
Step 4 Code (2/2)
//Returns the total payment public
double getTotalPayment( ) double
totalPayment totalPayment
getMonthlyPayment( )
numberOfPayments return totalPayment

27
Step 4 Test
  • We run the program numerous times with different
    types of input values and check the results.

28
Step 5 Finalize
  • We will implement the describeProgram method
  • We will format the monthly and total payments to
    two decimal places using DecimalFormat.
  • Directory Chapter4/Step5
  • Source Files (final version)
  • LoanCalculator.java
  • Loan.java

29
Step 5 Code (1/2)
// File Step5/LoanCalculator.java //
Gets three input values--loan amount, interest
rate, // and loan period // Provides a
brief explaination of the program to the user.
private void describeProgram()
System.out.println( "This program
computes the monthly and total")
System.out.println( "payments for a
given loan amount, annual ")
System.out.println( "interest rate,
and loan period ( of years).")
System.out.println("\n")
30
Step 5 Code (2/2)
//Displays the input values and monthly and
total payments. private void displayOutput()
DecimalFormat df new
DecimalFormat("0.00")
System.out.println("Loan Amount "
loan.getAmount())
System.out.println("Annual Interest Rate"
loan.getRate() "")
System.out.println("Loan Period (years) "
loan.getPeriod())
System.out.println("Monthly payment is "
df.format(loan.getMonthly
Payment())) System.out.println(" TOTAL
payment is "
df.format(loan.getTotalPayment()))
31
End of Recitation 2
Write a Comment
User Comments (0)
About PowerShow.com