Lecture 18 — Exercises¶
Solutions to the problems below must be sent to Submitty for automatic scoring. A separate file must submitted for each problem. Solutions must be submitted by 4 pm on Tuesday, April 4.
Starting from the
Point2d.pyfile you download from the Resources page of the Piazza site, please do the following:- Write a new
Point2dmethod calledscalethat takes as an input argument aPoint2dobject (self) and a numerical value (int or float) and multiples both thexandyattributes by this value. - Write a new
Point2dmethod calleddominatesthat two takes twoPoint2dobjects and returnsTrueif and only if the x coordinate of the first object is greater than that of the second object and the y coordinate of the first object is greater than that of the second object. - The code to test these functions is commented out in the main
code area. Please remove this commenting, test on Piazza, and
submit your resulting
Point2d.pyfile. Call itPoint2d_q1.py
- Write a new
Copy your resulting file from the first question to a new file, perhaps called
Point2d_q2.py.- Write and test the implementation of the method
__str__which returns a string created from the values of aPoint2dobject. For our purposes this is mostly used to create a string that can be printed. Make sure you have this working before you proceed to the other parts of this exercise because they depend on it. - Write the implementation of the subtraction method
__sub__for thePoint2dobject. Uncomment the code in the main area and test this in Wing IDE. Do NOT proceed to the next part of the question until you have this working! - Write the implementation of the method
__mul__which is like thescalefunction you wrote for part 1, but it creates a newPoint2dobject. - Write the implementation of the method
__eq__which returnsTrueif and only if the twoPoint2dobjects have exactly the samexandyvalues.
For each of these you should look at the commented out main code in the
Point2d.pyfile you were provided to see how these methods should be used. Uncomment this code, test your methods, and upload to Submitty when you are done.- Write and test the implementation of the method