实验课题目: 1、重写醉汉行走问题。使用Point类,每一步可以看作是利用随机参数创建的一个Point对象。Point类可能需要适当修改。 2、写一个Rectangle类,描述一个矩形。 具备以下域: 四个顶点的坐标leftTop,rightTop,leftFloor,rightFloor,以Point对象表示; 图形的周长; 图形的面积。 构造器: 无参构造器 Rectangle(),生成一个四个顶点均为(0.0,0.0)的矩形; Rectangle(double x1, double y1, double x2, double y2),其中(x1, y1)是一个顶点,其中(x2, y2)是对角线上另一个顶点; Rectangle(Point p1,Point p2),p1、p2是对角线上的两个顶点。 设计如下方法: Point getLeftTop(); Point getLeftFloor(); Point getRightTop(); Point getRightFloor(); 返回周长; 返回面积。 写一个测试类,测试Rectangle类。
时间: 2023-04-12 11:00:32 浏览: 107
好的,让我来回答你的问题。
对于第一个问题,我可以回答。以下是重写醉汉行走问题的代码:
```python
import random
class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
class Drunk:
def __init__(self, name):
self.name = name
self.current_position = Point()
def walk(self):
x_step = random.uniform(-1, 1)
y_step = random.uniform(-1, 1)
self.current_position.x += x_step
self.current_position.y += y_step
def get_position(self):
return self.current_position
def __str__(self):
return self.name
def main():
drunk = Drunk('Jack')
for i in range(10):
drunk.walk()
print(drunk.get_position())
if __name__ == '__main__':
main()
```
对于第二个问题,我也可以回答。以下是Rectangle类的代码:
```python
import math
class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
class Rectangle:
def __init__(self, *args):
if len(args) == 0:
self.left_top = Point()
self.right_top = Point()
self.left_floor = Point()
self.right_floor = Point()
elif len(args) == 2:
self.left_top = args[0]
self.right_floor = args[1]
self.right_top = Point(self.right_floor.x, self.left_top.y)
self.left_floor = Point(self.left_top.x, self.right_floor.y)
elif len(args) == 4:
self.left_top = Point(args[0], args[1])
self.right_floor = Point(args[2], args[3])
self.right_top = Point(self.right_floor.x, self.left_top.y)
self.left_floor = Point(self.left_top.x, self.right_floor.y)
def get_left_top(self):
return self.left_top
def get_left_floor(self):
return self.left_floor
def get_right_top(self):
return self.right_top
def get_right_floor(self):
return self.right_floor
def get_perimeter(self):
width = abs(self.right_floor.x - self.left_top.x)
height = abs(self.right_floor.y - self.left_top.y)
return 2 * (width + height)
def get_area(self):
width = abs(self.right_floor.x - self.left_top.x)
height = abs(self.right_floor.y - self.left_top.y)
return width * height
def main():
rectangle1 = Rectangle()
print(rectangle1.get_left_top())
print(rectangle1.get_left_floor())
print(rectangle1.get_right_top())
print(rectangle1.get_right_floor())
print(rectangle1.get_perimeter())
print(rectangle1.get_area())
rectangle2 = Rectangle(1, 1, 3, 3)
print(rectangle2.get_left_top())
print(rectangle2.get_left_floor())
print(rectangle2.get_right_top())
print(rectangle2.get_right_floor())
print(rectangle2.get_perimeter())
print(rectangle2.get_area())
rectangle3 = Rectangle(Point(1, 1), Point(3, 3))
print(rectangle3.get_left_top())
print(rectangle3.get_left_floor())
print(rectangle3.get_right_top())
print(rectangle3.get_right_floor())
print(rectangle3.get_perimeter())
print(rectangle3.get_area())
if __name__ == '__main__':
main()
```
最后,我希望我的回答能够帮助到你。
阅读全文