Invoke instance method enter annual interest rate for

This preview shows page 9 - 12 out of 33 pages.

invoke instance method Enter annual interest rate, for example, 8.25: Enter number of years as an integer: Enter loan amount, for example, 120000.95: The loan was created on Sat Jun 16 21:12:50 EDT 2012 The monthly payment is 17.75 The total payment is 1064.84 1000 5 2.5 The main method reads the interest rate, the payment period (in years), and the loan amount; creates a Loan object; and then obtains the monthly payment (line 29) and the total payment (line 30) using the instance methods in the Loan class. The Loan class can be implemented as in Listing 10.2. L ISTING 10.2 Loan.java 1 public { 2 private double annualInterestRate; 3 private int numberOfYears; 4 private double loanAmount; 5 private java.util.Date loanDate; 6 7 /** Default constructor */ 8 public Loan() { 9 this ( 2 . 5 , 1 , 1000 ); 10 } 11 12 /** Construct a loan with specified annual interest rate, 13 number of years, and loan amount 14 */ class Loan no-arg constructor
378 Chapter 10 Thinking in Objects 15 16 { 17 this .annualInterestRate = annualInterestRate; 18 this .numberOfYears = numberOfYears; 19 this .loanAmount = loanAmount; 20 loanDate = new java.util.Date(); 21 } 22 23 /** Return annualInterestRate */ 24 public double getAnnualInterestRate() { 25 return annualInterestRate; 26 } 27 28 /** Set a new annualInterestRate */ 29 public void setAnnualInterestRate( double annualInterestRate) { 30 this .annualInterestRate = annualInterestRate; 31 } 32 33 /** Return numberOfYears */ 34 public int getNumberOfYears() { 35 return numberOfYears; 36 } 37 38 /** Set a new numberOfYears */ 39 public void setNumberOfYears( int numberOfYears) { 40 this .numberOfYears = numberOfYears; 41 } 42 43 /** Return loanAmount */ 44 public double getLoanAmount() { 45 return loanAmount; 46 } 47 48 /** Set a new loanAmount */ 49 public void setLoanAmount( double loanAmount) { 50 this .loanAmount = loanAmount; 51 } 52 53 /** Find monthly payment */ 54 public double getMonthlyPayment() { 55 double monthlyInterestRate = annualInterestRate / 1200 ; 56 double monthlyPayment = loanAmount * monthlyInterestRate / ( 1 - 57 ( 1 / Math.pow( 1 + monthlyInterestRate, numberOfYears * 12 ))); 58 return monthlyPayment; 59 } 60 61 /** Find total payment */ 62 public double getTotalPayment() { 63 double totalPayment = getMonthlyPayment() * numberOfYears * 12 ; 64 return totalPayment; 65 } 66 67 /** Return loan date */ 68 public java.util.Date getLoanDate() { 69 return loanDate; 70 } 71 } double loanAmount) public Loan( double annualInterestRate, int numberOfYears, constructor
10.6 Object-Oriented Thinking 379 From a class developer’s perspective, a class is designed for use by many different customers. In order to be useful in a wide range of applications, a class should provide a variety of ways for customization through constructors, properties, and methods. The Loan class contains two constructors, four get methods, three set methods, and the methods for finding the monthly payment and the total payment. You can construct a Loan object by using the no-arg constructor or the constructor with three parameters: annual inter- est rate, number of years, and loan amount. When a loan object is created, its date is stored in the loanDate field. The getLoanDate method returns the date. The three get methods— getAnnualInterest , getNumberOfYears , and getLoanAmount —return the annual

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture