Question

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 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.

Sample Run 1

Enter the x-coordinate of point1: 2.1

Enter the y-coordinate of point1: 2.3

Enter the x-coordinate of point2: 19.1

Enter the y-coordinate of point2: 19.2

The distance between the two points is 23.97

The two points are not near each other

Sample Run 2

Enter the x-coordinate of point1: 2.1

Enter the y-coordinate of point1: 2.3

Enter the x-coordinate of point2: 2.3

Enter the y-coordinate of point2: 4.2

The distance between the two points is 1.91

The two points are near each other

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Point:
    def __init__(self, x=0.0, y=0.0):
        self.__x = x
        self.__y = y

    def get_x(self):
        return self.__x

    def get_y(self):
        return self.__y

    def move(self, dx, dy):
        self.__x += dx
        self.__y += dy
        return dx, dy

    def move_to(self, newx, newy):
        self.__x = newx
        self.__y = newy
        return newx, newy

    def __str__(self):
        return '(' + str(self.__x) + ', ' + str(self.__y) + ')'

    def __repr__(self):
        return 'Point' + '({0}, {1})'.format(self.__x, self.__y)

    def distance(self, p):
        return ((self.__x - p.__x) ** 2 + (self.__y - p.__y) ** 2) ** 0.5

    def isNearby(self, p):
        return self.distance(p) < 5


x1 = float(input("Enter the x-coordinate of point1: "))
y1 = float(input("Enter the y-coordinate of point1: "))
x2 = float(input("Enter the x-coordinate of point2: "))
y2 = float(input("Enter the y-coordinate of point2: "))
p1 = Point(x1, y1)
p2 = Point(x2, y2)
print("The distance between the two points is {:.2f}".format(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")

Add a comment
Know the answer?
Add Answer to:
NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a...
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
  • Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with...

    Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with x-and y-coordinates. The 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 using passed in arguments. Two get functions for data fields x and y, respectively. A method named distance that returns the distance from this point to another point of the MyPoint...

  • Design a class named MyPoint to represent a point with x and y-coordinates. The class contains:...

    Design a class named MyPoint to represent a point with x and y-coordinates. The 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. A function named distance that returns the distance from this point to another point of the MyPoint type Write a test program that creates two...

  • 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...

  • Implement the Point class (code for this up to the isHigher method was discussed in the...

    Implement the Point class (code for this up to the isHigher method was discussed in the lecture). The class has the following instance variables: • x coordinate (an int) • y coordinate (an int) and the following methods: • Constructor that sets the x and y coordinates • Get and set methods • Method equals if this point is equal to another point object (if the x and y coordinates are the same). • Method isHigher if this point is...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • 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....

  • Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in thr...

    Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have the...

  • Question 2 - Programming Exercise 1. Make a directory for this lab and change into it....

    Question 2 - Programming Exercise 1. Make a directory for this lab and change into it. 2. Copy files using the following command: cp/net/data/ftp/pub/class/115/ftp/cpp/Inheritance/Exercise.cpp Exercise.cpp Finish the program so that it compiles and runs. The instructions are contained in the C++ file. Your completed program should generate output similar to the following: TwoD default constructor This program asks for the coordinates of two points in 3D space and calculates their distance. Please enter the xyz coordinates for the first point:...

  • 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...

  • In JAVA Design a class named Point that meets the following requirements: Two data fields x...

    In JAVA Design a class named Point that meets the following requirements: Two data fields x and y for representing a point with getter methods. A no-arg constructor that constructs a point for (0, 0). A constructor that constructs a point with the specified x and y values. Override the equals method. Point p1 is said to be greater than point p2 if p1.x == p2.x and p1.y == p2.y. Implement the Comparable<Point> interface and the compareTo method. Point p1...

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