Question 1a: Extending Geometric Shapes (2 points) Add the method area to the cl
ID: 3724041 • Letter: Q
Question
Question 1a: Extending Geometric Shapes (2 points) Add the method area to the classes Line and Rectangle that returns the area of the given shapes. The area of a line is always 0 class Shape: def setorigin(self, x, y): self.x, self.y- x, y def move(self, dx, dy): """Move by dx and dy"" self.x, self.y self.x + dx, self.ydy class Line(Shape): def-init--(self, x, y, u, v): "Line with start point (x, y) and end point (u, v)" Shape.setOrigin(self, x, y) self , u, self .v u, v def move(self, dx, dy): Shape.move(self, dx, dy) self , u, self.v self.u + dx, self .v + dy def area (self) pass # Replace this line with your body def str_(self): return "Line from"str(self.x) +", str(self.y) ") to (" str(self.u)", " str(self.v) + ")" class Rectangle(Shape): def _init__(self, x, y, w, h): Rectangle at (x, y) with width w and height h"" Shape.setOrigin(self, x, y) self , w, self . h w, h def area(self) pass # Replace this line with your body def str (self): return "Rectangle at (" str(self.x)", " + str(self.y) "), width"str(self.w) +", height "str(self.h) r Rectangle(-2, -3, 2, 3) 1 = Line(1, 2, 5, 6) str(r) "Rectangle at (-2, -3), width 2, str(1) "Line from (1, 2) to (5, 6)" height 3"Explanation / Answer
import math;
class Shape:
def setOrigin(self,x,y):
self.x,self.y = x,y
def move(self,dx,dy):
self.x,self.y = self.x + dx,self.y + dy
class Line(Shape):
def __init__(self,x,y,u,v):
Shape.setOrigin(self,x,y)
self.u,self.v = u,v
def move(self,dx,dy):
Shape.move(self,dx,dy)
self.u,self.v = self.u+dx,self.v+dy
def area(self):
return abs(self.u - self.x)+abs(self.v-self.y);
def __str__(self):
return "Line from ("+str(self.x)+", "+str(self.y)+") to ("+str(self.u)+", "+str(self.v)+")"
class Rectangle(Shape):
def __init__(self,x,y,w,h):
Shape.setOrigin(self,x,y)
self.w,self.h = w,h
def area(self):
return self.w*self.h;
def __str__(self):
return "Rectangle At ("+str(self.x)+", "+str(self.y)+") Width : "+str(self.w)+", Height : "+str(self.h)
class Circle(Shape):
def __init__(self,x,y,r):
Shape.setOrigin(self,x,y)
self.r = r
def area(self):
return math.pi*self.r*self.r;
def __str__(self):
return "Circle At ("+str(self.x)+", "+str(self.y)+") Radius : "+str(self.r)
class Polygon(Shape):
def __init__(self,x):
Shape.setOrigin(self,x[0][0],x[0][1])
self.cord = x
def move(self,dx,dy):
for i in range(0,len(self.cord)):
self.cord[i] = (self.cord[i][0]+dx,self.cord[i][1]+dy)
def area(self):
ar = 0.0;
for i in range(0,len(self.cord)-1):
ar = ar + abs(self.cord[i][0]*self.cord[i+1][1] - self.cord[i+1][0]*self.cord[i][1]);
return ar;
def __str__(self):
s = "Polygon At ("+str(self.x)+", "+str(self.y)+") Co-ordinates : "+str(self.cord)
return s
class Group:
def __init__(self):
self.c = set()
self.area = 0.0
def add(self,s):
self.c.add(s)
self.area = self.area + s.area()
def move(self,dx,dy):
for s in self.c:
s.move(dx,dy)
def area(self):
return self.area
def __str__(self):
r = "Group With:"
for s in self.c:
r = r + " " + str(s)
r = r + " Total Area : "+str(self.area);
return r
c = Circle(0,0,4)
l = Line(0,0,5,8)
r = Rectangle(0,5,6,8)
p = Polygon([(0,0),(1,0),(3,0),(5,6),(3,6),(3,0)])
g1 = Group()
g1.add(p)
g1.add(c)
g1.add(l)
g1.add(r)
print(g1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.