Object Oriented Programming

This lab will teach you some fundamentals of working in an Object Oriented Language. This is a very important lab, give it your best.

Objects

An object is a entity that lives in the heap. It is created from a template (a class) while your program is running. Objects contain data (instance variables) and the code that operates on that data (instance methods).

When you write a java program, you write classes. In general, a class is a template for making objects---each object can be thought of as a copy of a class. The class defines both the instance variables and the methods that an object has. The process of creating a new object from a class is called instantiation. You use the keyword new to instantiate (i.e., create) an object. For example, the code:

new myClass();

would create (instantiate) an object in the heap from the class myClass and then return the location of that object. Unfortunately, Java can't refer to things in the heap by a name. After an object is created, the system forgets where it is. So, in order to use objects, we must store their locations in reference variables

Reference Variables

Simply put, a reference variable stores an address of an object. It refers to an object; it serves as the object's name. Previously, you learned about "primitive variables," variables that have primitive data types. You can think of primitive variables as storing data, and reference variables storing links to objects (i.e., their addresses). Creating reference variables is just like creating primitive variables. When you create a new reference variable, it links to nothing. You make it link to an object when you assign to it (using the assignment operator =) the value returned by new. Pretend we have a class called intHolder which looks like this:

class intHolder {
  private myInt;

  public intHolder (int newInt) {
    myInt = newInt;
  }

  public int getInt() {
    return myInt;
  }
}

The following Stack N' Heap demonstrates the difference between primitive and reference variables.


Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primative variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
Code
int prim = 5;
intHolder ref = new intHolder(5);

System.out.println( "prim = " + prim );
System.out.println( "ref.getInt() = " + ref.getInt() );
Stack
Heap
Temporary

Parts of an Object

Objects can have both state and behavior. The state of a object is maintained in instance variables and instance methods provide the behavior. Instance variables are the data storage of an object (i.e., its state), each object has its own set. The instance methods are bound to each object and work with that object's variables. For example, lets say you had two intHolder objects, one called bi which has 2 in it's instance variable, and one called tri which has 3 in it's instance variable. If you typed the command:

	
bi.getInt();

you would run the instance method getInt() in the bi object. This would return the number 2.

Static

Unlike instance variables and methods, static variables and methods are not part of any object. Static variables and methods are part of the class. When you create a static variable, only one copy of that variable will ever exist in memory, and the same is true for a static method. When you call a static method or use a static variable, you give the name of the class first, followed by a period, and then the name of the method or variable. For example, lets say that the intHolder class has a static variable called maxValue, the code for it might look like this inside the class:

public static int maxValue = 3000;

To access the maxValue variable, from any place in your program you would type intHolder.maxValue. Because there can be only one class called intHolder in your program, there is also only one variable called intHolder.maxValue. Any intHolder object that you instantiate from this class would not have its own maxValue, HOWEVER, you can access the intHolder.maxValue variable from within instance methods just as you would from anywhere else in your program.

Constructors

When we first instantiated the intHolder object above using new, you may have noticed that we placed a number in parenthesis following the class name: intHolder ref = new intHolder ( 5 );. This number is passed to the intHolder object's constructor. A constructor is the first method that is run when an object is instantiated. The intHolder's constructor is:

public intHolder (int newInt) {
	myInt = newInt;
}	

To write a constructor, you: (1) write a method that has NO return type, and (2) make the method name the same as the class name. Constructors are covered in much more detail in your book.

Using Objects
To Get Started ...

Download the jar file: Forest.jar to your csci/201 directory, unjar the file (Help), create a new Netbeans project called Forest, and mount the Forest directory created when you unjarred the archive file (Help).

Notice that your project contains an html file Forest.html. This means we'll be developing an applet this week. There's also a Java source code file Forest.java. You'll be extending this file. Finally, there's a Java bytecode file Triangle.class. You'll use the Triangle class, but you will not modify it. As part of this lab, you'll create a new class called Tree and add it to your project.

The code for the class Triangle of package edu.unca.cs.csci201.LabAids is shown below. Study the comments of this code for a moment. You'll need to use the methods of Triangle to complete this assignment.

package edu.unca.cs.csci201.LabAids;
import java.awt.*;

public class Triangle 
{
  // This class creates an upward-pointing equilateral triangle.
  // Each triangle is specified by the coordinates of its top
  // corner, the length of its sides, and its color.

  private Color color;
  private Polygon triangle;

  public Triangle(int x, int y, int length, Color c)
  // x and y are the coordinates of the top corner of the triangle,
  // length is the length of its sides, and color is is the fill Color 
  {
    int[] xpoints = {x, x-length/2, x+length/2};
    int[] ypoints = {y, y+(int)(0.866*length), y+(int)(0.866*length)};
    triangle = new Polygon(xpoints, ypoints, 3);
    color = c;
  }

  public void draw(Graphics g)
  {
    g.setColor(color);
    g.fillPolygon(triangle);
  }
	
}

Now take a look at Forest, our applet class. This applet, in its incomplete state, contains one instance variable, triangleA. You should use this variable when you create the first Triangle object.

Applets contain a paint() method. The paint() method is called whenever the browser needs to display (or redisplay) its windows. The class Forest contains an additional method called init() which is called when the applet is first loaded into the browser. Usually init() creates the objects used by the applet.

Your Assignment ...

Now that you are familiar with the setup, create three Triangle objects in your applet and draw them to produce a display similar to the pine tree shown below.
The lonesome pine

Now that you are good at drawing the top of a tree, try drawing a complete tree by adding a tree trunk. To do this, add a call to the fillRect() method of the Graphics object in the applet's paint() method. The fillRect() method should be familiar. Make the tree trunk grey as shown below.
A lonesome pine with a trunk

Instructor Checkoff ...

Show your lab instructor the lonesome pine tree.

Adding a New Class

Now that you can make a tree, why not make a Tree class so that it would be easy to create a whole forest of trees?

Your Assignment ...

Add a new, empty class to your project and call it Tree. (Help) Make the Tree class public. Since it will use Triangle, you must make sure to import edu.unca.cs.csci201.LabAids.Triangle along with all the classes of the java.awt package.

Now, copy the contents of the Forest class into your Tree class and change the names of its two methods. Make the init() method be the Tree class constructor, and change the name of paint() to draw(). Just making these simple changes should create a Tree class that can be used to draw a single tree. Now modify the Forest applet to create a single Tree object and draw it.

Instructor Checkoff ...

Show your lab instructor the classy pine tree.

Enhancing the Tree Class

Now, what you want to do is draw multiple Tree objects and allow each object to be positioned at a different location and have a different color. In order to do that, we have to introduce additional instance variables to the Tree class allowing us to give a unique position, and color to each Tree object. These variables must then be used to position the triangles and rectangle that form the tree, as well as color the triangles.

Your Assignment ...

Make the changes described above to the Tree class, and then draw three trees similar to those shown below.
The forest

Instructor Checkoff ...

Show your lab instructor a forest with at least three trees of different colors.