国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網 會員登陸 & 注冊

加拿大瑞爾森大學 Python習題 Lab 7

2023-08-24 15:57 作者:逃跑的叮當貓  | 我要投稿

Lab?7 –?CPS?106

?

?

This lab helps you get familiar with classes and inheritance.

1. ??Write?a?class?called?Shape.

class Shape:

2. ??The Shape class is?going to?represent?a?general?shape?in?the?2D?plane.?A?shape?is?described?by?a set of?points that determine its boundary. Write?a?class?called Point. The?point?class?has?two?attributes?x?and?y?which?are?unique?to?each?point.?So?we?define?them?as?instance?variables,?i.e., using the self?object. We initialize them in the __init__ mathod.

class Point(x,?y):

def __init__(self, x, y):

self.x?=?x

self.y?=?y

?

3. ??Write ?the?__str__?method?for?the?Point?class.?The?method ?should?return?a ?string?in?the?following format: “(x, y)”

?

4. ??Test the point class using the following:

p?=?Point(31,?7)

print(p)

?

5. ??Add?the?__init__?method?to?the?shape?class.?In?the?method?you?should?initialize?the?list?of?boundary points to empty:

self.boundary =?[]

?

6. ??Add a method called?set_boundary?to the?Shape?class.?The?set?boundary?method?accepts?a?list?of?points called boundary. In the method you should set self.boundary to the input boundary:

def set_boundary(boundary):

?

7. ??Write?the?__str__?method?for?Shape.?The?method?should?return?a?string?in?the?following?format:?“Boundary: (x1, y1), (x2, y2),?...” where the?set of?points?are?the?ones?in?self.boundary.?Hint1:?remember?that?the?point?class?gives?you?a?string?of the?form?“(x,?y)”,?so?you?can?use?that here. For?example?if self.boundary[0]?is?the point p?=?(5,?12)?then?str(p)?is?the?string?“(5,

12)”. Hint2: you should loop through all the points in self.boundary to get the final?result.

8. ??Define a class called Rectangle.?The?Rectangle?class?will?be?a?subclass?of?Shape:

class Rectangle(Shape):

pass

?

9. ??Test?the?rectangle?class?using?the?following.?The?first?point?is?assumed?to?be?the?bottom?left?corner of?the rectangle.


?

?

?

rect =?Rectangle()

rect.set_boundary([Point(0, 0), Point(0, 3), Point(5,?3),?Point(5,?0)])

print(rect)

?

10.?Define a class called Square. The Square?class will be?a?subclass?of?Rectangle:

class Square(Rectangle):

pass

?

Test?the?Square?class?using?the?following.?The?first?point?is?assumed?to?be?the?bottom?left?corner of?the square.

square =?Square()

square.set_boundary([Point(0, 0), Point(0, 3), Point(3,?3),?Point(3, 0)])

print(square)

?

11.?Define a class called Triangle. The Triangle class will be?a?subclass?of?Shape.?Test?the Triangle?class using the following:

trgle =?Triangle()

p(t)ri(rg)t(e)tgle(set_)oundary([Point(0, 7), Point(3, 3),?Point(2,?5)])

?

12.?The diagram of?the classes now looks like the following:

?



?

13.?Add the following method to the Square class:

def set_side_length(length):

?

The argument length is an integer, based on which the new boundary?point?of?the?square?will?be?calculated.?Since?the?first?point,?i.e.,?p?=?self.boundary[0],?is?the?bottom?left?corner,?the?point?above p, i.e.,?self.boundary[1], will be?(p.x,?p.y?+?length).?Figure?out?the?position?of the?rest of?the points. Test your code using the following:

square.set_side_length(5)

print(square)


代碼

7-1

7-2

7-13 class


加拿大瑞爾森大學 Python習題 Lab 7的評論 (共 條)

分享到微博請遵守國家法律
阳朔县| 定远县| 南和县| 沾化县| 浦北县| 江陵县| 宜春市| 民和| 望谟县| 白朗县| 衡山县| 德庆县| 阳曲县| 延安市| 长沙县| 昆山市| 凤山县| 南平市| 沂水县| 蛟河市| 潞西市| 明光市| 梅河口市| 大庆市| 望奎县| 集贤县| 巴林右旗| 镇雄县| 鞍山市| 新兴县| 五台县| 始兴县| 故城县| 岳阳市| 肃南| 区。| 霍林郭勒市| 玉门市| 鄂托克前旗| 志丹县| 隆林|