CS 117: Writing your own classses
Winter 2005

In the programs that you have written so far, you have used the Canvas class to draw pictures. While you undoubtedly had a lot of fun coming up with different drawings, the problem is that you have to "hard-code" whatever you want to draw. In other words, every time you want to change your drawing, you have to change the code and recompile it. Perhaps you find yourself drawing the same things over and over again, and you'd like to specify a way to just draw, say, a car, simply by calling a method that you've written to do so. This can be done by defining a Car class, and then creating multiple objects from that class.

In this lab, you will take a very small class called Car and gradually add new capabilities to it. Once you have a more sophisticated Car, you'll move on to starting a new class (Tree) from scratch. By the end of the lab, you'll have a picture of a bunch of cars on a tree-lined road, plus a bit more knowledge about the structure of Java classes.

The Car and Road classes

Create a directory named car, and copy Car.java, Road.java, and Canvas.java into it from /Accounts/courses/cs117/ondich/car.

Take a look first at Road.java. What do you expect to see when you run java Road? Notice that it uses objects from the class Car that we are creating. Take a look at Car.java to see how to set up a class that we can create objects from.

Compile Car.java, Road.java, and Canvas.java and run java Road. Does the picture look like you expected it to? If not, take a closer look at the code.

A Car of a different color

You may have noticed a tragic uniformity in the color of the cars. Let's fix that.

  1. Add a Color object variable (call it color) to the Car class. variable should be declared near the top of the class, right along with the declarations for the other object variables..
  2. Add a setColor method to the Car class. This method should begin with "public void setColor( Color newColor )", and it should simply assign a new value to the color instance variable.
  3. Change the Car constructor to set the color variable to the default value (red).
  4. Change the draw method to use color instead of red.
  5. Add a line to main in Road to change the color of one of the cars to white or yellow or something.
  6. Recompile and run. Did you get that automotive chromatic variety you have been craving?

SUVs and subcompacts

Now that you can control the color of your Car, let's try changing the size of the car. You can start by following instructions like those in the previous section: add a width instance variable to Car, add a setWidth method, initialize width in the constructor, and change main in Road.

The tricky part will be changing draw. In the original version of draw, you'll see numbers that depend on the width of the car being 100. But now you'll need to change those numbers to expressions that vary depending on the value of width.

You'll know you have gotten it right if you can modify the widths of your cars and get different-colored cars of different sizes on your road.

Growing your own

Let's add a little foliage. Using the ideas from Car, start from scratch and create a Tree class that allows you to control the height of the tree. Use the Car class as an example. Don't get too fancy with your drawing--a brown rectangle plus a green circle make a lovely tree at this stage. Again, you'll know that you have succeeded when your landscape has several trees of different sizes, all created as separate instances of a single Tree class.

Wrapping it up

You do not need to hand anything in for this lab, but please make sure you complete the lab and ask me any questions you have about it.

Have fun.


Written by Jeff Ondich, Dave Musicant, Amy Csizmar Dalal.