# CSE 142 Python sessions # This file implements a new class of objects named Point. from math import * class Point: # fields x = 0 y = 0 # constructor def __init__(self, x = 0, y = 0): self.x = x self.y = y # methods def translate(self, dx, dy): self.x += dx self.y += dy def set_location(self, x, y): self.x = x self.y = y def distance_from_origin(self): return sqrt(self.x * self.x + self.y * self.y) def distance(self, other): dx = self.x - other.x dy = self.y - other.y return sqrt(dx*dx + dy*dy) # This is the equivalent of Java's toString method. def __str__(self): return "(" + str(self.x) + ", " + str(self.y) + ")" # operator overloading # This special method implements a + operation on two Points. def __add__(self, other): return Point(self.x + other.x, self.y + other.y)