Capturing real-world objects
We could easily recognize objects from Pitstop's artwork. We understood that each pattern is composed of many 2D geometric shapes and we recognized the different shapes that she used in all the examples we analyzed. Now, let's focus on one of the core requirements for the Web Service, which is calculating the required amounts of acrylic paint to produce the artwork. We must take into account the following data for each 2D shape included in the pattern in order to calculate the required materials and the amount of acrylic paint to produce each shape:
- The line color
- The perimeter
- The fill color
- The area
It is possible to use a specific color for the line that draws the borders of each shape, and therefore, we have to calculate the perimeter to use it as one of the values that will allow us to estimate the amount of acrylic paint that the user must buy to paint the border of each 2D shape. Then, we have to calculate the area to use it as one of the values that will allow us to estimate the amount of acrylic paint that the user must buy to fill the area of each 2D shape.
We have to start working on the backend code for our Web Service that calculates areas and perimeters for the different 2D shapes we have recognized in all the sample artwork we have analyzed so far. We conclude that the Web Service must support patterns with the following nine shapes:
- Circles
- Ellipses
- Equilateral triangles
- Squares
- Rectangles
- Regular pentagons
- Regular hexagons
- Regular octagons
- Regular decagons
After doing some research to refresh our minds about 2D geometry, we can start writing Java 9 code. Specifically, we might write nine methods that calculate the areas of the previously enumerated 2D shapes and another nine to calculate their perimeters. Note that we are talking about methods that would return the calculated value, also known as functions. We stopped thinking about objects, and therefore, we will face some problems with this path, which we will solve with an object-oriented approach.
For example, if we start thinking about methods to solve the problem, one possible solution is to code the following eighteen functions to do the job:
calculateCircleArea
calculateEllipseArea
calculateEquilateralTriangleArea
calculateSquareArea
calculateRectangleArea
calculateRegularPentagonArea
calculateRegularHexagonArea
calculateRegularOctagonArea
calculateRegularDecagonArea
calculateCirclePerimeter
calculateEllipsePerimeter
calculateEquilateralTrianglePerimeter
calculateSquarePerimeter
calculateRectanglePerimeter
calculateRegularPentagonPerimeter
calculateRegularHexagonPerimeter
calculateRegularOctagonPerimeter
calculateRegularDecagonPerimeter
Each of the previously enumerated methods has to receive the necessary parameters of each shape and return either its calculated area or perimeter. These functions do not have side effects, that is, they do not make changes to the parameters they receive and they just return the results of the calculated areas or perimeters.
Now, let's forget about methods or functions for one moment. Let's go back to the real-world objects from the Web Service requirements that we were assigned. We have to calculate the areas and perimeters of nine elements, which are nine nouns in the requirements that represent real-life objects, specifically 2D shapes. We have already built a list with nine real-world objects.
After recognizing the real-life objects and thinking a bit about them, we can start designing our Web Service by following an object-oriented paradigm. Instead of creating a set of methods that perform the required tasks, we can create software objects that represent the state and behavior of each of the enumerated 2D shapes. This way, the different objects mimic the real-world 2D shapes. We can work with the objects to specify the different attributes required to calculate the area and perimeter. Then, we can extend these objects to include the additional data required to calculate other required values, such as the quantity of acrylic paint required to paint the borders.
Now, let's move to the real world and think about each of the previously enumerated nine shapes. Imagine that we have to draw each of the shapes on paper and calculate their areas and perimeters. After we draw each shape, which values will we use to calculate their areas and perimeters? Which formulas will we use?
Tip
We started working on an object-oriented design before we started coding, and therefore, we will work as if we didn't know many concepts of geometry. For example, we can easily generalize the formulas that we use to calculate the perimeters and areas of regular polygons. However, in most cases we won't be experts on the subject and we have to gain some knowledge on the application domain before we can generalize behavior with an object-oriented approach. Thus, we will pe deeper into the subject as if we had little knowledge on the topic.
The following figure shows a drawn circle and the formulas that we will use to calculate its perimeter and area. We just need the radius value, usually identified as r.
The following figure shows a drawn ellipse and the formulas that we will use to calculate its perimeter and area. We need the values for the semimajor axis (usually labelled as a) and semiminor axis (usually labelled as b). Notice that the formula provided for the perimeter provides an approximation that is not very accurate. We will pe deeper on this specific problem later.
The following figure shows a drawn equilateral triangle and the formulas that we will use to calculate its perimeter and area. This type of triangle has equal sides, and the three internal angles are equal to 60 degrees. We just need the length of side value, usually identified as a.
The following figure shows a drawn square and the formulas that we will use to calculate its perimeter and area. We just need the length of side value, usually identified as a.
The following figure shows a drawn rectangle and the formulas that we will use to calculate its perimeter and area. We need the width and height values, usually identified as w and h.
The following figure shows a drawn regular pentagon and the formulas that we will use to calculate its perimeter and area. We just need the length of the side value, usually labelled as a.
The following figure shows a drawn regular hexagon and the formulas that we will use to calculate its perimeter and area. We just need the length of the side value, usually labelled as a.
The following figure shows a drawn regular octagon and the formulas that we will use to calculate its perimeter and area. We just need the length of the side value, usually labelled as a.
The following figure shows a drawn regular decagon and the formulas that we will use to calculate its perimeter and area. We just need the length of the side value, usually labelled as a.
The following table summarizes the data required for each shape to calculate its perimeter and area:
Each object that represents a specific shape encapsulates the required data that we identified. For example, an object that represents an ellipse will encapsulate the ellipse's semimajor and semiminor axes values, while an object that represents a rectangle will encapsulate the rectangle's width and height values.
Note
Data encapsulation is one of the major pillars of object-oriented programming.