Question

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 line can be instantiated in one of two ways, as shown below.

somePoint = Point(2, 3)

anotherPoint = Point(4, 8)

someLine = Line(somePoint, anotherPoint)

or...

someLine = Line()

In the second case, the constructor should initialize both points to the origin (0, 0). You must override the __str__method to print the line as follows: "(2, 5)--(4, 7)". Your class must also provide a length method that returns the length of the segment.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !

===========================================================================

class Point():

    def __init__(self, x=0, y=0):
        self.x_coordinate = x
        self.y_coordinate = y

    def __str__(self):
        return '({},{})'.format(self.x_coordinate, self.y_coordinate)


class Line():

    def __init__(self, start=Point(), end=Point()):
        self.start = start
        self.end = end

    def __str__(self):
        return '{}-{}'.format(self.start, self.end)

    def length(self):
        square_sum = (self.start.x_coordinate - self.end.x_coordinate) ** 2
        square_sum += (self.start.y_coordinate - self.end.y_coordinate) ** 2
        return square_sum**0.5


def main():
    somePoint = Point(2, 3)
    anotherPoint = Point(4, 8)
    someLine = Line(somePoint,anotherPoint)
    print(someLine)
    print('Lengh: {}'.format(someLine.length()))
main()

=======================================================================

class Point () 1 init (self, х-0, у-0) : def 3 self.x_coordinate = x 4 self.y coordinate = y def str_(self) 7 return ,}).fo

10 class Line 11 12 start-Point (), end=Point )): def init (self, 13 self.start start 14 self.end end 15 16 def .(self) 17 st

Add a comment
Know the answer?
Add Answer to:
I need help coding this with Python. Create a Point class that consists of an ordered...
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
  • 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....

  • Create an application that provides a solution for problem 20.8 In addition to requirements specified in...

    Create an application that provides a solution for problem 20.8 In addition to requirements specified in the description. The class must satisfy the following Default Constructor Two argument constructor for data used to initialize "first" and "second" "toString" method for displaying the "first" and "second" data elements Creates a "Comparator" object (to be used by the sort method; i.e. create an instance of a class that implements the interface [must override "compare" and "equals" methods]) The application must create an...

  • Need Help with these questions for coding. This is the signature of a constructor for the...

    Need Help with these questions for coding. This is the signature of a constructor for the Person class. public Person private Person () public Object Person () O public Person () Question 3 (1 point) This element of a class allows properties to be pre-loaded as an object of the class is being instantiated. methods attributes constructors properties Question 4 (1 point) If you create a Parce method to take a record from a file and create an object of...

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

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • Help with this coding assignment for C++! Add any comments for better understanding. Create a class...

    Help with this coding assignment for C++! Add any comments for better understanding. Create a class named CupCake. It contains: • Has private instance variables 1. egg 2. butter 3. baking powder 4. sugar 5. flour 6. milk • All members must have public methods: getter() and setter(). • A constructor - a default constructor with no arguments. The constructor will initial all member variable to a default value 0. • One public pure virtual function showingredients() that returns void....

  • I need help asap, Python language 2) Write a class to represent a class to represent...

    I need help asap, Python language 2) Write a class to represent a class to represent a Car and its passengers. Write a constructor that takes one additional parameter a number, the total seats in the car. The constructor should save this as a datamember and also initialize an additional datamember, a list, to keep track of the passengers in the car. Write method addRider that takes an additional parameter a string of the passenger to be added to the...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • I need this in java please Create an automobile class that will be used by a...

    I need this in java please Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage. Your program should have appropriate methods such as: default constructor parameterized constructor add a new vehicle method list vehicle information (return string array) remove a vehicle method update vehicle attributes method. All methods...

  • In Java please! Problem You will be using the point class discussed in class to create...

    In Java please! Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...

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