Java Reference
In-Depth Information
To compute the sum all integers between 40 and 60 , you can execute the following snippet of code:
int s2 = getRangeSum(40, 60);
System.out.println(s2);
This snippet of code will print 1050 , which is exactly the same result you had achieved before.
The abstraction method that you used in defining the getRangeSum procedure is called abstraction by
parameterization. The formal parameters in a procedure are used to hide the identity of the actual data on which
the procedure's body operates. The two parameters in the getRangeSum procedure hide the identity of the upper and
lower limits of the range of integers. Now you have seen the first concrete example of abstraction. Abstraction is a vast
topic. I will cover some more basics about abstraction in this section.
Suppose a programmer writes the code for the getRangeSum procedure as shown in Listing 1-2 and another
programmer wants to use it. The first programmer is the designer and writer of the procedure; the second one is the
user of the procedure. What pieces of information does the user of the getRangeSum procedure need to know in order
to use it?
Before you answer this question, let's consider a real-world example of designing and using a DVD player
(Digital Versatile Disc player). A DVD player is designed and developed by electronic engineers. How do you use a
DVD player? Before you use a DVD player, you do not open it to study all the details about its parts that are based on
electronics engineering theories. When you buy it, it comes with a manual on how to use it. A DVD player is wrapped
in a box. The box hides the details of the player inside. At the same time, the box exposes some of the details about the
player in the form of an interface to the outside world. The interface for a DVD player consists of the following items:
Input and output connection ports to connect to a power outlet, a TV set, etc.
A panel to insert a DVD
A set of buttons to perform operations such as eject, play, pause, fast forward, etc.
The manual that comes with the DVD player describes the usage of the player's interface meant for its users. A
DVD user need not worry about the details of how it works internally. The manual also describes some conditions to
operate it. For example, you must plug the power cord to a power outlet and switch on the power before you can use it.
A program is designed, developed, and used in the same way as a DVD player. The user of the program, shown
in Listing 1-1, need not worry about the internal logic that is used to implement the program. A user of the program
needs to know only its usage, which includes the interface to use it, and conditions that must be met before and after
using it. In other words, you need to provide a manual for the getRangeSum procedure that will describe its usage. The
user of the getRangeSum procedure will need to read its manual to use it. The “manual” for a program is known as its
specification. Sometimes it is also known as documentation or comments. It provides another method of abstraction,
which is called abstraction by specification. It describes (or exposes or focuses) the “what” part of the program and
hides (or ignores or suppresses) the “how” part of the program from its users.
Listing 1-3 shows the same getRangeSum procedure code with its specification.
Listing 1-3. The getRangeSum Procedure with its Specification for Javadoc Tool
/**
* Computes and returns the sum of all integers between two
* integers specified by lowerLimit and upperLimit parameters.
*
* The lowerLimit parameter must be less than or equal to the
* upperLimit parameter. If the sum of all integers between the
* lowerLimit and the upperLimit exceeds the range of the int data
* type then result is not defined.
*
 
Search WWH ::




Custom Search