Java programs are made out of classes.

Classes serve dual role: 

(a) they are containers of static members (methods/variables)

(b) they also hold a blueprint of instance members

You can instantiate the blueprints into objects. 

You will also see constructors in classes. 

Constructors are initialization procedures for instances. 

Constructors are not members, static or non-static. 

We instantiate blueprints with the new operator. 

Today we will use BlueJ to visualize objects. 

So we talked about: instance variables, instance methods and constructors. 

And we modeled:

public class Student {
  String name; 
  public Student(String name) {
    this.name = name;     
  }
  public void sayName() {
    System.out.println( this.name ); 
  }
}

In-class problem: 

Extend the model above to add:

(a) a birth year inside 
(b) ability of any student to answer to the questions:

  (b.1) how old were you in 1996? 
  (b.2) how old are you now (implicit current year is 2013) 

Example use of the new model: 

  Student a = new Student("Larry Bird", 1956); 
  a.sayName(); // prints:     My name is Larry Bird
  a.sayAge(); // prints:      Larry Bird is currently 57 years old
  a.sayAge(1979); // prints:  In 1979 Larry Bird was 23 years old

Here's what Michael (Team Five) developed: 

  public class Student {
    String name;
    int birthYear;
    public Student(String name, int year) {
      this.name = name;
      this.birthYear = year;
    }
    public void sayName() {
      System.out.println( "My name is " + this.name ); 
    }
    public void sayAge() {
      // this.sayName();
      System.out.println( this.name + " is currently " + ( 2013 - this.birthYear ) + " years old.");
    }
    public void sayAge(int y) {
      // this.sayName();
      System.out.println( "In " + y + " " + this.name + " was " + (y - this.birthYear) + " years old."); // need an if 
    }
    public static void main(String[] args) {
      Student a = new Student("Larry Bird", 1956);
      a.sayName();    // prints:  My name is Larry Bird
      a.sayAge();     // prints:  Larry Bird is currently 57 years old
      a.sayAge(1979); // prints:  In 1979 Larry Bird was 23 years old
    }
  }

And here's how this works on silo:  
 
  -bash-4.1$ cat Student.java
  public class Student {
    String name;
    int birthYear;
    public Student(String name, int year) {
      this.name = name;
      this.birthYear = year;
    }
    public void sayName() {
      System.out.println( "My name is " + this.name );
    }
    public void sayAge() {
      // this.sayName();
      System.out.println( this.name + " is currently " + ( 2013 - this.birthYear ) + " years old.");
    }
    public void sayAge(int y) {
      // this.sayName();
      System.out.println( "In " + y + " " + this.name + " was " + (y - this.birthYear) + " years old."); // need an if
    }
    public static void main(String[] args) {
      Student a = new Student("Larry Bird", 1956);
      a.sayName();    // prints:  My name is Larry Bird
      a.sayAge();     // prints:  Larry Bird is currently 57 years old
      a.sayAge(1979); // prints:  In 1979 Larry Bird was 23 years old
    }
  }  

  -bash-4.1$ javac Student.java
  -bash-4.1$ java Student
  My name is Larry Bird
  Larry Bird is currently 57 years old.
  In 1979 Larry Bird was 23 years old.
  -bash-4.1$

So it's exactly what we wanted. 

--