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

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:

主站蜘蛛池模板: 神池县| 屯留县| 保亭| 青岛市| 嘉善县| 连云港市| 沂源县| 兴海县| 郧西县| 休宁县| 乳源| 商丘市| 南木林县| 双峰县| 卢湾区| 天全县| 尤溪县| 普宁市| 苏尼特右旗| 克东县| 顺义区| 云浮市| 惠水县| 彩票| 扶沟县| 理塘县| 民乐县| 天柱县| 股票| 刚察县| 兴安盟| 南汇区| 大同市| 连南| 蒲江县| 潼南县| 常德市| 孝昌县| 原阳县| 嵊州市| 莎车县|