<< Chapter < Page | Chapter >> Page > |
Abstraction is the process of hiding the details and exposing only the essential features of a particular concept or object. Computer scientists use abstraction to understand and solve problems and communicate their solutions with the computer in some particular computer language. We illustrate this process by way of trying to solve the following problem using a computer language called Java.
Problem : Given a rectangle 4.5 ft wide and 7.2 ft high, compute its area.
We know the area of a rectangle is its width times its height. So all we have to do to solve the above problem is to multiply 4.5 by 7.2 and get the the answer. The question is how to express the above solution in Java, so that the computer can perform the computation.
The product of 4.5 by 7.2 is expressed in Java as:
4.5 * 7.2
. In this expression, the symbol
*
represents the multiplication operation. 4.5 and 7.2 are called number
literals . Using DrJava, we can type in the expresssion
4.5 * 7.2
directly in the interactions window and see the answer.
Now suppose we change the problem to compute the area of a rectangle of width 3.6 and height 9.3. Has the original problem really change at all? To put it in another way, has the essence of the original problem changed? After all, the formula for computing the answer is still the same. All we have to do is to enter 3.6 * 9.3. What is it that has not change (the invariant )? And what is it that has changed (the variant )?
The problem has not changed in that it still deals with the same geometric shape, a rectangle, described in terms of the same dimensions, its width and height. What vary are simply the values of the width and the height. The formula to compute the area of a rectangle given its width and height does not change:
width * height
It does not care what the actual specific values of width and height are. What it cares about is that the values of width and height must be such that the multiplication operation makes sense. How do we express the above invariants in Java?
We just want to think of the width and height of a given rectangle as elements of the set of real numbers. In computing, we group values with common characteristics into a set and called it a type . In Java, the type double is the set of real numbers that are implemented inside the computer in some specific way. The details of this internal representation is immaterial for our purpose and thus can be ignored. In addition to the type double, Java provides many more pre-built types such as int to represent the set of integers and char to represent the set of characters. We will examine and use them as their need arises in future examples. As to our problem, we only need to restrict ourselves to the type double.
We can define the width and the height of a rectangle as double in Java as follows.
double width;
double height;
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?