Let us use the Loan class as an example to demonstrate the creation and use of classes. Loan has the data fields: annualInterestRate , numberOfYears , loanAmount , and loanDate , and the methods getAnnualInterestRate , getNumberOfYears , getLoanAmount , getLoanDate , setAnnualInterestRate , setNumberOfYears , setLoanAmount , getMonthlyPayment , and getTotalPayment , as shown in Figure 7.23.
Note
An object can contain another object. The relationship between the two is called composition . In this example, a Loan object contains a Date object. |
The UML diagram in Figure 7.23 serves as the contract for the Loan class. Throughout the book, you will play the role of both class user and class writer. The user can use the class without knowing how the class is implemented. Assume that the Loan class is available. Let us begin by writing a test program that uses the Loan class in Listing 7.8.
1 import javax.swing.JOptionPane; 2 3 public class TestLoanClass { 4 /** Main method */ 5 public static void main(String[] args) { 6 // Enter yearly interest rate 7 String annualInterestRateString = JOptionPane.showInputDialog( 8 "Enter yearly interest rate, for example 8.25:" ); 9 10 // Convert string to double 11 double annualInterestRate = 12 Double.parseDouble(annualInterestRateString); 13 14 // Enter number of years 15 String numberOfYearsString = JOptionPane.showInputDialog( 16 "Enter number of years as an integer, \nfor example 5:" ); 17 18 // Convert string to int 19 int numberOfYears = Integer.parseInt(numberOfYearsString); 20 21 // Enter loan amount 22 String loanString = JOptionPane.showInputDialog( 23 "Enter loan amount, for example 120000.95:" ); 24 25 // Convert string to double 26 double loanAmount = Double.parseDouble(loanString); 27 28 // Create Loan object 29 Loan loan = 30 new Loan(annualInterestRate, numberOfYears, loanAmount) ; 31 32 // Format to keep two digits after the decimal point 33 double monthlyPayment = 34 ( int )( loan.getMonthlyPayment() * 100 ) / 100 . ; 35 double totalPayment = 36 ( int )( loan.getTotalPayment() * 100 ) / 100 . ; 37 38 // Display results 39 String output = "The loan was created on " + 40 loan.getLoanDate().toString() + "\nThe monthly payment is " + 41 monthlyPayment + "\nThe total payment is " + totalPayment; 42 JOptionPane.showMessageDialog( null , output); 43 } 44 } |
The main method reads interest rate, payment period (in years), and loan amount; creates a Loan object; and then obtains the monthly payment (lines 33 “34) and total payment (lines 35 “36) using the instance methods in the Loan class. Figure 7.24 shows the output of a sample run of the program.
The Loan class can be implemented in Listing 7.9.
1 public class Loan { 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 ( 7.5 , 30 , 100000 ); 10 } 11 12 /** Construct a loan with specified annual interest rate, 13 number of years and loan amount 14 */ 15 public Loan( double annualInterestRate, int numberOfYears, 16 double loanAmount) { 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 newloanAmount */ 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 return loanAmount * monthlyInterestRate / ( 1 - 57 (Math.pow( 1 / ( 1 + monthlyInterestRate), numberOfYears * 12 ))); 58 } 59 60 /** Find total payment */ 61 public double getTotalPayment() { 62 return getMonthlyPayment() * numberOfYears * 12 ; 63 } 64 65 /** Return loan date */ 66 public java.util.Date getLoanDate() { 67 return loanDate; 68 } 69 } |
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 monthly payment and total payment. You can construct a Loan object by using the no-arg constructor or the one with three parameters: annual interest 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 annual interest rate, payment years, and loan amount, respectively. All the data properties and methods in this class are tied to a specific instance of the Loan class. Therefore, they are instance variables or methods.
Recall that the java.util.Date can be used to create an instance to represent current date and time in §7.5.1, "The Date Class." The Loan class contains the accessor method for loanDate , but no mutator method for it. Does this mean that the contents of loanDate cannot be changed? See Review Question 7.24.
Important Pedagogical Tip
The UML diagram for the Loan class is shown in Figure 7.23. Students should begin by writing a test program that uses the Loan class even though they don't know how the Loan class is implemented. This has three benefits:
For all the examples from now on, you may first create an object from the class and try to use its methods and then turn your attention to its implementation. |