Classes and Objects

Why bother?

Answers:

On learning a programming language

An expert programmer knows all the tricks,
and doesn't use them. -- James Neighbors

For each construct you should learn:

Defining a class and a simple application

The file is named "ComplexTest.java". Normally one puts each class definition in a separate file and the files for a program/project into a single directory. *DO NOT* follow the comment style given here. These are explanatory comments for learning the language. Alternatively you can view these as bad comments, which are sometimes seen. We will provide examples of good commenting style.
     class Complex        //  end of line comment
   {
     private double x;   // private: not accessible outside of class
     private double y;   // data member
      
     Complex(double x, double y)   // constructor
       {
         this.x = x;        // x is passed parameter
         this.y = y;        // this.x is the x of the object
                            // . accesses data members
       } 
     Complex(double x)     // by Convention, class names begin with capital
      {                   // Java is case sensitive
         this(x,0);        // here "this" refers to constructor at this level
       }
     Complex()           // functions are distinguished by their parameters
     {
       this(0,0);
     }

     double getReal()     // accessor: get values of data members
    {                     // by conventions methods, start with lower case letter
       return x;
    }
     double getImaginary()
    { 
       return y;
    }
    void setReal(double x)   // mutators: change values of fields
    {
      this.x = x;
     }
    void setImaginary(double y)
    {
      this.y = y;
    }                    
    Complex  mult(Complex c)          
      {
        Complex d = new Complex(); // new allocates storage and returns reference to it.
         d.setReal( x*c.getReal() - y*c.getImaginary() );  
                 // . accesses member functions
         d.setImaginary( x*c.getImaginary() + y*c.getReal() );
        return d;
      }
 public String toString()  
/*     String is class in Java, not array of characters.
       toString() is useful for printing and debugging
       This overrides Object.toString(). Note that 
       overriding requires that the accessibility not be changed.
       toString() has unusual semantics. It is called whenever
       an object of type String is needed. Why did they do this?
*/

   {
     return(x+"+"+y+"i"); // + concatenates string objects
   }
}

public class ComplexTest   //note the class corresponds to the filename
{
  public static void main(String[] args)  // this line is required by applications
 {
   Complex c1 = new Complex(3,2); 
   Complex c2 = new Complex(1,2);
   System.out.println(c1.toString());
   System.out.println(c2); // automatic call to toString()
   System.out.println("product of c1 and c2 is ");
   System.out.println(c1.mult(c2));
   Complex c3 = new Complex(Double.valueOf(args[0]).doubleValue(),
                            Double.valueOf(args[1]).doubleValue());
   // note . is left associative
   System.out.println(c3);
 }
}

Java Class Vector in java.util

A vector holds objects and allows addition and deletion of objects. You may think of it as an extensible array. It has 3 constructors and about 30 methods. Vectors contain objects (references). If you need it, Vector probably has it.

Notes:

Address Book: Class Exercise

What are the objects: book, address, name, last-name,...

What are the behaviors:

Static Classes and Static Methods

Example

Return to the Complex number class. Suppose we want to count the number of complex numbers our program generated. To do this add a static data member "count" and modify our constructors. Static functions can only access static data members. We also add a corresponding print function.
  public  class Complex
  {
     private static int count = 0;    // int is associated with class
   
     private double x;   // x,y are associated with each instance of class
     private double y;
      
     Complex(double x, double y)   
       {
         this.x = x;        
         this.y = y;        
         count++;
       } 

     ...
   
    public static printCount()   
    { 
      System.out.println("Number of Complex numbers used is "+count);
    }
    // The output goes to the console. It would be called with
    //  Complex.printCount();     
Notice that static functions are not passed a "this" parameter.

Java Semantics

Design Rules (from Eiffel school)

Object-Oriented Design Review

Class/Object Summary

Primitive Types

What you should be absolutely sure of