CS 051 | Fall 2011 |
   new FramedRect(100, 100, 50, 75, canvas);
This creates a new object of type FramedRect.
This particular object is located at x = 100, y = 100. It is 50 pixels wide, and 75 pixels tall.
We can use instance variables to give our objects a name, so that we can refer to them later, as in:
   private FramedRect smallBox = new FramedRect(60, 50, 25, 25, canvas);
which creates a 25 pixel by 25 pixel framed rectangle 60 pixels from the left edge of the canvas
and 50 pixels down from the top of the canvas.
Now, we can use the name "smallBox" to refer to the object or its methods. For example,
   smallBox.setColor(new Color(100, 100, 100);
uses the name "smallBox" to identify which object we want to change, then uses that object's
"setColor" method to change the objects color to a shade of gray.
If we want to use our own custom made objects, we have to define what they look like, and what
values and methods they will have. We use the Java Class file to do this.
The structure of a class file is as follows:
  public class Name   {
      constant and instance variable declarations
      constructor definitions
      method definitions
  }
where Name is what we want to call our types of objects. Name can be anything,
but remember that Class names should always be capitalized.
We defined our own class called
Classy Basketball that allowed us to create a more realistic looking ball
for our basketball game.
When we define such a class:
   public void moveTo(double x, double y) { ... }
   public void moveTo(Location point) { ... }
If a particular method name occurs more than once in a class we say that the method is
overloaded. Usually we will avoid doing this unless it is clear that the methods should do the
same thing, but it is convenient to allow both kinds of parameters.
We can make sure that two overloaded methods do the same thing by making sure they execute
the same code. Suppose we wrote out the complete definition of the first version of the moveTo
method. Then we could write the second version quite simply as:
   public void moveTo(Location point) {
       this.moveTo(point.getX(), point.getY());
   }
It is much better to do this, than to repeat the same code in two places!! Making changes to the
code (either for new functionality or fixing mistakes we've made) means that we only have to
change the code in one place, not both.