Using Objects
Objective #1: Use objects in a client program.
- Objects in any computer language come from something called classes. That is, in Visual Basic, a Button object comes from a class named Button. A Label object in VB comes from a class named Label. Java has its own built-in classes that can be used to make buttons and labels named JButton and JLabel. However, the AP exam does not test you on those graphical objects. Some objects are not graphical. For example, a String object could be used to store a word or phrase.
- A class is a file that acts as a template or recipe for how an object can be used in a program. The properties and methods that can be used with a certain kind of object are typed out in the object's class. So it is in the Visual Basic Button class where the Name, Text, ForeColor, & BackColor properties as well as the Click and other methods are typed out.
- A class that contains a main method like our HelloWorld program is usually called a "client program".
- You can use buttons, labels and other objects in a Java client program similar to how we used objects on a Form in our Visual Basic class last year.
Objective #2: Declare and instantiate objects.
The following explanations reference this demo program
public class TurtleHelloWorld
{
public static void main(String[] args)
{
World earth = new World();
Turtle donatello = new Turtle(earth);
donatello.forward();
}
}
- declaring a reference to an object
To use a Turtle object in a client program, you must first create a World object since a Turtle must have a World that it lives in. The statement below declares a variable named earth.
World earth;
Technically the statement declares a reference to an object because the variable earth stores a memory address which can be considered a reference to the actual underlying object in the memory (RAM - primary storage) of the computer.
The name of the variable is earth. I could have used any name that I like as long as it doesn't contain a space or other illegal characters. It is good style to begin variables with a lowercase first letter even if the word is a proper noun.
Because earth is a variable that refers to an object, it can also be referred to as an object variable.
Given the declaration statement int num = 5; you would not say that num is an object variable.
By typing World in front of the name of the variable, I've specified that earth has the data type (aka type) World.
- instantiating an object
Next you need to actually instantiate the actual object with the statement
earth = new World();
The keyword new is a command in Java that causes the object to be created and not just declared. We'll talk about the use of the parentheses later in the course.
So, in review, World earth; is a declaration statement that declares a reference to the object variable earth as a World data type.
The statement earth = new World(); is an instantiation statement that causes earth to not only be a valid object variable name but also an instantiated object.
These two statements (the declaration statement & the instantiation statement) can be combined into one statement as they are in the demo program above.
World earth = new World();
- Next you can declare and instantiate a Turtle object.
Turtle donatello;
donatello = new Turtle(earth);
but notice in this instantiation statement, you must specify which World the Turtle donatello is going to live in.
Those two statements could be consolidated into one as they are in the demo program above.
Turtle donatello = new Turtle(earth);
- Notice that earth anddonatelloare object variable names in the example above. You should always begin variable names with a lowercase first letter. The names of classes, like Turtle and World, however will always begin with an uppercase letter.
- Now that you have created a Turtle object, you can have some fun and call methods that are found in the Turtle class.
- You can see in the Turtle class API (explained below) that a Turtle object has a forward method, a turnRight method and many other methods that are inherited from the SimpleTurtle class.
- Here is a code segment that makes donatello move forward, turn right, and then move forward some more.
donatello.forward();
donatello.turnRight();
donatello.forward();
Just like you learned in VB, a method is called by typing it with its parentheses after the name of the object and the dot operator.
- See the Turtle related demo programs on from 9/01/2011 to 9/20/2011 at http://www2.cs.uic.edu/~i101/code/
- Here is a link to the first four chapters of the textbook by Profs Mark Guzdial & Barbara Ericson who created the Turtle graphics library - http://coweb.cc.gatech.edu/mediaComp-plan/uploads/101/javaMediacomp-book--9-6-05-4chaps.pdf
- You can use a graphic as the background of your turtle's World with something like this:
Picture p = new Picture("C:\\Documents and Settings\\JohnDoe\\My Documents\\My Pictures\\beach.jpg");
Turtle t = new Turtle(100, 100, p);
Objective #3: Read a javadoc API
- Every class that is created by a professional programmer (or college professors or the College Board AP Computer Science exam creators) has an API. An API (Application Programmer's Interface) is a web page of documentation that explains what methods are found in the class.
- An API can easily be created using Eclipse if you add the proper javadoc comments to a class. We will not be creating an API in this course but you should understand that it is important to use javadoc comments in the real world where you are often responsible for creating API's.
- The API for the Turtle-related classes is found here. The Turtle class itself is found by clicking the Turtle entry on the left edge.
- When you scroll down the page about the Turtle class, you see a list of methods that are inherited from the SimpleTurtle class. Click on anyone of the methods to see a description of what it does.
Objective #4: Add static methods to a client program & call them from the main method.
- You could add a static method to a client class by typing it out below the main method. This allows you to call that method from within the main method. You would do this in situations where you have a task that you want to repeat many times without having to type out the code over and over again. In other words, adding methods to a client class allows for reusability of your code.
- See the drawSquare method in the TurtleWithStaticMethod demo program as an example of a reusable static method. Notice there is no return statement in the drawSquare method. That is why the word void is used in its header.
- See the makeRandomNum method in the TurtleWithStaticMethod demo program as an example of a reusable static method that returns an integer (i.e. int). The return statement causes a random integer to be returned to the method that calls makeRandomNum. Instead of using the word void in the header, the word int is used to show that this method returns a whole number.
- In the main method drawSquare is called multiple times. The call statement drawSquare(donatello); causes the computer to stop executing code in the main method and to transfer down to the drawSquare method. After executing the drawSquare method, the computer returns back up to where it left off in the main method. Notice that the drawSquare method is called with different turtles being passed as parameters. You must send a specific Turtle to the method as a parameter. Otherwise, the method wouldn't know which turtle should be used to draw a square.
Objective #5: Understand garbage collection.
- Garbage collection is an aspect of Java in which the memory occupied by unused object variables is constantly being reclaimed as a program executes.
public static void main(String[] args)
{
World earth = new World();
Turtle donatello = new Turtle(earth);
drawLine(donatello);
donatello.turnRight();
donatello.backward();
}
public static void drawLine(Turtle anyTurtle)
{
anyTurtle.forward();
}
For example, when the method drawLine is finished executing, anyTurtle is no longer used. In other words, it falls out of scope and cannot be used in the main method. So anyTurtle is garbage collected. However the object variable donatello is not garbage collected since it is still used up until the very last statement in the main method.
Garbage collection is not something that a Java programmer must worry about. It is an automatic feature of the Java language.