Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 8 - More on Strings and Special Methods - Programming Exercises - Page 267: 8.17

Answer

code

Work Step by Step

# 8.17 (The Point class) Design a class named Point to represent a point with x- and ycoordinates. # The class contains: # ■ Two private data fields x and y that represent the coordinates with get methods. # ■ A constructor that constructs a point with specified coordinates with default # point (0, 0). # ■ A method named distance that returns the distance from this point to another # point of the Point type. # ■ A method named isNearBy(p1) that returns true if point p1 is close to this # point. Two points are close if their distance is less than 5. # ■ Implement the __str__ method to return a string in the form (x, y). # Draw the UML diagram for the class, and then implement the class. Write a test # program that prompts the user to enter two points, displays the distance between # them, and indicates whether they are near each other. import math class Point: def __init__(self, x=0, y=0): self.__x__ = x self.__y__ = y def getX(self): return self.__x__ def getY(self): return self.__y__ def distance(self, point): dis = math.sqrt((self.__x__ - point.getX()) ** 2 + (self.__y__ - point.getY()) ** 2) return dis def isNearBy(self, point): d = self.distance(point) return True if d < 5 else False def __str__(self): return '( ' + str(self.__x__) + ',' + str(self.__y__) + ' )' def main(): x1, y1, x2, y2 = eval(input("Enter two points x1, y1, x2, y2: ")) p1 = Point(x1, y1) p2 = Point(x2, y2) print("The distance between the two points is", p1.distance(p2)) if p1.isNearBy(p2): print("The two points are near each other") else: print("The two points are not near each other") main()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.