Howdy.

Chris Kayl Levi Seth Jiebo Jared Jerry Vanessa Mike C He He Arif
Keqin Yinan Arthi Andrew T Sunghyun Zhiwei Matt L Brandon P Xing W
Kevin S Danny N Chenrui He Alex Stilian 

Absent: 

Joe, Haoyang, Siddartha, Rerajitha, Rajeev, 
Jiahao, Andrew Ray, David Lee, Alex Q, Xinyu Zhao, 

public class One {
  public int add(int n, int m) {
    // return n + m;
    /*
      for (int i = 0; i < n; i++) {
        m = m + 1; 
      } 
      return m; 
     */ 
    if (n == 0) 
      return m; 
    else 
      return 1 + add(n-1, m); 
  }
  public static void main(String[] args) {
    One a = new One(); 
    System.out.println( a.add(3, 4) ); // prints 7
  }
}

We also discussed Xming and X11 connection to silo via putty. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
7


Let's go on with generation two. 

public class Two extends One {
  public int mul(int n, int m) {
    if (n == 0) return 0; 
    else if (n == 1) return m;
    else return add( m , mul(n-1, m) ); 
  } 
  public static void main(String[] args) {
    Two a = new Two(); 
    System.out.println( a.add(4, 5) ); // 9
    System.out.println( a.mul(4, 5) ); // 20 
  }
}

This runs as follows: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Two
9
20


The last exercise is available online. Read it. 

Problem: 

  (a) define Circle

  (b) define Rectangle 

Please create a few objects of these types and store them in an array. 

public class Circle {
  public String toString() {
    return "Soy un círculo.";  
  }
}

public class Rectangle {
  public String toString() {
    return "Ik ben een rechthoek";  
  }
}

import java.util.Arrays; 

public class Seth {
  public static void main(String[] args) {
    Circle a = new Circle(); 
    Rectangle b = new Rectangle(); 
    Circle c = new Circle(); 
    Object[] shapes = new Object[3]; 
    shapes[0] = a; // polymorphism
    shapes[1] = b; 
    shapes[2] = c; 
    System.out.println( Arrays.toString( shapes ) ); 
    System.out.println( b.toString() ); 
    System.out.println( b ); // what's going on here? 
    // Answer: dynamic method lookup kicks in 
  }
}

Class Object has a method toString() that prints what Brandon said. 

Circle, Rectangle don't explicitly extend anything so they extend Object. 

By inheritance they get a toString() as it is defined in Object. 

This method is special: 

  every time an object reference is evaluated in a String context

  the toString method for that object is invoked automatically 

Just to be clear System.out.println( ... ) creates a String context. 

import java.util.Arrays; 

public class Seth {
  public static void main(String[] args) {
    Circle a = new Circle(); 
    Rectangle b = new Rectangle(); 
    Circle c = new Circle(); 
    Object[] shapes = new Object[3]; 
    shapes[0] = a; // polymorphism
    shapes[1] = b; 
    shapes[2] = c; 
    System.out.println( Arrays.toString( shapes ) ); 
    System.out.println( b.toString() ); 
    System.out.println( b ); 
    for (Object o : shapes)
      System.out.println( o ); 

    System.out.println( a.area() ); 
    System.out.println( b.area() ); 
    System.out.println( c.area() ); 
    
    for (Object o : shapes)
      System.out.println( o.toString() ); 
    
    // for (Object o : shapes)
    //   System.out.println( o.area() ); 
    // fix this in lab (two ways) 
       
  }
}

public class Rectangle {
  public String toString() {
    return "Ik ben een rechthoek";  
  }
  public double area() {
    return Math.E;  
  }
}

public class Circle {
  public String toString() {
    return "Soy un círculo.";  
  }
  public double area() {
    return Math.PI;  
  }
}

Here's how this works: