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

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:

主站蜘蛛池模板: 锡林浩特市| 玉山县| 建始县| 哈巴河县| 志丹县| 江津市| 稷山县| 三台县| 嘉善县| 纳雍县| 浙江省| 彭州市| 长岭县| 都江堰市| 育儿| 汝州市| 金秀| 黄陵县| 绥中县| 铜梁县| 泸水县| 泰来县| 安溪县| 大方县| 黔江区| 陕西省| 马边| 马公市| 长阳| 柘荣县| 池州市| 十堰市| 柳江县| 巴南区| 广汉市| 大新县| 兴业县| 北碚区| 宜良县| 安福县| 安丘市|