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

public class Point {
  private int x, y; 
  public Point (int x, int y) {
    this.x = x; 
    this.y = y;
  } 
  public Point() {
    this(0, 0); // call the other constructor 
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x; 
    int dy = this.y - other.y; 
    return Math.sqrt(dx * dx + dy * dy); 
  }
  static Point origin = new Point(); 
}

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

In DrJava:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Point a = new Point(3, 4)
> a
Point@62255b2f
> a.distanceTo(new Point())
5.0


In BlueJ:

I need a picture. 

http://www.cs.indiana.edu/classes/c212/fall2015/image1005a.jpg

I also change the main method to point out a structure:

public class Example {
  public static void main(String[] args) {
    Point a, b; 
    a = new Point(); 
    b = new Point(3, 4); 
    System.out.println(Point.origin.distanceTo(b)); // 5.0
    System.out.println(Point.origin.distanceTo(a)); // 0.0
    // both look like System.out.println(...) 
  }
}

I change it again: 

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

I also modify a bit the Point class: 

public class Point {
  private int x, y; 
  public Point (int x, int y) {
    this.x = x; 
    this.y = y;
  } 
  public Point() {
    // System.out.println("The point is being created ha ha."); 
    // Error: call to this must be first statement in constructor
    this(0, 0); // call the other constructor 
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x; 
    int dy = this.y - other.y; 
    return Math.sqrt(dx * dx + dy * dy); 
  }
  static Point origin = new Point(); 
  public String toString() {
    return "Point(" + this.x + ", " + this.y + ")";  
  }
}

When I run Example it gives: 

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


If I take the toString out from Point we get:

public class Point {
  private int x, y; 
  public Point (int x, int y) {
    this.x = x; 
    this.y = y;
  } 
  public Point() {
    // System.out.println("The point is being created ha ha."); 
    // Error: call to this must be first statement in constructor
    this(0, 0); // call the other constructor 
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x; 
    int dy = this.y - other.y; 
    return Math.sqrt(dx * dx + dy * dy); 
  }
  static Point origin = new Point(); 
//  public String toString() {
//    return "Point(" + this.x + ", " + this.y + ")";  
//  }
}

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


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Example
Point@6ed92269
Point@48ee96d4


What exactly is happening?

Sara C, Longhua Z, Chris P, Aaron C, Brian W, Mitch W, Tyler M, Domino B, Timmy
Joseph A, Aiyun X, Xinhui S, Yiming L, Daozhen L, Yifan Z, John K, Brian F, Roy S, 
Andrew M, Anthony K, Brandon W 

Kevin B, Victor P, Jack G, Ben L, Abby S, Tori M, Shuanghao L, Nate S, Annette M, Rob S, Sam E
Ke Z, Hunter J, Ryan B, Tristan R, Carson B, Ivy M, Ingrid W,
Sergio P, John G, Drake D, Duncan T

There are two mysteries we need to explain:

(a) how is toString being called? (not explicitly)

(b) how is it still called? (when we delete it)

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

public class Point {
  private int x, y; 
  public Point (int x, int y) {
    this.x = x; 
    this.y = y;
  } 
  public Point() {
    // System.out.println("The point is being created ha ha."); 
    // Error: call to this must be first statement in constructor
    this(0, 0); // call the other constructor 
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x; 
    int dy = this.y - other.y; 
    return Math.sqrt(dx * dx + dy * dy); 
  }
  static Point origin = new Point(); 
}

That's the thing that we can explain this week (b). Answer: by inheritance. 

(a) is just a convention: when an object reference is placed in a String context
its toString method (it inherits one from Object) is being called if you don't 

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
Horse@1583f0d9
> a.talk()
Howdy


Can you model a Unicorn? 

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

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

The class extension mechanism is basically the set union of features:

Horse = { talk() }

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

What do we get from the class extension mechanism? 

Here's what we get:

  (a) inheritance  (Unicorns can talk because they are Horses) 

  (b) polymorphism (Unicorns are Horses, they are also Unicorns)

  (c) dynamic method lookup 

  (d) constructor chaining 

Today let's clarify (a), (b) and (c). 

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


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

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

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn();
> a.talk()
Howdy
> Unicorn b = new Horse(); // not good
Static Error: Bad types in assignment: from Horse to Unicorn


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


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


Next time we review this and we talk constructor chaining. 

--