Assignment 3
Modifying classes and extending classes
Submit before 11:30 Saturday February 3

Overview

For this assignment, you will modify the Color class and extend it to create the NamedColor class.

Instructions, code writing and questions

  1. Download the assn3 zip folder.
  2. 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.
  3. 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?
  4. 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.
  5. 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))
    
  6. 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.
  7. 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.
  8. 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:

  1. 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.
  2. Your clearly labeled answers to explanations and questions
  3. A summary of the files included in the zip folder
  4. 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 5 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