Assignment 3
Modifying classes and extending classes
Submit before 11:30 Saturday April 25
Overview
For this assignment, you will modify the Color class and extend it to create the NamedColor class.
Instructions, code writing and questions
- (from lab 2) Add a new method called complement to the Color class. This method should create a new Color object whose red, green and blue components are 255 - the color component of the self object. For example, if the self component of red is 100, the color component of the new object is 155 (i.e. 255 - 100).
- (from lab 2) Test your new component method by creating a new method called show_complement_colors. It works like show_colors, except it serially displays all of the complement colors in the list.
- Download the assn3 zip folder.
- In the nameddemo.py file, study the demo function. This displays
example code, which could be used for testing. It shows how the
NamedColor class can be used to create an object just based on the
color name. It also inherits methods from the Color class,
including sepia and __eq__, which you'll be adding.
- Note the addition of display_colors in the Color class (in
color.py). It is a class method. What makes it different from the
instance methods?
- Add __eq__ method so that two color objects with the same color components will be considered equal. The start of the __eq__ function is already present but commented out. You may want to base this example off of the __eq__ method for the MarbleJar, which we will do in class.
- Add a sepia method to the Color class. This method should return a new color object based on a sepia filter to the self object. The start of this function is already present but commented out. Here's the sepia formula (already in python syntax):
sepia_red = \
min(255, int(self.red * 0.393 + self.green * 0.769 + self.blue * 0.189))
sepia_green = \
min(255, int(self.red * 0.349 + self.green * 0.686 + self.blue * 0.168))
sepia_blue = \
min(255, int(self.red * 0.272 + self.green * 0.534 + self.blue * 0.131))
- Write the __init__ function for NamedColor
class. This should take the color_name to look up the red, green
and blue components and assign them as instance variables. Also,
create a name instance variable and set it equal to
the given color_name. Finally, if the given color_name
does not exist in the color table, raise an
exception with an appropriate message. Hint:
the set_by_name method in the Color class has code
that you can reuse here.
- Write the display method for the NamedColor class. It should work just like the display method in the Color class. Copying the code over provides a good start. However, the label should also include the color name for the NamedColor object.
- You are encouraged to test your methods using statements in the console. When you're ready, you can then test by running the demo function in nameddemo.py file. Include the output of this function with your summary document.
Deliverable
Create a text, word or pdf file that contains the following:
- A statement that summarizes your completion of the lab. The
statement should include any help you received or whether you
discussed your assignment with others. Keep in mind that you may
not copy code from others or allow others to copy your code.
- Your clearly labeled answers to explanations and questions
- A summary of the files included in the zip folder
- Demonstrations that show that your code works
Put your doc file and your py files in a folder, zip it, and submit it on D2L. Check that your submitted zip file is complete.
Grading
The assignment is worth 6 points. Full credit will be awarded to complete, accurate
and well presented submissions. Points will be deducted for the following:
- Missing requirements
- Inaccuracies (possibly excepting minor ones)
- Poorly written explanations
- Code constructs or style not used in class