Python: In shape.py , create a Shape class with these methods. __init__(), a cla
ID: 3911318 • Letter: P
Question
Python:
In shape.py, create a Shape class with these methods.
__init__(), a class constructor what sets an objects x,y coordinates.
move(), a method to move objects to new x,y coordinates.
location(), a method that returns a tuple containing an object x,y coordinates.
In the Shape class complete the location() method.
The Shape class provides methods to set a shape’s x,y coordinates, to move the shape to a new set of coordinates, and to report the shape’s location as a tuple containing the object’s x,y coordinates. You have to complete the location() method.
Here’s some code to get you started on shape.py. It’s based on the source code for the Shape class on p. 30 of The Quick Python Book.
Example output:
Explanation / Answer
'''
Run the application as
python3 shape.py
'''
#import math
class Shape:
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, deltaX, deltaY):
'''
Moves Shape object by adding values to the object's x and y coordinates.
'''
self.x = self.x + deltaX
self.y = self.y + deltaY
def __str__(self):
'''
Return class name, x, and y
'''
return "{}, x:{}, y:{}".format(type(self).__name__, self.x, self.y)
def location(self):
'''
Returns a tuple containing the (x,y) coordinates of a Shape object
'''
tup = (self.x, self.y)
return tup
def _main():
'''
Testing Shape class move(), location(), and __str__() methods
'''
print("--- START ---")
i2 = 0
c1 = Shape(0, 5)
c2 = Shape(5, 5)
for i in range(10):
i2 += i
c1.move(i, i)
c2.move(i, i2)
print('--')
print('Shape 1: ',c1)
print('Shape 2: ',c2)
print(c1.location())
print(c2.location())
print("--- END ---")
_main()
# The Shape._main() function will be executed when this file is run as a standalone script.
#if __name__ == '_main':
# _main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.