Question: Write the function in Python please Provide explanations for each line of code Provide snapshot of code Consider the following class definition: class MyPoint: def

Write the function in Python please Provide explanations for each line of code Provide snapshot of code

Consider the following class definition:

class MyPoint: def __init__(self, x=0, y=0): self.__x = x self.__y = y def __str__(self): return '({0}, {1})'.format(self.__x, self.__y) def __repr__(self): return 'MyPoint({}, {})'.format(self.__x, self.__y) def get_x(self): return self.__x def get_y(self): return self.__y def set_x(self, x): self.__x = x def set_y(self, y): self.__y = y def get_distance(self, other): return math.sqrt((self.get_x()-other.get_x())**2 + (self.get_y()-other.get_y())**2)

Question 1 a)

Define a class named MyLine by using the MyPoint class. A line is composed of two points. The MyLine class contains the following:

  • A private data field named __start_point of type MyPoint that defines the start point.
  • A private data field named __end_point of type MyPoint that defines the end point.
  • A constructor/initializer which takes 4 integers as parameters (start_x, start_y, end_x, end_y) and creates a line with two MyPoint objects. The default value for each coordinate is 0.
  • The __str__ method which returns a string representation of the object as in the examples below.
Test Result
line1 = MyLine(10, 20, 20, 30) print(line1) 
(10, 20) to (20, 30)
line1 = MyLine() print(line1) 
(0, 0) to (0, 0)
line1 = MyLine(start_x=20, start_y=30) print(line1) 
(20, 30) to (0, 0)

Question 1 b)

Extend the MyLine class by implementing the following methods:

  • the set_end_point(self, end_x, end_y) method which takes 2 integers as parameters and sets the end point of a line.
  • the get_end_point(self) method which returns the end point of a line.
  • the set_start_point(self, start_x, start_y) method which takes 2 integers as parameters and sets the start point of a line.
  • the get_start_point(self) method which returns the start point of a line.
Test Result
line1 = MyLine(10, 20, 20, 30) print(line1.get_start_point()) print(line1.get_end_point()) 
(10, 20) (20, 30)
line1 = MyLine() print(line1.get_start_point()) print(line1.get_end_point()) 
(0, 0) (0, 0)
line1 = MyLine() line1.set_start_point(100, 200) line1.set_end_point(-100, 40) print(line1)
(100, 200) to (-100, 40)
line1 = MyLine() line1.set_start_point(start_y=50, start_x=100) line1.set_end_point(end_y=200, end_x=100) print(line1)
(100, 50) to (100, 200)

Question 1 c)

Extend the MyLine class by implementing the get_line_length() method. The get_line_length(self) method returns the distance between the start point and the end point of a line. For example, the following code fragment:

line1 = MyLine(10, 20, 20, 30) print(line1.get_line_length()) # Use the distance() method 

produces

14.142135623730951
Test Result
line1 = MyLine(10, 20, 20, 30) print(line1) print('The length is {:.2f}.'.format(line1.get_line_length()))
(10, 20) to (20, 30) The length is 14.14.
line1 = MyLine() print('The length is {:.2f}.'.format(line1.get_line_length()))
The length is 0.00.
line1 = MyLine(start_x=10, start_y=20) print(line1) print('The length is {:.2f}.'.format(line1.get_line_length()))
(10, 20) to (0, 0) The length is 22.36.
line1 = MyLine() line1.set_start_point(start_x=10, start_y=10) line1.set_end_point(end_x=20, end_y=20) print(line1) print('The length is {:.2f}.'.format(line1.get_line_length()))
(10, 10) to (20, 20) The length is 14.14.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!