Return to lecture notes index
January 22, 2002 (Lecture 4)

Overview

In the next couple of classes we're going to move from plain English specification and abstraction into actual programming. Before that happens, it's important to understand how to translate your plain English into code, as well as understand basic Java syntax and conventions.

Thinking About Variables

Variables are nothing more than place holders. They are a space to hold something. Java is a strongly typed language, so when we declare variables, we must also declare what they will hold. Most of you are familiar with many different types of values: ints, floats, chars, &c.

In Java, there is another type of value -- a reference. Remember, in Java, objects are named with references -- unlike C++, you can not declare an object. You can only allocate an object dynamically, using the "new" operator.

When you declare a variable, such as "Person p", you are really declaring a reference variable, and specifically a Person reference variable, that is to say, you are asking the computer for a variable capable of holding the name of a Person object.

Please keep this in mind -- in Java, all variables store primitive values, not objects. We declare reference variables, not instances of objects.

Examining a Java Class

To help you guys understand Java classes, the example we covered in class is shown below. The comments further explain each portion.

  class Calculator
  {

      /* The variable below, lastResult, is a private data member.  Variables are generally declared private
         and some public accessor method is created to allow the variable to be set or changed.  Methods
         can also be declared private if you don't want them to be accessed outside of the class. For example,
         in a Human class, the methods "digest", "moveFoodToLargeIntestine", "moveFoodToSmallIntestine" would
         all be private methods.  You wouldn't want someone to be able to call any of those independently or
         else a blockage could occur. */

      private double lastResult = 0.0;

      /* The methods below are all what are called public methods.  Public methods can be accessed from 
         outside the class.  For example, in a Human class, the method "feed" would be public because anyone
         should be able to feed the Human object.  You can also declare public variables, but there aren't
         very many common reasons to do so. */

      public double add(double op1, double op2)
      {

         /* As you can see, we declared the add method as a public method.  The next descriptor "double"
            indicates that this method will return a double.  Next, we indicate the name of the method.  In
            this case, "add".  After that, we put the parameters of the method in parentheses.  These
            parameters tell the method what type(s) of data it should expect.  In this case, the add method
            should expect two double values. One will be named "op1" and the other named "op2". */

         /* Here we perform a simple calculation, adding op1 and op2 together and assigning that value to
            lastResult. */

         lastResult = op1 + op2;

         //Finally we return our result.

         return lastResult;
      }

      public double subtract(double op1, double op2)
      {
         //subtract code here
      }

      public double multiply(double op1, double op2)
      {
         //multiply code here
      }

      public double divide(double op1, double op2)
{ //divide code here
} public double sqrt(double op1) { //sqrt code here } /* You'll notice that the next two methods both have the same name. Each of them takes a different number of operands, however. In Java, you can overload methods as long as they have different signatures. If you call gcd with two parameters, it will use the code in the first method declaration below. If you call gcd with three parameters, it will use the second method declaration. */ public long gcd(long op1, long op2) { //gcd code here } public long gcd(long op1, long op2, long op3) { //gcd code here } public double getLastResult( ) { return lastResult; /* Here we could also use "return this.lastResult;". "this" is a reference to the actual object that is executing the code. The dot in between "this" and "lastResult" is the scope operator */ } }

Creating and Using a Calculator

At some point, you'll probably want to create an instance of class Calculator. You can do this with the following code:

Calculator c = new Calculator( );

The "new" command calls in the construction teams which builds an object based on the constructor contained within that class. Somewhere else you'll probably want to call methods within the Calculator object that you created. You can do this with the following code:

c.add(op1, op2);

This will call add within the Calculator named "c". We might have many objects that can add, so this specifies which one we want to execute the code.

A More Sophisticated Constructor

Instead of, or even in addition to, statically initalizing the lastResult value, we could write a constructor to do this. As was the case in C++, the constructor is called after the "new" operator allocates the object to initalize its state.

I actually don't like the name constructor very much. Instead, I think it should be called the initializer. By the time it is called, the object actually exists -- it doesn't allocate sapce and create binding tables. Instead, it just initializes the object's state. But, oh well...the good folks at Sun seem to have disagreed with me.

Regardless, we could write a constructor like this. Notice that no return type is specified. This is correct syntax for Java -- constructor's don't return a value or void to the program -- new actually returns the refernce.

  void Calculator (double lastResult)
  {
    // Notice the use of the "this" reference. 
    // As was the case in C++, "this" is a reference to
    // the current object and can be used with the scope
    // operator, the ".", to distinguish between the
    // conflicting object-wide adn local defintions of
    // "lastResult"

    this.lastResult = lastResult.
  }
  

With this, we can instantiate a new Calculator as follows:

  Calculator c = new Calculator (10.0);