Journal for SP Snowperson

Code:

Specifications

Write a program that creates the head of a SnowPerson. The head should have two eyes, a nose, and a mouth. We have created some classes for you in the package cs015.SP which you can use to aid you in programming this assignment.

The Process

In this case a lot of objects already exist for us, so we only need to instantiate a couple of things, and we will be all set. What are the specs? We want a SnowPerson's face. Well, what does that consist of? I think we will need a face to act as a background, then two eyes, a nose, and a mouth. We actually already have an object that contains two eyes, so we just need to instantiate one of those objects.

We know that we want to do very little in the Applet, so the only thing we should do is instantiate the SnowFace class that we have created. Our SnowFace class can contain everything else. Since we know that most things have been written for us, we'll just have the SnowFace instantiate one of everything (keeping in mind that the RoundEyes class contains two eyes). If we put all of this in the constructor, and keep them as instance variables, everything will appear and nothing will disappear until we want it to.

That's it, pretty simple huh?

Questions

Why is there a private before all the variables?
I made the variables private because I don't want anything else to have access to my data. In the SnowFace class, I don't want anything else to know what I contain. I just want other objects to think of me as a Face, and not think about what other parts I might have. It is my responsibility to change any of my properties this way, and I can make sure it is done right. Allowing some other object access to my variables could let them change something without me knowing it, and possibly screw up my data. This is bad, so by making things private, I can make sure that everything is changed correctly.
Why do the variables all have underscores at the beginning?
In this case it's pretty easy to see what my variables are, and where I declared them. And technically, putting underscores doesn't make much of a difference. What the underscore does is remind me (and you the reader of the code) that that specific variable is an instance variable (as opposed to a local variable). So this way I know that whenever I use this variable, it has been declared at the top and is something that will remain with the class forever. If the variable does not have an underscore, I can assume it is local to the method, and that it will not stick around once the method is closed. So if I want to do anything that is permanent to the class, I better make sure I am using an instance variable.
Why did you create everything in the constructor?
A face is made up of a number of parts, in this case the eyes, nose, and mouth. I want to make sure that every time a face is created, the other parts are created as well. I can guarantee that this happens by instantiating (creating) these variables in the constructor. The constructor of an object is called every time it is instantiated. So something that I want to happen each time the object is created should go in the constructor. Also, in this case I do not have a lot of code. If I had a lot I wanted to do every time the object is created, I might have broken it down into different methods and simply had the constructor call those methods.

Comments


Last modified: Mon Mar 2 16:19:33 EST 1998