(Solved) : First Half Assignment Writing Two Different Versions Class Work Test Code Provided Need Im Q37273553 . . .
For the first half of the assignment, you will be writing twodifferent versions of the same class. They both have to work withthe same test code (which I have provided), but they need to beimplemented in two different ways. Put version 1 in a script namedblock1.py and version 2 in a script named block2.py
The point of this assignment is to understand how encapsulationallows you to change the internal workings of a class withoutimpacting how a programmer would use the class.
Warning: Every semester, many studentscompletely misunderstand the instructions for this assignment andhave to do the whole thing over again. Make sure you go to your laband have a TA look at your code once you have a few methodswritten, so that they can make sure that you’re on the righttrack.
-
Both versions must have a class definition for for a Block,which stores information about the position and size of arectangular block. (We’re assuming that the block will always beoriented horizontally, so there are no diagonals to worry abouthere.) In both cases, none of your attributes should be public.
Both versions will have the exact same methods and magicmethods, with the exact same signatures and purpose statements. Theonly difference will be how the size and location of the Block isstored. This will mean that some of your methods will have to beimplemented differently (in particular, the getters and setters).If you do things correctly, you will be able to use the exact samecode for all of the other methods.
Here is how you should store your data in the two versions:
- Version 1 should have attributes to store the x-coordinate andthe y-coordinate of the center of the Block, as well as attributesto store the width and the height of the Block. Note: this versionshould not have any attributes that have anything to dowith the corners. If it does, it will be rejected!
- Version 2 should have attributes to store the coordinates ofthe bottom-left corner(the “SW”* corner) and thecoordinates of the upper-right corner (the “NE”* corner).Note: this version should not have any attributes thathave anything to do with the center of the Block, norshould there be attributes for the width orheight. Otherwise, it will be rejected!
*When you have to talk about diagonal directions on atwo-dimensional grid, it’s common to use “north,” “east,” “south,”and “west” to refer to “up,” “right,” “down,” and “left.” That way,you can use “SW” to refer to something on the bottom-left, “NE” torefer to something on the upper-right, etc.
-
Both versions should have a setter called .set_CWH() that takesa pair of coordinates (for the center), along with the width andheight, and sets the attributes to match the given information.
In version 1, this will be easy (since you’ll just need to setthe appropriate attributes to the given values). In version 2, thiswill be harder because you’ll have to use the center, the width,and the height to calculate the new coordinates of the corners.
For version 1, I recommend writing this method first and thenwriting the constructor, so you can test it right away.
-
Both versions should have a setter called .set_corners() thattakes two pairs of coordinates (representing the lower-left (“SW”)and upper-right (“NE”) corners), and sets the attributes to matchthe given information.
In version 2, this will be easy (since you’ll just need to setthe appropriate attributes to the given values). In version 1, thiswill be harder because you’ll have to use the coordinates of thecorners to calculate the new center, width, and height.
For version 1, I’d recommend putting this one off until you’vealready done the other setter and the constructor.
When you’ve created both setters, the commandsb1.set_CWH((50,100), 8, 6) and b1.set_corners((46,97), (54,103))should both do the same thing.
-
Both versions should have a constructor that willeither take a pair of coordinates (for the center), alongwith the width and height; or take two pairs ofcoordinates (representing the lower-left (“SW”) and upper-right(“NE”) corners). So for example, the commands Block((50,100), 8, 6)and Block((46,97), (54,103)) should both create Blocks with thesame size (width 8 and height 6) and position (lower-left corner at(46,97), center at (50,100), and upper-right corner at(54,103)).
For version 1, I recommend handling the center-width-heightversion first, making sure it works, and then adding the option forthe two corners. For version 2, if you’ve done things correctly(using your setters), you won’t have to change this method atall!
-
Both versions should have getter methods called .get_width() and.get_height() that return the width or height of the Block.
As with most of the getters for this class, the implementationsfor the two versions will be very different. Version 1 will likelybe a lot simpler for this one.
-
Both versions should have a getter method called .get_center()that returns the coordinates of the center of the Block. (Thismethod should return a tuple of two numbers.) Version 1 will likelybe simpler here.
-
Both versions should have getters method called.get__SW_corner() and .get_NE_corner(), which should return thecoordinates of the appropriate corner (as a two-item tuple). Forthis one, Version 2 will be much easier.
-
Both versions need to have an appropriate .__repr__() method.Since the Blockconstructor has two different options, there are twodifferent options for how to represent a Block object (one usingthe width, height, and center, and one using the two corners). In amore realistic setting, you could probably choose whichrepresentation to use for displaying a Block, but for thisassignment I am requiring your .__repr__() method to return astring of the format “Block((SW-corner-x, SW-corner-y),(NE-corner-x,NE-corner-y))”.
Hint: Write the above getters first,and use them as part of this method. If you do that correctly(meaning that you don’t access any of the attributes directly), youcan use exactly the same code for both versions.
Expert Answer
Answer to For the first half of the assignment, you will be writing two different versions of the same class. They both have to wo…
OR