CS21A: Smiley

| -Uncategorized

I drew a smiley on the board and provided the skeleton for a new class that drew only the circle that represented the face. I asked the students to get it to display a smiley according to the drawing. While they did that, I downloaded my source code for Smiley.java and the modified classes from the BlueJ sample project (added move() to Triangle and Circle) and showed them my implementation. I wanted them to define a class that supported many of the operations the basic shapes did. I started with a single Circle attribute representing the face, a constructor that created the circle, and a makeVisible() method that made the face visible.

The major conceptual point I wanted to make was:

You can define your own class that contains objects. You can call methods on those objects and you don’t have to worry about the implementation details. You can support the other methods by passing the method calls to the different objects.

A number of students started by looking at Circle.java and trying to model their work on it, but I suggested working from the very simple skeleton we had on the board. This greatly helped the students who were getting lost in the source code.

I also pointed out that they could use BlueJ to determine the coordinates and sizes experimentally. Instead of edit-compile-test, they could interactively create an object, manipulate it, and then inspect it to find the necessary values.

This exercise was open-ended. After students got the smiley to display, they added support for moveUp(), moveDown(), moveLeft(), and moveRight(). moveHorizontal(int distance) and moveVertical(int distance) followed shortly after. move(int x, int y) required a bit more thought because they couldn’t just pass the request on. I also had a simple animate() method. I suggested making it possible for the user to change the smiley’s colors (face, eyes, and mouth – separate methods, or one method with three parameters) or size (bit more complicated).

I think this exercise worked very well. I did not have to tell the students exactly what to do, although I gave them a number of hints. I exposed my planning process – first, get the face to display, then add the eyes, then the mouth, then add other methods one at a time. Students learned how to plan implementation in small steps. I did not tell them what they had to do; I just drew a figure on the board and had a demo up on the projector. On their own, they figured out that they needed to specify negative height in order to get an inverted triangle and add offsets during the move.

You can comment with Disqus or you can e-mail me at sacha@sachachua.com.