Question

Create a new class called Point that represents a point in the Cartesian plane with two...

Create a new class called Point that represents a point in the Cartesian plane with two coordinates. Each instance of Point should have to coordinates called x and y. The constructor for Point should take two arguments x and y that represent the two coordinates of the newly created point. Your class Point should override the __str__(self) method so that it returns a string showing the x- and y-coordinates of the Point enclosed in parentheses and separated by a comma. Your class Point should override the __repr__(self) method so that it returns a string that contains Python code that would produce an instance of the same point. Your class Point should override the __eq__(self, other) method so that it returns True if and only if other is also a Point that has the same x- and y- coordinate as self.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

"""
  Python program for class Point
"""

class Point:
    def __init__(self,x_init,y_init):
        self.x = x_init
        self.y = y_init

    def __str__(self):
        return "(%s,%s)"%(self.x, self.y)

    def __repr__(self):
        return "".join(["Point(", str(self.x), ",", str(self.y), ")"])

    def __eq__(self, other):
      if type(self) == type(other):
        if self.x == other.x and self.y == other.y:
          return True
        else:
          return False
  
p1 = Point(10, 3)
p2 = Point(1, 0)
p3 = Point(10, 3)

print('P1: ', p1)
print('P2: ', p2)
print('P3: ', p3)
print('')
print('P1: ', repr(p1))
print('P2: ', repr(p2))
print('P3: ', repr(p3))
print('')
print('P1 is P2: ', p1 == p2)
print('P1 is P3: ', p1 == p3)

Add a comment
Know the answer?
Add Answer to:
Create a new class called Point that represents a point in the Cartesian plane with two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are to write a class called Point – this will represent a geometric point in...

    You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints). Point should have the following: Data:  that hold the x-value and the y-value. They should be ints and must be private. Constructors:  A default constructor that will set the values to (3, -5)  A parameterized constructor that will receive 2 ints (x then y) and set the data to what is...

  • I need help coding this with Python. Create a Point class that consists of an ordered...

    I need help coding this with Python. Create a Point class that consists of an ordered pair (x, y) representing a point's location on the x and y axes. The constructor allow the x and y values to be passed in and should default missing values to 0. You must override the __str__ method to print the point as normal (i.e., "(2, 5)"). Also, create a Line class that consists of a pair of Point objects p and q. A...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a...

    NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a point with x- and y-coordinates. 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...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Consider the following class Point which represents a point in Cartesian coordinate system. The class has...

    Consider the following class Point which represents a point in Cartesian coordinate system. The class has two instance variables x and y which represent its position along the x-axis and y-axis. The constructor of the class initializes the x and y of the point with the specific value init; the method move() moves the point as specified by its parameter amount. Using the implementation of the class Point information and write its specification. The specification of the class includes the...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains:...

    Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains: • Two data fields x and y that represent the coordinates. • A no-arg constructor that creates a point (0,0). • A constructor that constructs a point with specified coordinates. • Two get functions for data fields x and y, respectively. • Two set functions for data fields x and y, respectively. • A function named distance that returns the distance from this point...

  • The Java class called Holiday is started below. An object of class Holiday represents a holiday...

    The Java class called Holiday is started below. An object of class Holiday represents a holiday during the year. This class has three instance variables: name, which is a String representing the name of the holiday day, which is an int representing the day of the month of the holiday month, which is a String representing the month the holiday is in public class Holiday { private String name; private int day; private String month; // your code goes here...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT