Java Reference
In-Depth Information
computing the monthly payment and total payment are its methods. When you buy a car, a loan
object is created by instantiating the class with your loan interest rate, loan amount, and loan
period. You can then use the methods to find the monthly payment and total payment of your
loan. As a user of the Loan class, you don't need to know how these methods are implemented.
Listing 2.8, ComputeLoan.java, presented a program for computing loan payments. That
program cannot be reused in other programs because the code for computing the payments is
in the main method. One way to fix this problem is to define static methods for computing the
monthly payment and total payment. However, this solution has limitations. Suppose you wish
to associate a date with the loan. There is no good way to tie a date with a loan without using
objects. The traditional procedural programming paradigm is action-driven, and data are sepa-
rated from actions. The object-oriented programming paradigm focuses on objects, and actions
are defined along with the data in objects. To tie a date with a loan, you can define a loan class
with a date along with other of the loan's properties as data fields. A loan object now contains
data and actions for manipulating and processing data, and the loan data and actions are inte-
grated in one object. Figure 10.4 shows the UML class diagram for the Loan class.
VideoNote
The Loan class
Loan
-annualInterestRate: double
-numberOfYears: int
-loanAmount: double
-loanDate: java.util.Date
The annual interest rate of the loan (default: 2.5).
The number of years for the loan (default: 1).
The loan amount (default: 1000).
The date this loan was created.
+Loan()
Constructs a default Loan object.
+Loan(annualInterestRate: double,
numberOfYears: int,loanAmount:
double)
+getAnnualInterestRate(): double
Constructs a loan with specified interest rate, years,
and loan amount.
Returns the annual interest rate of this loan.
+getNumberOfYears(): int
+getLoanAmount(): double
+getLoanDate(): java.util.Date
Returns the number of the years of this loan.
Returns the amount of this loan.
Returns the date of the creation of this loan.
+setAnnualInterestRate(
annualInterestRate: double): void
Sets a new annual interest rate for this loan.
+setNumberOfYears(
numberOfYears: int): void
+setLoanAmount(
loanAmount: double): void
+getMonthlyPayment(): double
Sets a new number of years for this loan.
Sets a new amount for this loan.
Returns the monthly payment for this loan.
+getTotalPayment(): double
Returns the total payment for this loan.
F IGURE 10.4
The Loan class models the properties and behaviors of loans.
The UML diagram in Figure 10.4 serves as the contract for the Loan class. Throughout
this topic, you will play the roles of both class user and class developer. Remember that a
class user can use the class without knowing how the class is implemented.
Assume that the Loan class is available. The program in Listing 10.1 uses that class.
L ISTING 10.1 TestLoanClass.java
1 import java.util.Scanner;
2
3 public class TestLoanClass {
4
/** Main method */
5
public static void main(String[] args) {
 
 
Search WWH ::




Custom Search