Book Image

Learning Object-Oriented Programming

By : Gaston C. Hillar
Book Image

Learning Object-Oriented Programming

By: Gaston C. Hillar

Overview of this book

Table of Contents (16 chapters)
Learning Object-Oriented Programming
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Recognizing actions from verbs – methods


So far, we have designed four classes and identified the necessary attributes for each of them. Now, it is time to add the necessary pieces of code that work with the previously defined attributes to perform all the tasks. In other words, we have to make sure that each class has the necessary encapsulated functions that process the attribute values specified in the objects to perform all the tasks.

Let's start with the Square class. The application's requirements specified that we have to calculate the areas and perimeters of squares. Thus, we need pieces of code that allow each instance of this class to use the LengthOfSide value to calculate the area and the perimeter.

Tip

The functions or subroutines defined in a class to encapsulate the behavior for each instance of the class are known as methods. Each instance can access the set of methods exposed by the class. The code specified in a method is able to work with the attributes specified in the class. When we execute a method, it will use the attributes of the specific instance. A good practice is to define the methods in a logical place, that is, in the place where the required data is kept.

The Square class defines the following two parameterless methods. Notice that we declare the code for both methods in the definition of the Square class:

  • CalculateArea: This returns a floating-point value with the calculated area for the square. The method returns the square of the LengthOfSide attribute value (LengthOfSide2 or LengthOfSide ^ 2).

  • CalculatePerimeter: This returns a floating-point value with the calculated perimeter for the square. The method returns the LengthOfSide attribute value multiplied by 4 (4 * LengthOfSide).

Imagine that, our object-oriented programming language uses a dot (.) to allow us to execute methods of the instances. Remember that we had two instances of the Square class: square1 with LengthOfSide equal to 10 and square2 with LengthOfSide equal to 20. If we call square1.CalculateArea, it would return the result of 102, which is 100. On the other hand, if we call square2.CalculateArea, it would return the result of 202, which is 400. Each instance has a diverse value for the LengthOfSide attribute, and therefore, the results of executing the CalculateArea method are different.

If we call square1.CalculatePerimeter, it would return the result of 4 * 10, which is 40. On the other hand, if we call square2.CalculatePerimeter, it would return the result of 4 * 20, which is 80.

Now, let's move to the Rectangle class. We need exactly two methods with the same names specified for the Square class. However, they have to calculate the results in a different way.

  • CalculateArea: This returns a floating-point value with the calculated area for the rectangle. The method returns the result of the multiplication of the Width attribute value by the Height attribute value (Width * Height).

  • CalculatePerimeter: This returns a floating-point value with the calculated perimeter for the rectangle. The method returns the sum of two times the Width attribute value and two times the Height attribute value (2 * Width + 2 * Height).

Remember that, we had two instances of the Rectangle class: rectangle1 representing a 10 x 20 rectangle and rectangle2 representing a 30 x 50 rectangle. If we call rectangle1.CalculateArea, it would return the result of 10 * 20, which is 200. On the other hand, if we call rectangle2.CalculateArea, it would return the result of 30 * 50, which is 1500. Each instance has a diverse value for both the Width and Height attributes, and therefore, the results of executing the CalculateArea method are different.

If we call rectangle1.CalculatePerimeter, it would return the result of 2 * 10 + 2 * 20, which is 60. On the other hand, if we call rectangle2. CalculatePerimeter, it would return the result of 2 * 30 + 2 * 50, which is 160.

The Circle class also needs two methods with the same names. The two methods are explained as follows:

  • CalculateArea: This returns a floating-point value with the calculated area for the circle. The method returns the result of the multiplication of π by the square of the Radius attribute value (π * Radius2 or π * (Radius ^ 2)).

  • CalculatePerimeter: This returns a floating-point value with the calculated perimeter for the circle. The method returns the result of the multiplication of π by two times the Radius attribute value.

Finally, the Ellipse class defines two methods with the same names but with different code and a specific problem with the perimeter. The following are the two methods:

  • CalculateArea: This returns a floating-point value with the calculated area for the ellipse. The method returns the result of the multiplication of π by the square of the Radius attribute value (π * SemiMajorAxis * SemiMinorAxis).

  • CalculatePerimeter: This returns a floating-point value with the calculated approximation of the perimeter for the ellipse. Perimeters are very difficult to calculate for ellipses, and therefore, there are many formulas that provide approximations. An exact formula needs an infinite series of calculations. Thus, let's consider that the method returns the result of a formula that isn't very accurate and that we will have to improve on it later. The method returns the result of 2 * π * SquareRoot ((SemiMajorAxis2 + SemiMinorAxis2) / 2).

The following figure shows an updated version of the UML diagram with the four classes, their attributes, and their methods: