Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given Code: O O 1. Create a Point class with the following requirements: Attributes: _x: x-coordinate of the point _y: y-coordinate of the point Two
Given Code:
O O 1. Create a Point class with the following requirements: Attributes: _x: x-coordinate of the point _y: y-coordinate of the point Two readonly Properties: X: X-coordinate of the point o y: y-coordinate of the point A constructor: This takes two required arguments: x and y coordinates One Method: o translate(dx,dy): method moves the point by dx in x-direction and by dy in y- direction (it updates the x and _y attributes by adding dx and dy to them respectively) 2. Create a Rectangle class with the following requirements: Three static attributes: o DEFAULT_WIDTH (initialized to O DEFAULT_HEIGHT (initialized to 1) o rectangleCount (initialized to 0, represents the count of rectangles instantiated so far) . O Attributes: _topLeft: an instance of Point class that denotes the top-left corner of the rectangle. width: width of the rectangle (cannot be negative or 0) height: height of the rectangle (cannot be negative or 0) Three properties: top Left: Getter should return the top Left attribute and the setter should update the topLeft attribute. o width: The getter method should return the width attribute. Knowing that width cannot be negative or 0, the setter should check if client tries to set an O . invalid value, if so, the setter should print a meaningful message, and not update the width attribute. If the new value is valid, the setter should update the width attribute. o height: The getter method should return the_height attribute. Knowing that _height cannot be negative or 0, the setter should check if client tries to set an invalid value, if so, the setter should print a meaningful message, and not update the height attribute. If the new value is valid, setter should update the height attribute. Three readonly properties: o bottomRight: Returns a Point object representing the bottom right corner of the Rectangle. This will be calculated using the top Left, width and height property values. area: returns the area of the rectangle calculated using the width and height property values. o perimeter: returns the perimeter of the rectangle calculated using the width and height property values. A constructor: This takes three required arguments: An object of Point class representing the top-left corner of the rectangle. the width of the rectangle the height of the rectangle O O Width and height arguments cannot be negative or 0. If either of them is invalid, the constructor should print a meaningful message. In case of error, the constructor should set the value of the attribute to the corresponding static attribute (either DEFAULT_WIDTH or DEFAULT_HEIGHT respectively.) The constructor should increment the static attribute rectangleCount (which keeps track of the number of rectangles created) by 1. One Method: o translate(dx,dy): method just calls the translate method of the top left point, effectively moving the rectangle by dx in x-direction and by dy in y-direction. ######################################################## ############### ##Client code to test the Rectangle and Point classes ## 1. Creates 4 rectangles: ## two with valid arguments ## two with invalid arguments ## 2. Verifies that Rectangle rectangleCount is correct ## 3. Doubles width and height of Rectngles[o], prints the rectangle before and after ## 4. Translates Rectngles [1], prints the rectangle before and after ## 5. Attempts to set width of -2 on Rectangles [2] ## 6. Updates width and height of Rectangles[3] successfully ############################################################################ from rectangle import Rectangle, Point ##Variable to enable/disable assert checks # Set this to false, if you don't want the checkRectangle and checkValue to be called. checkAll = True def printRectangle(rect): " Function to print all the properties of a Rectangle object' print(f'Top: {rect.topLeft.y}, Left: {rect. topleft.x}, Width: {rect.width}, Height: {rect. height}, ' + f'Bottom: {rect.bottomRight.y}, Right: {rect.bottomRight.x}, Area: {rect.area}, Perimeter: {rect.perimeter}') def checkRectangle(rect, expected): !!! Function to check all the properties of a Rectangle object .nst the list of values provided in the second parameter (expected)' if (checkAll): actual = [rect. topleft.y, rect. topleft.x, rect.width, rect.height, rect. bottomRight.y, rect.bottomRight.x, rect.area, rect.perimeter] for a, b in zip(actual, expected): assert (a == b) def checkValue(x, y): if (checkail): assert(x == y) def main(): starline = "*"*50 dashline = "."*50 print (starline+" Testing Rectangle and point classes.") print (starline) ############################################################################ #data used to create 4 rectangles # [left, top, width, height] rawData = [[1,2,3,4],[5,6,7,8], [9, 10, 0,12], (13, 14, 15,-4]] #Create 4 Rectangle objects using rawData rectangles = [] for k, d in enumerate(rawData): print("Creating Rectangle {k}") r = Rectangle(Point(d[0],d[i]), d[2],d[3]) rectangles.append(r) ### print(starline) print("Checking rectangle count, should be 4.") print("Rectangle count: ", Rectangle rectangleCount) checkValue (Rectangle rectangleCount, 4) print(starline) ############################################################################ #Process Rectangles one by one and print the properties before and after ############################################################################ print("Rectangles[0] before processing.") printRectangle(rectangles[0]) checkRectangle rectangles[o], [2,1,3,4,6, 4, 12, 14]) print(dash line) #Change the width and height of rectagnles[0] rectangles[0].width *= 2 rectangles[0].height *= 2 print("Rectangles[0] after doubling width and height.") printRectangle(rectangles[0]) # Should show width and height doubled checkRectangle(rectangles[0],[2,1, 6, 8, 10, 7, 48,28]) print(starline) ############################################################################ #Translate rectangles[1] print("Rectangles[1] before processing.") printRectangle(rectangles[1]) checkRectangle(rectangles[1], [6,5,7,8,14,12,56,30]) print(dashline) rectangles[1].translate(10,10) print("Rectangles[1] after translating by (10,10).") printRectangle(rectangles[1])# Should show rectangle top-left is moved checkRectangle(rectangles[1], [16,15,7,8, 24, 22,56,30]) print(starline) ############################################### ################ print("Rectangles [2] before processing.") printRectangle(rectangles [2]) checkRectangle(rectangles[2],[10,9,1, 12, 22, 10, 12, 26]) print(dashline) #Attempt to set the width of rectagnles [2] to -2 rectangles [2].width = -2 print("Rectangles [2] after attempting to set width to -2.") printRectangle(rectangles [2])# Should show error message and width should be unchanged checkRectangle(rectangles [2], [10,9,1, 12, 22, 10, 12, 26]) print(starline) ######################################################### ################## # Reset width and height of Rectangle[3] to 15 and 16 print("Rectangles [3] before processing.") printRectangle(rectangles [3]) checkRectangle(rectangles[3], [14, 13, 15, 1, 15, 28,15,32]) print(dashline) rectangles[3].width = 15 rectangles[3].height = 16 print("Rectangles [3] after resetting width and height to 15 and 16.") printRectangle(rectangles[3])# Should show width of 15 and height of 16 checkRectangle(rectangles[3],[14, 13, 15, 16, 30, 28, 240,62]) print(starline) ####### ############################ print("Thanks for your patience! Goodbye") ##### ####### ######## #### II __main__"): if ( name main() Sample Run: - X l Python 3.6.3 Shell File Edit Shell Debug Options Window Help === RESTART: C:/code/Python/Playground-120/Assignments/rectangleClient.py === **** Testing Rectangle and Point classes. ***** Creating Rectangle O Creating Rectangle 1 Creating Rectangle 2 Width cannot be negative or zero. Setting it to the default value of 1 Creating Rectangle 3 Height cannot be negative or zero. Setting it to default value of 1 ******************* ***** Checking rectangle count, should be 4. Rectangle count: 4 ******** ******** Rectangles [0] before processing. Top: 2, Left: 1, Width: 3, Height: 4, Bottom: 6, Right: 4, Area: 12, Perimeter: 14 Rectangles [0] after doubling width and height. Top: 2, Left: 1, Width: 6, Height: 8, Bottom: 10, Right: 7, Area: 48, Perimeter: 28 Rectangles [1] before processing. Top: 6, Left: 5, Width: 7, Height: 8, Bottom: 14, Right: 12, Area: 56, Perimeter: 30 Rectangles[1] after translating by (10,10). Top: 16, Left: 15, Width: 7, Height: 8, Bottom: 24, Right: 22, Area: 56, Perimeter: 30 Rectangles [2] before processing. Top: 10, Left: 9, Width: 1, Height: 12, Bottom: 22, Right: 10, Area: 12, Perimeter: 26 Width cannot be negative or zero Rectangles [2] after attempting to set width to -2. Top: 10, Left: 9, Width: 1, Height: 12, Bottom: 22, Right: 10, Area: 12, Perimeter: 26 ******* ***** Rectangles [3] before processing. Top: 14, Left: 13, Width: 15, Height: 1, Bottom: 15, Right: 28, Area: 15, Perimeter: 32 Rectangles [3] after resetting width and height to 15 and 16. Top: 14, Left: 13, Width: 15, Height: 16, Bottom: 30, Right: 28, Area: 240, Perimeter: 62 *** Thanks for your patience! Goodbye Ln: 128 Col: 4Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started