We hold these truths to be self-evident:

  that Java programs are made out of classes
  that classes are containers of static members and blueprints
  that blueprints can be instantiated (they contain instance members)
  that instance variables should be private, and initialized by constructors
  that the current reading assignment is Chapter 9

So far we have explored:

  literals (numbers, Strings, etc.) operators, expressions, types, variables,
  assignments, booleans, conditionals, if statements, loops (while, do, for),
  all of these happening in (static) methods, with the occasional object: be
  it a (two-dimensional) array, an array list, a Scanner, or a JFrame. 

So at this point we have: 

 1. Introduction 
 2. Primitive Types
 3. Conditionals
 4. Loops
 5. Methods (static)
 6. Arrays and ArrayLists
 7. -- 
 8. Simple Modeling
 9. The Class Extension mechanism (this week)

I will post the new lab and the new assignment today. 

Trevor, Jing, Chia-Hsuan, Jack G, Noor A-M, Rob J, Ryan P, Sara T, Thomas D,
Alex N, Kongchen J, Yi Z, Qingyue L, Shengyu C, Andrew 

; A Point is a (make-point Number Number) 
(define-struct point (x y))

public class Point {
  private int x, y; 
  public Point() {
    
  } 
  
}

However the starting point was:

class Example {
  public static void main(String[] args) {
    Point a = new Point();
    a.x = 3; 
    a.y = 5;
  }
}

public class Point {
  int x, y; 
  
}

But this is not like the (define-struct so we introduce private. 

Now we have:

public class Point /* extends java.lang.Object */ {
  private int x, y; 
  public Point() {
    // System.out.println("I am starting."); 
    // Error: call to this must be first statement in constructor
    this(0, 0); // call the code from the other constructor with params 0, 0
    System.out.println("I am done."); 
  }
  public Point(int x, int y) {
    // System.out.println("I am in the constructor."); 
    this.x = x;
    this.y = y;
  }
  public int getX() {
    return this.x;  
  }
  public int getY() {
    return this.y;
  }
}

And the code for Example: 

class Example {
  public static void main(String[] args) {
    Point a = new Point(3, 5);
    Point b = new Point(); 
  }
}

So far: 

  -- the motivating example this week: sorting of an array(list) of objects

  -- the use of this(...) to call a peer constructor (and the limitations)

  -- the mystery of toString(...)

  -- the mystery of System.out revealed 

class Example {
  public static void main(String[] args) {
    Point a = new Point(3, 5);
    Point b = new Point(); 
    System.out.println( a.toString() ); 
    System.out.println( b ); 
  }
}

public class Point /* extends java.lang.Object */ {
  private int x, y; 
  public Point() {
    // System.out.println("I am starting."); 
    // Error: call to this must be first statement in constructor
    this(0, 0); // call the code from the other constructor with params 0, 0
    System.out.println("I am done."); 
  }
  public Point(int x, int y) {
    // System.out.println("I am in the constructor."); 
    this.x = x;
    this.y = y;
  }
  public int getX() {
    return this.x;  
  }
  public int getY() {
    return this.y;
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y;
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  public String toString() {
    return "Point(" + this.x + ", " + this.y + ")";  
  }
}

When I run it I get: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Example
I am done.
Point(3, 5)
Point(0, 0)


Who's calling toString() for b? 

Not only that but the code still runs even if I delete toString(); 

class Example {
  public static void main(String[] args) {
    Point a = new Point(3, 5);
    Point b = new Point(); 
    System.out.println( a.toString() ); 
    System.out.println( b ); 
  }
}

public class Point /* extends java.lang.Object */ {
  private int x, y; 
  public Point() {
    // System.out.println("I am starting."); 
    // Error: call to this must be first statement in constructor
    this(0, 0); // call the code from the other constructor with params 0, 0
    System.out.println("I am done."); 
  }
  public Point(int x, int y) {
    // System.out.println("I am in the constructor."); 
    this.x = x;
    this.y = y;
  }
  public int getX() {
    return this.x;  
  }
  public int getY() {
    return this.y;
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y;
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  //  public String toString() {
  //    return "Point(" + this.x + ", " + this.y + ")";  
  //  }
}

I am going to explain with the following:

public class Horse {
  public void talk() {
    System.out.println("Howdy");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse()
> a.talk()
Howdy
> Horse b = new Horse()
> b.talk()
Howdy


http://ecx.images-amazon.com/images/I/61Qj4apY99L._SY355_.jpg

https://s-media-cache-ak0.pinimg.com/236x/8f/c2/48/8fc248172ba51937f7afe450e074af88.jpg

public class Unicorn extends Horse {
  public void sing() {
    System.out.println("Bork! Bork");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Unicorn a = new Unicorn()
> a.talk()
Howdy
> a.sing()
Bork! Bork


So the class extension mechanism is a way to model in stages.

The class extension mechanism is just the set union of (instance) features. 

Horse = { talk() } 

Unicorn = Horse U { sing() } = { talk(), sing() }

This is called inheritance (that the Unicorns can talk). 

class Example {
  public static void main(String[] args) {
    Point a = new Point(3, 5);
    Point b = new Point(); 
    System.out.println( a.toString() ); 
    System.out.println( b ); 
    
    System.out.println( Point.origin.distanceTo(new Point(1, 1)) ); // 1.4142
  }
}

public class Point /* extends java.lang.Object */ {
  private int x, y; 
  public Point() {
    // System.out.println("I am starting."); 
    // Error: call to this must be first statement in constructor
    this(0, 0); // call the code from the other constructor with params 0, 0
    System.out.println("I am done."); 
  }
  public Point(int x, int y) {
    // System.out.println("I am in the constructor."); 
    this.x = x;
    this.y = y;
  }
  public int getX() {
    return this.x;  
  }
  public int getY() {
    return this.y;
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y;
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  public String toString() {
    return "Point(" + this.x + ", " + this.y + ")";  
  }
  public static Point origin = new Point(); // this is a static feature!!
}

This runs as follows: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Example
I am done.
I am done.
Point(3, 5)
Point(0, 0)
1.4142135623730951


public class Unicorn extends Horse {
  public void sing() {
    System.out.println("Bork! Bork");  
  }
  public void talk() {
    System.out.println("Bonjour.");  
  }
}

public class Horse {
  public void talk() {
    System.out.println("Howdy");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Unicorn a = new Unicorn()
> a.talk()
Bonjour.
> Horse b = new Unicorn(); 
> b.talk()
Bonjour.


public class Unicorn extends Horse {
  public void sing() {
    System.out.println("Bork! Bork");  
  }
  public void talk() {
    super.talk(); 
    System.out.println("Bonjour.");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Unicorn b = new Unicorn();
> b.talk()
Howdy
Bonjour.


We'll work with BlueJ next time. 

Next time quiz:

public class Point /* extends java.lang.Object */ {
  private int x, y; 
  public Point() {
    // System.out.println("I am starting."); 
    // Error: call to this must be first statement in constructor
    this(0, 0); // call the code from the other constructor with params 0, 0
    System.out.println("I am done."); 
  }
  public Point(int x, int y) {
    // System.out.println("I am in the constructor."); 
    this.x = x;
    this.y = y;
  }
  public int getX() {
    return this.x;  
  }
  public int getY() {
    return this.y;
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y;
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  public String toString() {
    return "Point(" + this.x + ", " + this.y + ")";  
  }
  public static Point origin = new Point(); // this is a static feature!!
}

--