Question

In: Computer Science

Triangle Class write a class for triangle input is the length of the 3 edges you...

Triangle Class
write a class for triangle
input is the length of the 3 edges
you can use arccos: math.acos(you_input_here), the output is -pi to pi (in real value)

class math

class Triangle():
# initialize here
def __init__(xxx): # you need to modify this line
  
  
  
# your function angles is to output the 3 angles (in degree) in ascending order
def angles():
  
  
  
# is_equilateral outputs True if the triangle is equilateral
def is_equilateral():
  
  
  
# is_isosceles outputs True if the triangle is isosceles
def is_isosceles():
  
  
  
  
# if triangle A and B are similar, A.is_similar(B) returns True, False otherwise
def is_similar(xxx): # you need to modify this line

use python

Solutions

Expert Solution

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:


Related Solutions

Write class Hypotenuse that calculates the length of the hypotenuse of a right triangle and return...
Write class Hypotenuse that calculates the length of the hypotenuse of a right triangle and return to HypotenuseTest class. The lengths of the other two sides are given by the user by using HypotenuseTest class and send into Hypotenuse class. Hypotenuse class should take two arguments of type double and return the hypotenuse as a double into HypotenuseTest’s Main method. Finally, the HypotenuseTest class displays the hypotenuse side of a triangle.
An equilateral triangle is a triangle whose sides are equal. You are to write a class...
An equilateral triangle is a triangle whose sides are equal. You are to write a class called Triangle, using filenames triangle.h and triangle.cpp, that will allow the creation and handling of equilateral triangles, whose sides are integers in the range 1-39. Details: 1. The single constructor for the Triangle class should have 3 parameters: an integer size (required), which is the length of a side; a border character (optional, with a default of '#'); and a fill character (optional, with...
You have to write a program that computes the area of a triangle. Input consists of...
You have to write a program that computes the area of a triangle. Input consists of the three points that represent the vertices of the triangle. Points are represented as Cartesian units i.e. X,Y coordinates as in (3,5). Output must be the three points, the three distances between vertices, and the area of the triangle formed by these points. The program must read the coordinates of each point, compute the distances between each pair of them and print these values....
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Write in c++ as simple as possible In a right triangle, the square of the length...
Write in c++ as simple as possible In a right triangle, the square of the length on one side is equal to the sum of the squares of the lengths of the other two sides.  Write a program that prompts the user to enter the length of three sides of the tringle (as doubles) and the outputs a message indication whether the triangle is a right triangle.  You should split this into two functions (but only one .cpp file).  One function is main()...
Write a program that prompts the user for the length of one side of a triangle...
Write a program that prompts the user for the length of one side of a triangle and the sizes of the two adjacent angles in degrees and then displays the length of the two other sides and the size of the third angle.
Given a right triangle with one leg length of a, one leg length of x-3 and...
Given a right triangle with one leg length of a, one leg length of x-3 and the hypo is x, how does a affect x? Shown on a graph. I solved for a, it is equal to the square root of 6x-9.... I am having a difficult time interpreting how a affects x.. I am not sure how to graph
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
1. Given a string of at least 3 characters as input, if the length of the...
1. Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. public class Class1 {    public static String midString(String str) { //Enter code here } } ----------- 2. Given an array of integers return the sum of the values stored in the first and last index of the array. The...
Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT