官术网_书友最值得收藏!

Recognizing objects from nouns

Let's imagine, we have to develop a new simple application, and we receive a description with the requirements. The application must allow users to calculate the areas and perimeters of squares, rectangles, circles, and ellipses.

It is indeed a very simple application, and you can start writing code in Python, JavaScript, and C#. You can create four functions that calculate the areas of the shapes mentioned earlier. Moreover, you can create four additional functions that calculate the perimeters for them. For example, the following seven functions would do the job:

  • calculateSquareArea: This receives the parameters of the square and returns the value of the calculated area for the shape
  • calculateRectangleArea: This receives the parameters of the rectangle and returns the value of the calculated area for the shape
  • calculateCircleArea: This receives the parameters of the circle and returns the value of the calculated area for the shape
  • calculateEllipseArea: This receives the parameters of the ellipse and returns the value of the calculated area for the shape
  • calculateSquarePerimeter: This receives the parameters of the square and returns the value of the calculated perimeter for the shape
  • calculateRectanglePerimeter: This receives the parameters of the rectangle and returns the value of the calculated perimeter for the shape
  • calculateCirclePerimeter: This receives the parameters of the circle and returns the value of the calculated perimeter for the shape

However, let's forget a bit about programming languages and functions. Let's recognize the real-world objects from the application's requirements. It is necessary to calculate the areas and perimeters of four elements, that is, four nouns in the requirements that represent real-life objects:

  • Square
  • Rectangle
  • Circle
  • Ellipse

We can design our application by following an object-oriented paradigm. Instead of creating a set of functions that perform the required tasks, we can create software objects that represent the state and behavior of a square, rectangle, circle, and an ellipse. This way, the different objects mimic the real-world shapes. We can work with the objects to specify the different attributes required to calculate their areas and their perimeters.

Now, let's move to the real world and think about the four shapes. Imagine that you have to draw the four shapes on paper and calculate both their areas and perimeters. What information do you require for each of the shapes? Think about this, and then, take a look at the following table that summarizes the data required for each shape:

Tip

The data required by each of the shapes is going to be encapsulated in each object. For example, the object that represents a rectangle encapsulates both the rectangle's width and height. Data encapsulation is one of the major pillars of object-oriented programming.

The following diagram shows the four shapes drawn and their elements:

主站蜘蛛池模板: 泸定县| 万山特区| 山阳县| 睢宁县| 黄平县| 册亨县| 锡林浩特市| 锡林浩特市| 海林市| 郑州市| 双辽市| 墨玉县| 湖南省| 凌海市| 喜德县| 祁东县| 松阳县| 微博| 那曲县| 舒兰市| 沈丘县| 淳化县| 万源市| 台中市| 乌审旗| 伊川县| 太保市| 景洪市| 玉龙| 德清县| 万源市| 仪陇县| 临潭县| 屏南县| 宝兴县| 洞口县| 奉节县| 大同县| 乐亭县| 二连浩特市| 桐庐县|