Question

using python 3: Do not change the tests. Write the body of the distFromOrigin method. class...

using python 3:

Do not change the tests. Write the body of the distFromOrigin method.

class Point:

def __init__(self, initX, initY):
""" Create a new point at the given coordinates """
self.__x = initX
self.__y = initY

def getX(self):
""" Get its x coordinate """
return self.__x

def getY(self):
""" Get its y coordinate """
return self.__y

def __str__(self):
""" Return a string representation of self """
return "({}, {})".format(self.__x, self.__y)

def distFromOrigin(self):
""" Return the distance between self and (0,0) """
return 0


if __name__ == "__main__":
import test
a = Point(-4,3)
test.testEqual(a.distFromOrigin(), 5)

b = Point(1,1)
test.testEqual(b.distFromOrigin(), 2**0.5)

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

    def __init__(self, initX, initY):
        """ Create a new point at the given coordinates """
        self.__x = initX
        self.__y = initY

    def getX(self):
        """ Get its x coordinate """
        return self.__x

    def getY(self):
        """ Get its y coordinate """
        return self.__y

    def __str__(self):
        """ Return a string representation of self """
        return "({}, {})".format(self.__x, self.__y)

    def distFromOrigin(self):
        """ Return the distance between self and (0,0) """
        return ((self.__x*self.__x)+(self.__y*self.__y))**(0.5)


if __name__ == "__main__":
    import test
    a = Point(-4,3)
    test.testEqual(a.distFromOrigin(), 5)

    b = Point(1,1)
    test.testEqual(b.distFromOrigin(), 2**0.5)
Add a comment
Know the answer?
Add Answer to:
using python 3: Do not change the tests. Write the body of the distFromOrigin method. class...
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
  • Need Help! in English Can not get program to run  plz include detailed steps as comments of...

    Need Help! in English Can not get program to run  plz include detailed steps as comments of what i did not do what I have done so far class Point: def __init__(self): self._x = 0 self._y = 0 def getX(self): return self._x def setX(self, val): self._x = val def getY(self): return self._y def setY(self, val): self._y = val def __init__(self, initX = 0, initY = 0): self._x = initX self._y = initY def __str__(self): return '<'+str(self._x)+', '+str(self._y)+ '>' def scale(self, factor):...

  • Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born which is a subclass of Famous_Person. Add a method to calculate the day of the week the person was born...

    Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born which is a subclass of Famous_Person. Add a method to calculate the day of the week the person was born and print out all of the corresponding information using the overridden print method. Use the following code below as a starting point. ////////////////////////////////////////////////////// from datetime import datetime class Famous_Person(object): def __init__(self, last, first, month,day, year): self.last = last self.first = first self.month = month...

  • ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value):...

    ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value): self.value = value self.next = None def __repr__(self): return "{}".format(self.value) def __init__(self): self.top = None def shuffle(self): card_list = 4 * [x for x in range(2, 12)] + 12 * [10] random.shuffle(card_list) self.top = None for card in card_list: new_card = self.Card(card) new_card.next = self.top self.top = new_card def __repr__(self): curr = self.top out = "" card_list = [] while curr is not None:...

  • Hi I'm trying to write a code for a web server in python with flask. This...

    Hi I'm trying to write a code for a web server in python with flask. This is what I have so far from flask import Flask app = Flask(__name__) @app.route('/') #first endpoint i.e. "http://localhost/" def index(): return 'hello' #return data in string if __name__ == '__main__': app.run(debug=True) After running the code, I'm given a address to the web with the text hello. The problem is that this only works with my computer that is running the code. If I try...

  • How do you do this? class Event: """A new calendar event.""" def __init__(self, start_time: int, end_time:...

    How do you do this? class Event: """A new calendar event.""" def __init__(self, start_time: int, end_time: int, event_name: str) -> None: """Initialize a new event that starts at start_time, ends at end_time, and is named name. Precondition: 0 <= start_time < end_time <= 23 >>> e = Event(12, 13, 'Lunch') >>> e.start_time 12 >>> e.end_time 13 >>> e.name 'Lunch' """    self.start_time = start_time self.end_time = end_time self.name = event_name def __str__(self) -> str: """Return a string representation of this...

  • Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Ini...

    Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Initialize the object properties def __init__(self, id, name, mark): # TODO # Print the object as an string def __str__(self): return ' - {}, {}, {}'.format(self.id, self.name, self.mark) # Check if the mark of the input student is greater than the student object # The output is either True or False def is_greater_than(self, another_student): # TODO # Sort the student_list # The output is the sorted...

  • 10p Python For the following question, refer to the Python module on the right, as well...

    10p Python For the following question, refer to the Python module on the right, as well as the code below. 1) What is the output of the following code? #dodgeable.py #main dodgeable.py import dodgeable class Locatable: def NUM_CARS = 3 init (self,x,y): NUM TRUCKS = 2 self.mX = X WIDTH = 60 self.mY = y def setx (self,x): def main (): self.mX = x object_list = [] def sety (self, y): self.mY = y for i in range (NUM_CARS) :...

  • 11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl...

    11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl = oofraction.o0Fraction( 2, 5) f2 = oofraction.o0Fraction ( 1, 3) f3 = f1 + f2 print (str(3)) main() def __init__(self, Num, Den): self.mNum = Num self.mDen = Den return def_add_(self,other): num = self.mNumother.mDen + other.mNum*self.mDen den = self.mDen*other.mDen f = OOFraction(num,den) return f 1. Write the output below. def_str_(self): s = str( self.mNum )+"/" + str( self.mDen) returns 2. Write a class called wholeNum...

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

  • need help to complete the task: app.py is the Python server-side application. It includes a class...

    need help to complete the task: app.py is the Python server-side application. It includes a class for representing the list of albums. You need to complete the missing parts such that it loads data from the data/albums.txt and data/tracks.txt files. You can decide what internal data structure you want to use for storing the data. It is already implemented that a single instance of the Albums class is used, so that loading from the files happens only once (and not...

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