Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING PYTHON Make classes for a rectangle and a triangle. The purpose of this ex

ID: 3815287 • Letter: U

Question

USING PYTHON

Make classes for a rectangle and a triangle. The purpose of this exercise is to create classes like class circle from Chapter 7.2.3 for representing other geometric figures: a rectangle with width W, height H, and lower left corner (x_o, y_o); and a general triangle specified by its three vertices (x_o, y_o), (x_1, y_1), and (x_2, y_2) as explained in Exercise 3.3. Provide three methods: -- init-- (to initialize the geometric data), area, and circumference. Name of program file geometric_ shapes.py Write a test code after the class definition to demonstrate its usag.

Explanation / Answer

Please find the required solution:

import math
  
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
  
  
class Rectangle:
def __init__(self, w, h, lc):
self.w = w;
self.h = h;
self.lc = lc;
  
def area(self):
return self.w * self.h;
  
def circumference(self):
return 2*(self.w + self.h);

# test case for class rectangle
  
point = Point(3.0, -4.5)

rectangle = Rectangle(10,20, point);

print "Area of rectangle:",rectangle.area();
print "Circumference of rectangle:",rectangle.circumference();

class Traingle:
def __init__(self, pt1, pt2, pt3):
self.pt1 = pt1;
self.pt2 = pt2;
self.pt3 = pt3;
  
def area(self):
a = ((self.pt1.x * (self.pt2.y -self.pt3.y) + self.pt2.x * (self.pt3.y -self.pt1.y) + self.pt3.x * (self.pt1.y - self.pt2.y))/2);
if( a < 0):
return -a;
return a;
  
def circumference(self):
length1 = math.sqrt((self.pt1.x - self.pt2.x) * (self.pt1.x- self.pt2.x) + (self.pt1.y- self.pt2.y) * (self.pt1.y- self.pt2.y));
length2 = math.sqrt((self.pt2.x - self.pt3.x) * (self.pt2.x- self.pt3.x) + (self.pt2.y- self.pt3.y) * (self.pt2.y- self.pt3.y));
length3 = math.sqrt((self.pt3.x - self.pt1.x) * (self.pt3.x- self.pt1.x) + (self.pt3.y- self.pt1.y) * (self.pt3.y- self.pt1.y));
return length1 + length2 +length3;
  

# test case for class traingle
point1 = Point(0.0, 0.0)
point2 = Point(0.0, 0.2)
point3 = Point(2.0, 0.0)

traingle = Traingle(point1, point2, point3);

print "Area of traingle:",traingle.area();
print "Circumference of rectangle:",traingle.circumference();