Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: (No Handwritten Answers Please, Thank You) HW#4.1: Python Class and Object-Oriented Programming . Implement the missing pieces of codes at the places marked with

Python: (No Handwritten Answers Please, Thank You)

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

HW#4.1: Python Class and Object-Oriented Programming . Implement the missing pieces of codes at the places marked with "xxx" or "pass". Do not delete or alter any portion of this file. Grading: You get 0 credits if your program does not run or produce the expected results You can only add codesbut not delete any portions of this file. All the variables have to be private and have the pythonic getter and setter methods. Each testcase weights 10 points Create Test Framework In [3]; import unittest, re, inspect, string True): class TestShapeMethod(unittest. TestCase): def check (self, string_output, ansKey, trace s= string_output = re. sub("\s", "", s) # skip spaces s = re.sub("\(id=\d+\/", , S) s #regex = re.compile('[%]' f re.escape(string.punctuation)) #regex = re.compile('[%]' f string.punctuation ) #s = regex.sub'', s) a= sum( [ord(x) for x in s ]) # encoding if trace: print (string_output) msg = inspect.stack() [1][3] + ": " if (a == ansKey): msg += "pass!" else: "fail!" print (a, ansKey) print (msg) return None # self.assertEqual(a, ansKey, "fail.") msg += Class Shape: Base Class for Various Geometric Shapes In [11]: class Shape: "" "Base class for various geometric shapes""" # private class variable to keep track of the number of count = 0 all objects instantiated def init (self,x=0, y=0): self id = Shape. __count Shape. count += 1 self.x = x self. y - y #xxx: the getter method of def x(self): pass #xxx: the getter method of_y def y(self): pass x and y #xxx: implement the setter methods for def x(self,x): # xxx pass def y(self,y): # xxx pass #xxx implement the getter/setter of the private variable _id def id(self): # xxx pass def id(self, id): # xxx pass def details (self, name=None): "" "detailed description of the oject if name==None: name = self. class_ name x = "{:s}(id={:d})". format (name, self.id ) return X def str (self): "" "Create a new string object from the given object def return self.details() repr__(self): return self.details() Q#1: Test Shape In [7]: def test_case(self): ans = " " + inspect.stack()[0][3] + ": " a = [ str(Shape()) for in range (3) join (a) ans = self.check (ans, 2592, True) TestShapeMethod. test_case = test_case unittest.main(argv=["'], verbosity=0, exit=False) Expected outputs (except the id numbers and the spaces could be different test_case: Shape(id=174), Shape(id=175), Shape( id=176) test_case: pass! class Circle (Shape) In [13]: class Circle (Shape): """Child class of Shape""" def _init__(self,x=0, y=0, radius=0): """Shape with center and radius. # xxx pass radius #xxx: implement the getter and setter method of def radius(self): # xxx pass def radius(self,r): # xxx pass def contains (self, other): # overload the boolean operator in "" "overload boolean operator in. check if Circle completely contains Circle/Point other if isinstance (other, self._class__): # xxx pass return false def area (self): "The area of Circle""" #xxx pass def perimeter (self): "The perimeter of Circle". # xxx pass def coord_delta (self, o): "" "helper function"" return self.X - 0.x, self.y - 0.y def center_dist (self.0): ""the distance between the centers of circles self and other """ pass def dist (self, other): ""the distance between circles self and other" look up the defintion of the distance between circles on internet if isinstance (other, self.class): x, y = self.coord_delta (other) d = math.sqrt (x*x + y*y)-self._radius-other._radius d = max(0,0) return d else: return None !!!! def overlap(self, other): ""check if Circle self and other overlap # xxx pass #xxx: Set the minimum increase of the radius of Circle to contain Point or Circle def enclose(self, o): """Set the minimum increase of the radius of Circle to contain Point pt. The center position should not be changed #xxx pass def relocate (self, x,y): "" "Relocate object self to (x,y) Call the setter for both_x and # xxx pass def moveBy (self, dx, dy): *""add the displacement (dx, dy) to the current center location""" self.x +- dx self.y + dy # xxx: override Shape. details () def details (self): S = super().details () S te area={:.0f}".format (self.area() ) s += ", radius: {:.1f}".Format ( self.radius) s += ", center: ({:d}, {:d})".Format( self.x, self.y) returns def eq_(self, 0): "" "test if Circles self and o both have the same position and radius #xxx pass

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions