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

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 shapes x,y coordinates, to move the shape to a new set of coordinates, and to report the shapes location as a tuple containing the objects x,y coordinates. You have to complete the location() method.

Heres some code to get you started on shape.py. Its based on the source code for the Shape class on p. 30 of The Quick Python Book.

#!/usr/local/bin/python3 # NAME: Your Name # FILE: shape.py # DESC: A Shape class from The Quick Python Book (p. 30), set x,y coordinates, # moves objects, and reports location. import math class Shape: """Shape class: provides three methods: 1. __init__() set initial x,y coordinates 2. move() changes x,y coordinates 3. location() returns x,y coordinates The __str__() method is customized to show an object's class and x,y coordinates. You have to complete the location() method.""" # __init__() is the constructor. Instance methods always have a "self" parameter. def __init__(self, x, y): """Assign x,y coordinates to the object""" 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 # Custom __str__() method to report an objects properties # Every class inherits a __str__() method for the top-level Object class. # We can modify it to produce more descriptive output for this class. def __str__(self): """Return class name, x, and y""" return "{}, x:{}, y:{}".format(type(self).__name__, self.x, self.y) ########################################## # TODO TODO TODO --- COMPLETE THIS METHOD ########################################## def location(self): '''Returns a tuple containing the (x,y) coordinates of a Shape object''' pass # Example Testing code to execute when module is run as a standalone script. # This code will *not* run when the module is imported. 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 ---") # The Shape._main() function will be executed when this file is run as a standalone script. if __name__ == '_main': _main()

Example output:

--- START --- -- Shape 1: Shape, x:0, y:5 Shape 2: Shape, x:5, y:5 -- Shape 1: Shape, x:1, y:6 Shape 2: Shape, x:6, y:6 -- Shape 1: Shape, x:3, y:8 Shape 2: Shape, x:8, y:9 -- Shape 1: Shape, x:6, y:11 Shape 2: Shape, x:11, y:15 -- Shape 1: Shape, x:10, y:15 Shape 2: Shape, x:15, y:25 -- Shape 1: Shape, x:15, y:20 Shape 2: Shape, x:20, y:40 -- Shape 1: Shape, x:21, y:26 Shape 2: Shape, x:26, y:61 -- Shape 1: Shape, x:28, y:33 Shape 2: Shape, x:33, y:89 -- Shape 1: Shape, x:36, y:41 Shape 2: Shape, x:41, y:125 -- Shape 1: Shape, x:45, y:50 Shape 2: Shape, x:50, y:170 (45, 50) (50, 170) --- END --- 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!