Java Reference
In-Depth Information
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
interest rate, payment years, and loan amount, respectively. All the data properties and meth-
ods in this class are tied to a specific instance of the Loan class. Therefore, they are instance
variables and methods.
Important Pedagogical Tip
Use the UML diagram for the Loan class shown in Figure 10.4 to write a test program
that uses the Loan class even though you don't know how the Loan class is imple-
mented. This has three benefits:
It demonstrates that developing a class and using a class are two separate tasks.
It enables you to skip the complex implementation of certain classes without inter-
rupting the sequence of this topic.
It is easier to learn how to implement a class if you are familiar with it by using the class.
For all the class examples from now on, create an object from the class and try using its
methods before turning your attention to its implementation.
10.8
If you redefine the Loan class in Listing 10.2 without set methods, is the class
immutable?
Check
Point
10.6 Object-Oriented Thinking
The procedural paradigm focuses on designing methods. The object-oriented
paradigm couples data and methods together into objects. Software design using the
object-oriented paradigm focuses on objects and operations on objects.
Key
Point
Chapters 1-7 introduced fundamental programming techniques for problem solving using
loops, methods, and arrays. Knowing these techniques lays a solid foundation for object-
oriented programming. Classes provide more flexibility and modularity for building reusable
software. This section improves the solution for a problem introduced in Chapter 3 using the
object-oriented approach. From these improvements, you will gain insight into the differences
between procedural and object-oriented programming and see the benefits of developing
reusable code using objects and classes.
Listing 3.5, ComputeAndInterpretBMI.java, presented a program for computing body
mass index. The code cannot be reused in other programs, because the code is in the main
method. To make it reusable, define a static method to compute body mass index as follows:
public static double getBMI( double weight, double height)
This method is useful for computing body mass index for a specified weight and height. How-
ever, it has limitations. Suppose you need to associate the weight and height with a person's
name and birth date. You could declare separate variables to store these values, but these val-
ues would not be tightly coupled. The ideal way to couple them is to create an object that con-
tains them all. Since these values are tied to individual objects, they should be stored in
instance data fields. You can define a class named BMI as shown in Figure 10.5.
 
 
Search WWH ::




Custom Search