Question: For this assignment, you'll be creating a class called Dot for storing information about a colored dot that appears on a flat grid. The class
For this assignment, you'll be creating a class called Dot for storing information about a colored dot that appears on a flat grid. The class Dot will need to store information about its position (an x-coordinate and a y-coordinate, which should both be integers) and its color (which should be a string for now). You can choose what to call your attributes, but be sure to document them properly in the class's docstring.
Don't forget that every function definition, including method definitions, must have a docstring with a purpose statement and signature.
Put all of the code for this assignment into a script called Dot.py
Create the class definition for Dot.
Don't forget to include a docstring with a purpose statement and a list of the attributes and their types.
Define an .__init__() method that takes three arguments (x-position, y-position, and color) and properly assigns the appropriate values to the appropriate attributes. So the command Dot(50,100,"blue") should return a Dot with coordinates (50,100) with the color "blue".
Define a .__repr__() method for creating a string to represent the given Dot. Remember that the .__repr__() should never print anything on the screen; it should return a string that represents the Dot. So if d1 refers to a blue Dot with coordinates (50,100), then its representation would be the string "Dot(50,100,'blue')".
Add Dot methods .move_up() and .move_right() which each take a an integer and modify the Dot by adding the given number to either the y-coordinate (for .move_up()) or the x-coordinate (for .move_right()). So for example, if dot1 is a Dot with x-coordinate 5 and y-coordinate 27, executing dot1.move_right(10)would change the x-coordinate of dot1 to 15 and keep the y-coordinate and the color the same. Similarly, dot1.move_up(-7) would change the y-coordinate of dot1 to 20 and keep the x-coordinate and the color the same.
Add a Dot method .distance_to() that takes a second Dot and returns the distance from the original Dot to that second Dot. (If you want to use the sqrt()function to calculate square roots, you'll need to import it from the math module at the beginning of your script.
Add some code to your script to test your Dot class. The details for exactly what this does are up to you, but I have a few guidelines that you must always follow when writing test code (assuming you want full credit):
For testing commands that make changes (e.g., a command that reassigns a variable, creates a new object, or alters something about an object):
Before the command, there should be a print() statement explaining what is being done. Examples: print("Creating a blue Dot called dot1 with coordinates (10,75)...") or print("Moving dot2 7 places to the left...").
After the command describing what you're about to do, include the line of code that actually changes or creates the object (e.g., dot1 = Dot(10, 75, "blue") or dot1.move_right(-7)).
And after the command is carried out, include a print() statement that displays the results of the change. Example: print("dot1 is now: " + repr(dot1)). Note that in addition to demonstrating that the command did what it was supposed to do, a command like this (which uses the repr()function) also tests the .__repr__() method.
To test a command that displays something or that just performs a calculation (with no changes made or objects created):
Your test command(s) must print enough information so that it's clear what is being displayed. This can either be done by including a separate print()statement (e.g., print("Drawing a picture of dot1...")) before the display command (e.g., dot1.draw()), or it can be done by incorporating the description into the line of code that includes the test command (e.g., print("The distance between dot1 and dot2 is " + dot1.distance_to(dot2) + ".")).
For this assignment, at minimum, your test script must do the following:
Create at least two Dots with different positions and colors. (This will partially test your .__init__() method as well as the structure of the class definition.)
At least once, move one of your Dots up or down by some number using .move_up().
At least once, move one of your Dots left or right by some number using .move_right().
At least once, change the color of one a Dot by directly assigning a new value to the color attribute.
Calculate and display the distance between the two dots using .distance_to().
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
