Lab17: Passing and returning objects

 

Background

For this lab you are going to create a class named Color149. Python has several Color modules.  We will leverage one called webcolors.  We are going to create a simple class that is a variation on this. This lab was designed by Prof. Arch Harris for Java initially.

Painter penguin cartoon character

Objectives

  • Implement methods that accept objects as parameters.
  • Implement methods that return an object of the class.

Part 1: Getting Started

  1. Download a copy of color149.py. This file implements the main method you will use to test your implementation of Color149.
  2. Install the webcolors module in Thonny or your home computer by python3 -m pip install webcolors or using the Thonny Package Manager and searching for webcolors.
  3. Read through the code for color149.py and see what it is doing.
  4. You will run today's program from the command line. Recall that to use java from the command line, you must first change your working directory to be the location that holds your Python files.
  5. Run the file by: python3 color149.py.  
  6. This will create an output file named color149.html which you should view in a web browser.

Part 2: Implementation

  1. Look at the parts of Color149 that have already been completed.
    • A number of constants have been defined. Those constants are public so they may be used outside of the class.
    • Three instance variables(attributes) are defined. Thus every object that is created will have its own copy of those three instance variables. 
    • The methods __str__, add, and dim have already been defined. Look at those methods and ask questions about anything that you do not understand.
  2. Implement the constructor. Recall that a constructor's job is to initialize an object to a particular state. In this lab, your color class's state will not change after it is initially created. Run your python file.  The top portion of the html file that outputs the constants should work properly after you complete the constructor.
  3. Implement the other methods, one at a time. Rerun the program after implementing each method to test the implemented method. Make sure a method is working correctly before moving on to the next method.
  4. Note: Lighten will add 32 to each color component(red, green, blue), darken will subtract 32 from each, and brighten should do the opposite of lighten and multiply 1.2 times each component(red, green, blue).
  5. When you have everything working, open two browser windows: one with your web page and one with the sample output page. Ask a TA or instructor to verify that your program produces the correct output.

Submit your color149.py file via gradescope.com  by the end of the day.

Back to Top