77.9k views
4 votes
Create 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.

User Naya
by
3.5k points

1 Answer

1 vote

Answer:

Step-by-step explanation:

The following class is written in Python, it has the three instance variables that were requested. It also contains a constructor that takes in those variables as arguments. Then it has functions to update the positions of the Dot and change its color. The class also has functions to output current position and color.

class Dot:

"""Class Dot"""

x_coordinate = 0

y_coordinate = 0

color = ""

def __init__(self, x_coordinate, y_coordinate, color):

"""Constructor that takes x and y coordinates as integers and a string for color. It ouputs nothing but saves these argument values into the corresponding instance variables."""

self.x_coordinate = x_coordinate

self.y_coordinate = y_coordinate

self.color = color

def moveUp(self, number_of_spaces):

"""Moves the Dot up a specific number of spaces that is passed as a parameter. Updates y_position"""

self.y_coordinate += number_of_spaces

return ""

def moveDown(self, number_of_spaces):

"""Moves the Dot down a specific number of spaces that is passed as a parameter. Updates y_position"""

self.y_coordinate -= number_of_spaces

return ""

def moveLeft(self, number_of_spaces):

"""Moves the Dot left a specific number of spaces that is passed as a parameter. Updates x_position"""

self.x_coordinate -= number_of_spaces

return ""

def moveRight(self, number_of_spaces):

"""Moves the Dot right a specific number of spaces that is passed as a parameter. Updates x_position"""

self.x_coordinate += number_of_spaces

return ""

def dot_position(self):

"""Print Dot Position"""

print("Dot is in position: " + str(self.x_coordinate) + ", " + str(self.y_coordinate))

return ""

def dot_color(self):

"""Print Current Dot Color"""

print("Dot color is: " + self.color)

Create a class called Dot for storing information about a colored dot that appears-example-1
User SoliMoli
by
3.0k points