Add exercise 1

This commit is contained in:
lolabird 2024-02-08 20:03:51 -07:00
commit f48f1bbb94
2 changed files with 31 additions and 0 deletions

Binary file not shown.

View file

@ -0,0 +1,31 @@
import math
class Point:
""" Point class for representing and manipulating x,y coordinates. """
def __init__(self, initX, initY):
""" Create a new point at the given coordinates. """
self.x = initX
self.y = initY
def getX(self):
return self.x
def getY(self):
return self.y
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def distanceFromPoint(self, pnt):
dx = self.x - pnt.getX()
dy = self.y - pnt.getY()
return math.sqrt(dx**2 +dy**2)
def __str__(self):
return "x=" + str(self.x) + ", y=" + str(self.y)
p = Point(7, 6)
q = Point(10, 3)
print(p.distanceFromPoint(q))