CS 139 Algorithm Development
Colors
For this lab you are going to create a Color139
class. Java already comes with a Color class, but
we are going to create our own variant of that class.
Instructions
- Download a copy of the Main.java file.
This file implements the main function you will use to test your
implementation of Color139.
- Download a copy of the Color139.java
file. The documentation for the Color139 class
that this file implements is here.
- Look at the parts of Color139 that have
already been defined.
- A number of constants have been defined. Those constants are public
so they may be used outside of the class (you should note those
constants are referenced in Main.java).
- Three instance variables are defined. Thus every object that is
created will have its own copy of those three instance variables.
Those instance variables are private so they can only be accessed by
methods in the class.
- The methods toString, dim, and add have
already been defined. Look at those methods and ask questions about
anything that you do not understand.
- Implement the constructor method. Recall that a constructor's job is to
initialize an object to a particular state. In this case, 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 Color139.java file as needed. A sample of
what your program should produce is here.
- 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.
- 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
In addition to running your java programs within jGrasp, 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, 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 with a browser.
Updated 11/01/05