Write Code in Python class Line: \'\'\' Creates objects of the class Line, takes
ID: 3751371 • Letter: W
Question
Write Code in Python
class Line:
'''
Creates objects of the class Line, takes 2 tuples
>>> line1=Line((-7,-9),(1,5.6))
>>> line1.distance
16.648
>>> line1.slope
1.825
>>> line2=Line((2,6),(2,3))
>>> line2.distance
3.0
>>> line2.slope
'Infinity'
'''
def __init__(self, coord1, coord2):
#-- start code here ---
#-- ends here ---
#PROPERTY METHODS
def distance(self):
#-- start code here ---
#-- ends here ---
def slope(self):
#-- start code here ---
#-- ends here ---
[5 pts] Write the class Line that stores the coordinates of two points in a line and provides the distance between the two points and the slope of the line using the property methods called distance and slope. Unless the slope is equal to infinity, both methods must return the value as float. Tip: htt desa ntentW onte Coordinates must be provided as tuples when creating your class instances - Use the round method to format your output to 3 decimals [round(ouput value, 3) Incorrect format will result in -1 pt from your score EXAMPLES >>> line 1-Line( (8,3), (0,-4)) #Coordinates provided as tuple >>>linel.distance 10.63 >>>linel.slope 0.875 >>> line2-Line ( (-7,-9), (1, 5.6)) >>> line2.distance 16.648 >>> line2.slope 1.825 >>> line3-Line ( (2, 6), (2,3)) >>> line3.distance >>>line3.slope Infinity Quotes mean method returned a string, no need to append them in your code .Explanation / Answer
class Line: ''' Creates objects of the class Line, takes 2 tuples >>> line1=Line((-7,-9),(1,5.6)) >>> line1.distance 16.648 >>> line1.slope 1.825 >>> line2=Line((2,6),(2,3)) >>> line2.distance 3.0 >>> line2.slope 'Infinity' ''' def __init__(self, coord1, coord2): # -- start code here --- self.coord1 = coord1 self.coord2 = coord2 # -- ends here --- # PROPERTY METHODS @property def distance(self): # -- start code here --- return ((self.coord2[1] - self.coord1[1]) ** 2 + (self.coord2[0] - self.coord1[0]) ** 2) ** 0.5 # -- ends here --- @property def slope(self): # -- start code here --- try: return (self.coord2[1] - self.coord1[1]) / (self.coord2[0] - self.coord1[0]) except: return 'Infinity' # -- ends here --- line1 = Line((-7, -9), (1, 5.6)) print(line1.distance) print(line1.slope) line2 = Line((2, 6), (2, 3)) print(line2.distance) print(line2.slope)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.