Answer to Question #332238 in Python for Lala

Question #332238

A class called MyPoint, which models a 2D point with x and y coordinates. It contains:



 Two private instance variables x (int) and y (int).


 A constructor (__init__()) that constructs a point with the given x and y coordinates.


 Getter and setter for the instance variables x and y.


 Method setXY() to set both x and y.


 Method getXY() which returns the x and y in a 2-element list.


 A __str__ method that returns a string description of the instance in the format "(x, y)".


Method called distance(self, anotherpoint) that returns the distance from this point to the given MyPoint instance (called another), e.g.,



p1 = MyPoint(3, 4)


p2 = MyPoint(5, 6)


print(p1.distance(p2))



Method called distance_from_origin() method that returns the distance from this point to the origin (0,0), e.g.,



p1 = MyPoint(3, 4)


print(p1.distance_from_origin())

1
Expert's answer
2022-04-22T14:00:11-0400


//Create MyPoint class and demonstrat out class
#define class MyPoint
import math
class MyPoint:
  """2D Model of Point x and y"""
  def __init__(self,x=0,y=0):
    "constructor "
    self.__x=x
    self.__y=y
  def get_X(self):
    "Get X cooridnate values"
    return self.__x;
  def get_Y(self):
    "get Y coordinate value"
    return self.__y
  def setXY(self,x,y):
    "Set both coordinate"
    self.__x=x
    self.__y=y
  def getXY(self):
    "return both x,y in list"
    return [self.__x,self.__y]
  def __str__(self):
    "Return format string"
    return "("+str(self.__x)+","+str(self.__y)+")"
  def distance(self,another):
    "Evklid distance"
    return math.sqrt((self.__x-another.get_X())*(self.__x-another.get_X())+(self.__y-another.get_Y())*(self.__y-another.get_Y()))
p1=MyPoint(3,4)
p2=MyPoint(6,6)
print("P1 "+str(p1))
print("P2 "+str(p2))
print("==Distance==")
print(p1.distance(p2))
//This code test on Lunix online compiler we can use this class !!

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS