In: Computer Science
Completed Python code is provided below, please comment if any doubts:
Note: The indentation of the code may lost on copying, if so refer the screenshot to reconstruct the indentation
Python code:
import math
class Triangle():
# initialize here
def __init__(self, a, b, c): # you need to modify this line
self.a=a
self.b=b
self.c=c
# your function angles is to output the 3 angles (in degree) in
ascending order
def angles(self):
#find the first angle
ca=(self.a*self.a+self.b*self.b-(self.c*self.c))/(2*self.a*self.b)
ca1=math.acos(ca)
cad=ca1*180/math.pi
#find the second angle
ba=(self.a*self.a+self.c*self.c-(self.b*self.b))/(2*self.a*self.c)
ba1=math.acos(ba)
bad=ba1*180/math.pi
#find the third angle
aa=(self.c*self.c+self.b*self.b-(self.a*self.a))/(2*self.b*self.c)
aa1=math.acos(aa)
aad=aa1*180/math.pi
print("The angles are: ")
#print in ascending order
if(cad>= bad and cad <=aad):
print(bad)
print(cad)
print(aad)
elif(cad<= bad and cad <=aad):
print(cad)
print(bad)
print(aad)
elif(cad<= aad and cad <=bad):
print(bad)
print(aad)
print(cad)
elif(cad>= aad and cad <=bad):
print(aad)
print(cad)
print(bad)
elif(cad>= aad and aad <=bad):
print(aad)
print(bad)
print(cad)
else:
print(bad)
print(aad)
print(cad)
# is_equilateral outputs True if the triangle is equilateral
def is_equilateral(self):
if(self.a == self. b and self.b==self.c):
return True
else:
return False
# is_isosceles outputs True if the triangle is isosceles
def is_isosceles(self):
if(self.a == self. b or self.b==self.c or self.a==self.c):
return True
else:
return False
# if triangle A and B are similar, A.is_similar(B) returns True,
False otherwise
def is_similar(self, T1): # you need to modify this line
#check if the sides are proptional
if((self.a/self.b)==(T1.a/T1.b) or (self.c/self.b)==(T1.a/T1.b) or
(self.a/self.c)==(T1.a/T1.b)):
return True;
elif((self.b/self.a)==(T1.a/T1.b) or (self.b/self.c)==(T1.a/T1.b)
or (self.c/self.a)==(T1.a/T1.b)):
return True;
else:
return False
##the test cases of the class
T1=Triangle(10, 5, 6)
T2=Triangle(5, 5, 5)
T3=Triangle(6, 8, 10)
T4=Triangle(3, 4, 5)
print(T1.is_equilateral())
print(T2.is_equilateral())
print(T2.is_isosceles())
print(T3.is_isosceles())
T1.angles()
T2.angles()
T3.angles()
print(T3.is_similar(T4))
Output Test:
Code screenshot: