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

The purpose of this exercise is to create classes like class Circle from Chapter

ID: 3819943 • Letter: T

Question

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_0, y_0); and a general triangle specified by its three vertices (x_0, y_0), (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_ahapes.py. Write a test code after the class definition to demonstrate its usage.

Explanation / Answer

#!/usr/bin/python
import math # This will import math module
class triangle: #triangle class
def __init__(self, x0, y0, x1, y1, x2, y2): # initializing class variables
self.x0=x0
self.y0=y0
self.x1=x1
self.y1=y1
self.x2=x2
self.y2=y2
def triarea(self): # calculating area and circumference
a=(math.pow((self.x1-self.x0),2)+ math.pow((self.y1-self.y0),2))**(0.5)
b=(math.pow((self.x2-self.x1),2)+ math.pow((self.y2-self.y1),2))**(0.5)
c=(math.pow((self.x2-self.x0),2)+ math.pow((self.y2-self.y0),2))**(0.5)
s=0.5*(a+b+c);
areatri=(s*(s-a)*(s-b)*(s-c))**(0.5);
print (areatri);
circumtri=a+b+c;
print("circumference of triangle is :"% circumtri);
class rect:# rectangle class
def __intit__(self,height,width): # initializing class variables
self.height=height;
self.width=width;
def rectarea(self): # calculating area and circumference
arearect=self.width*self.height;
print (arearect);
circumrect=2*(self.width+self.height);
print("circumference of rectangle is :"%circumrect);
st="y"
while st == "y": # condition to continue with the program
ch=input("Enter your Shape: 1. Triangle 2. Rectangle"); # asking user the shape
if ch=="1": # Uses triangle.
tri=triangle(1,5,-2,2,6,-1) # This would create object of triangle class
print("the area of the trianngle" % tri.triarea()) # calling area method
elif ch=="2": # Uses rectangle.
rec=rectangle(12,10) # This would create object of rectangle class
print("the area of rectangle" % rec.rectarea()) # calling area method
st = input("Do you want to calculate another shape's area?: ") # This line allows the user the chance to do just what it says in the brackets.
if st == "n": #This is for when the user no longer wants to use the program.
print("Goodbye then!")
input("Press any key to close")
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote