Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: 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

Python:

image text in transcribed

image 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 s = re.sub("\s", ', s) # skip spaces s = re. sub("\(id=\d+\)", , S) #regex = re.compile('[%]' f re.escape(string.punctuation)) #regex = re. compile('[%]' % string.punctuation ) #s = regex.sub('', s) msg = a= sum ( [ord(x) for x in s ]) # encoding if trace: print (string_output) 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 all objects instantiated count = 0 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,x): # xxx pass #xxx implement the getter/setter of the private variable id def id(self): # xxx pass defid(self, id): # xxx pass def details (self, name=None): """detailed description of the oject name if name=None: name = self. class 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 #xxx: implement the getter and setter method of _radius 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 - o.y 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 y # 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 () ", area={:.0f)".Format (self.area()) radius: {:.1f}".format ( self.radius ) ", center: ({:d}, {:d})".format( self.x, self.y) return s S + S + S + def eg (self, o): ""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

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago