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

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:

主站蜘蛛池模板: 特克斯县| 连云港市| 且末县| 辰溪县| 和政县| 连城县| 义乌市| 江北区| 黔西| 巴中市| 台中县| 松滋市| 乐亭县| 乡城县| 嵊泗县| 拉孜县| 隆化县| 常州市| 韩城市| 开阳县| 武强县| 铁力市| 余姚市| 墨江| 涞源县| 珲春市| 富宁县| 通渭县| 阳朔县| 惠安县| 威宁| 益阳市| 梁山县| 武山县| 江西省| 甘德县| 平舆县| 大兴区| 灵武市| 额尔古纳市| 吉林市|