Making a Program

For this lab, you're going to be creating your own clown! Currently, the clown face doesn't have cheeks, eyes, a mouth, or a nose. The MyClown class that you've started writing will be responsible for creating cheeks, eyes, a mouth, and a nose. It will then add these parts to the empty ClownFace. In order to do this, MyClown will have to know about the empty clown face. Since MyClown didn't instantiate ClownFace, the best way to do so is through an association using the constructor.

CLOWN

"I could have worked for Google, but NOOO... I had to take CS17..."

Check Point 2

  1. Modify the constructor for MyClown so that it receives one parameter of type cs015.labs.ClownFaceSupport.ClownFace.
  2. Inside the constructor of App.java, instantiate an instance of MyClown passing it the empty ClownFace (look in the code comments to see where this is done).
  3. Compile what you have so far to make sure your parameters are syntactically correct. If you get any errors, try to track them down by line number.

Now it's time to add all the other components. A great place to instantiate child components is in the constructor of MyClown. Let's begin by adding cheeks to the clown face.

Check Point 3

  1. Inside the constructor for MyClown, instantiate an instance of cs015.labs.ClownFaceSupport.ClownCheeks.
  2. Compile and run the App. You'll see nothing has changed. This is because although you've made the cheeks, the empty clown face has no idea that you've made them. Fortunately, the clown face has a method you can call to tell it about the cheeks you've just made. cs015.labs.ClownFaceSupport.ClownFace has a method add(...) that takes in a cs015.labs.ClownFaceSupport.ClownCheeks as a parameter and does not return anything.
  3. Call the add(...) method inside the MyClown constructor on ClownFace object that was passed in and pass it the instance of cs015.labs.ClownFaceSupport.ClownCheeks that you've just instantiated.
  4. Compile and run the App again. Yay! The clown has cheeks now!

If you are having trouble getting the method invocation syntax correct, review the lecture notes. Remember that there are 3 parts to a method invocation: the receiver of the message, the name of the method, and the parameters (if there are any).

Check Point 4

  1. Finish up the empty clown face by adding eyes, a mouth, and a nose in the same way that you added cheeks. Here is a great opportunity for you to be appropriately lazy and copy and paste some of the code you already wrote (and make only small modifications)! The classes for them are:
    • cs015.labs.ClownFaceSupport.ClownEyes
    • cs015.labs.ClownFaceSupport.ClownMouth
    • cs015.labs.ClownFaceSupport.ClownNose

A Note on Design

We admit the example used in this lab is a bit contrived. From a design perspective, it makes more sense to have the MyClown instantiate and therefore contain the ClownFace (just like a CS15Mobile instantiates and contains its component tires). But now you have seen how one object can "know about" (and therefore send messages to) another object created somewhere else. The ClownFace was created external to the MyClown, but through the use of parameters, the MyClown established an association with the ClownFace. This allows the MyClown to call methods on the ClownFace and add parts to it.

In a real program, it would make more sense to have the MyClown contain the ClownFace.

Coding Conventions

Programming has a very unique style element to it. While the compiler couldn't care less about what your code looks like, neat and well formatted programs make it much easier to develop, debug, and look back at old code later on. CS15 has a Coding Conventions guideline that you should take a look at. (Coding Style will be part of the rubric for each programming assignment.)

Congratulations on finishing lab 1, you are well on your way to becoming an Xtreme Coder!

Make sure to get checked off by a TA before leaving!