8.9k views
3 votes
Remember that all data members should be private. An object can access it's own private data members directly. It can also access the private data members of other objects of the same class directly. But when accessing a private data member of an object of another class, it needs to call the appropriate get method. If that sounds complicated, just remember this: if a method is in the same class as a private data member, then it can access that data member directly, otherwise, it needs to use a get method (see the hints at the end).

Write a class named Point that has two data members, x_coord and y_coord, representing the two coordinates of the point. It should have:

an init method that takes two arguments, an x-coordinate and y-coordinate (in that order), and uses them to initialize the two data members.
get methods for the two data members: get_x_coord and get_y_coord.
a method named distance_to that takes a Point object as an argument. It returns the distance between the Point object the method is called on (the one in front of the dot) and the Point object passed as the argument. For example, if point_1 and point_2 are both Point objects, then point_1.distance_to(point_2) should return the distance between point_1 and point_2 (and point_2.distance_to(point_1) should return the same distance). For this method, you'll calculate the distance using the following formula:
formula for distance between two points where d is the distance and the two points are (x1, y1) and (x2, y2).

The above formula requires taking the square root. As you might recall from Project 6c, you can do by just using an exponent of 0.5. For example, the result of 9 ** 0.5 would be 3.0. Python does have a specific sqrt() function, but that involves importing a module, which we haven't covered yet.

Now write a class named LineSegment that has two data members, endpoint_1 and endpoint_2, representing the two endpoints of the line segment. It should have:

an init method that takes two Point objects as arguments and uses them to initialize the two data members (the endpoints of the LineSegment).
get methods for the two data members: get_endpoint_1 and get_endpoint_2.
a method named length that takes no arguments and returns the distance between its two endpoints. This can make use of the Point's distance_to method.
a method named is_parallel_to that takes a LineSegment object as an argument. It returns True if the LineSegment the method is being called on is parallel to the one being passed as the argument. Otherwise, it should return False. For example, if line_seg_1 and line_seg_2 are both LineSegment objects, then line_seg_1.is_parallel_to(line_seg_2) should return True if those two line segments are parallel, but otherwise should return False. For this method, you'll calculate the slopes of the two LineSegments and compare them. If the two slopes are equal, then the two line segments are parallel, otherwise they are not. This can make use of the following slope method. Remember that you shouldn't test two floats for exact equality because of possible lack of precision or round-off errors. Instead, if you need to compare float values for equality, you can do it like this: abs(num_1 - num_2) < 0.000001
a method named slope that takes no arguments and returns the slope of the LineSegment. You can find the slope using the following formula:
formula for slope of a line segment where m is the slope and again the two points are (x1, y1) and (x2, y2), which in this case are the two endpoints of the LineSegment.

Here's a simple example of how your code might be used:

point_1 = Point(7,4)
point_2 = Point(-6,18)
print(point_1.distance_to(point_2))
line_seg_1 = LineSegment(point_1,point_2)
print(line_seg_1.length())
print(line_seg_1.slope())

point_3 = Point(-2,2)
point_4 = Point(24, 12)
line_seg_2 = LineSegment(point_3,point_4)
print(line_seg_1.is_parallel_to(line_seg_2))
Hint 1: Start with the Point class and make sure it's working correctly before going on to the LineSegment class.

Hint 2: In the distance_to method, you'll be working with two pairs of coordinates. The Point object the method is called on is the one that executes the method, and it can access its own coordinates directly, for example self._x_coord. The other pair of coordinates comes from the Point that was passed in as an argument. Because the distance_to method belongs to the Point class, it can access those Point data members directly, without needing to use a get method. For example, if the parameter name is other_point, you can do other_point._x_coord.

Hint 3: In the methods of the LineSegment class, you'll need to access the coordinates of Point objects. Because those methods are not part of the Point class, they need to call the Point get methods on a Point object to access its coordinates. For example, self._endpoint_1.get_x_coord().

2 Answers

4 votes

Final answer:

1. Point class:

```python

class Point:

def __init__(self, x, y):

self._x_coord = x

self._y_coord = y

def get_x_coord(self):

return self._x_coord

def get_y_coord(self):

return self._y_coord

def distance_to(self, other_point):

return ((self._x_coord - other_point.get_x_coord()) ** 2 + (self._y_coord - other_point.get_y_coord()) ** 2) ** 0.5

```

2. LineSegment class:

```python

class LineSegment:

def __init__(self, endpoint_1, endpoint_2):

self._endpoint_1 = endpoint_1

self._endpoint_2 = endpoint_2

def get_endpoint_1(self):

return self._endpoint_1

def get_endpoint_2(self):

return self._endpoint_2

def length(self):

return self._endpoint_1.distance_to(self._endpoint_2)

def is_parallel_to(self, other_line_segment):

return abs(self.slope() - other_line_segment.slope()) < 0.000001

def slope(self):

x1 = self._endpoint_1.get_x_coord()

y1 = self._endpoint_1.get_y_coord()

x2 = self._endpoint_2.get_x_coord()

y2 = self._endpoint_2.get_y_coord()

if x2 - x1 != 0:

return (y2 - y1) / (x2 - x1)

else:

return float('inf')

```

Step-by-step explanation:

For the Point class, it initializes two private data members `_x_coord` and `_y_coord` using the provided x and y coordinates in the `__init__` method. Two get methods, `get_x_coord` and `get_y_coord`, are created to access these private members. The `distance_to` method calculates the distance between two Point objects using the distance formula, utilizing the direct access of coordinates for the Point object the method is called on and the get methods for the coordinates of the Point passed as an argument.

In the LineSegment class, the `__init__` method initializes private data members `_endpoint_1` and `_endpoint_2` using the provided endpoints. The get methods `get_endpoint_1` and `get_endpoint_2` retrieve these endpoints. The `length` method calculates the distance between endpoints using the `distance_to` method from the Point class. `is_parallel_to` compares the slopes of two LineSegment objects by calculating slopes using the `slope` method. The `slope` method computes the slope between the endpoints by applying the slope formula considering different scenarios, like when the x-values are the same (vertical line). The `is_parallel_to` method checks for similarity in slopes within a threshold to determine if the LineSegments are parallel.

User Malvadao
by
8.2k points
3 votes

Final answer:

The question involves creating two classes, Point and LineSegment, with specific methods and attributes to handle coordinates, distances, lengths, slopes, and parallelism.

Step-by-step explanation:

The question is about creating two classes: Point and LineSegment, with specific methods and attributes. The Point class has two data members representing the coordinates of a point, along with get methods and a distance_to method to calculate the distance between two points. The LineSegment class has two data members representing the endpoints of a line segment, along with get methods and methods to calculate the length, slope, and check for parallelism with another line segment.

User Segfault
by
7.7k points

No related questions found