CS 139 Algorithm Development
Lab13B: Immutable Objects

Objectives:

Students will be able to:

Background:

For this lab you are going to create a class named CSColor. Java already comes with its own Color class, but we are going to create a variant of that class.

Materials:

Acknowledgment:  Arch Harris


Part 1:  Getting Started

  1. Setup your program environment by creating a new folder.

  2. Download a copy of the Main.java file. This file implements the main function you will use to test your implementation of CSColor.

Part 2:  Implementation - Understanding the UML diagram and building stubs

CSColor

Above is the UML diagram for the CSColor class. While it has no documentation, it has enough information for you to build stubs for the CSColor class.

  1. Create the stubs for CSColor. Recall that a stub would be able to compile and it would include all of the attributes of the class and all of the methods of the class.
  2. You will notice that most of the methods have a return type of CSColor. What will you use for the return value? Without a return value the program will not compile. For now, you can use the value, null.
  3. Compile your stubs. Correct any errors that you encounter.
  4. Save this file as CSColorStubs.java. JGrasp will complain, but you should save it with this name. You will upload this file along with the completed version if you cannot demo it in class.

Part 3: Understanding the CSColor.java starter

  1. Download a fresh copy of CSColor.java from here: CSColor.java. Complete documentation for this class can be found here: documentation
  2. You will notice a series of static (belonging to the class not to any one instance of the class) final CSColor objects. These are some standard html colors. Each is calling the constructor passing in the corresponding RGB values for that color. BLACK has 0, 0, 0 for arguments, while RED has 255, 0, 0.
  3. Look at the return value for the sub method. In this case, instead of returning null like you did in your stub, you are returning one of the colors. That means that when you run it against main, it would simply show the color BLACK in place of that method call.
  4. These would be shown in a UML diagram like this one, the complete UML for this class:

CSColorV2 UML


Part 4: Implementing the CSColor.java class

  1. Implement the constructor method. 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. Compile your java files and run the main program as specified below. The top portion of the html file that outputs the constants should work properly. Debug your CSColor.java file as needed. Click here for a sample of what your program should produce. REFER TO Running Your Program to see how to test each of the methods.
  2. 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.
  3. When you have everything working, open two browser windows: one with your web page and one with the sample output page. Ask your instructor to check off that you have gotten your program to produce the correct output. There are no submissions for this lab.


Running Your Program

  1. In addition to running your java programs within an IDE, you can run your java programs from the command line. To use java from the command line, you must first change your working directory to be the working directory that holds your java files.

    If you have already compiled your java files in jGrasp or DrJava, you do NOT need to compile them again. But if you want to compile your java files from the command line, enter the following command.
          javac *.java
    If you have many errors and want to limit the number of error messages displayed, enter the following alternative command.
          javac *.java 2>&1 | head -20

    Normally, to run a java program that has the main in a file named Main.java, you would run the following command.
          java Main
    But instead of sending the program output to the screen, we want the output to go into a file. So for this program, execute it with the following command.
          java Main > output.html
    This will create a file named output.html which you should view in a web browser.


Updated 11/16/2014 (nlh)