Exams will be returned to you this week. 

In labs this week we will first make significant use of silo. 

In Java programs are made out of classes. 

What do we have in a class? 

(a) static members

(b) instance members

(c) constructors 

Types of variables in Java:

(a) static variables (have default values) 

(b) instance variables (have default values)

(c) parameters (like local variables, but they are initialized when method invoked)

(d) local variables (defined in method, must be initialized by programmer)

I will give you an example: 

class Something {
  static int a; // static variable 
  String b; // instance variable 
  Something(String b) { // constructors
    this.b = b;  
  }
  int fun() { // instance method 
    return this.b.length();  
  }
  public static void main(String[] args) { // static method 
    
  }
}

But this has no purpose. Let's work on purpose now. 

Please call static varibles by their class name . (dot) the variable name. 

Please refer to instance variables via the reference to their instance . (dot) their name.

That means inside an instance method you should use the keyword this to access instance variables. 

1. Please model Points in plane. 
2. Please model Lines in plane. 
3. Please model Triangles in the plane. 
4. Please model Circles and Rectangles. 

class Point {
  int x, y; // instance variables
  Point(int x, int y) { // constructor 
    this.x = x; 
    this.y = y;
  }
  Point() {
    this(0, 0); // calls the peer constructor  
  }
  double distanceTo(Point p) { // instance method 
    int dx = this.x - p.x; 
    int dy = this.y - p.y;
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  static Point origin = new Point(); // static variable 
  public static void main(String[] args) { // static method 
    Point a = new Point(1, 1);  
    System.out.println( a.distanceTo( Point.origin ) );
  }
}

The answer to the first exercise contains everything. 

    System.out.println( Point.origin.distanceTo( new Point(3, 4) ) );  // 5.0

This line reminds you how System.out.println is put together. 

class Line {
  Point a, b;
  Line(Point a, Point b) {
    this.a = a; 
    this.b = b;
  }
  double length() {
    return this.a.distanceTo(this.b); 
  }
  public static void main(String[] args) {
    Line a = new Line( new Point(1, 2), new Point(-2, -3) ); 
    System.out.println( a.length() ); // 5.830951...
  }
}

class Triangle {
  Line a, b, c; // instance variables
  Triangle(Point a, Point b, Point c) {
    this.a = new Line(a, b);  
    this.b = new Line(a, c);  
    this.c = new Line(c, b);  
  }
  double area() {
    // http://en.wikipedia.org/wiki/Heron%27s_formula 
    double a = this.a.length(); // local variables
    double b = this.b.length(); 
    double c = this.c.length(); 
    double s = (a + b + c) / 2; 
    return Math.sqrt( s * (s - a) * (s - b) * (s - c) );
  }
  public static void main(String[] args) {
    Triangle a = new Triangle( new Point(3, 4), new Point(-3, -2), new Point(5, 1) );  
    System.out.println( a.area() ); 
    Triangle b = new Triangle( Point.origin, new Point(3, 0), new Point(0, 4) );  
    System.out.println( b.area() ); 
    
  }
}

How about Circles? 

class Circle {
  Point center; 
  int radius;
  Circle(Point location, int radius) {
    this.center = location; 
    this.radius = radius;
  }
  double area() {
    return Math.PI * this.radius * this.radius;  
  }
  public static void main(String[] args) {
    Circle a = new Circle(new Point(-1, -2), 5); 
    System.out.println( a.area() ); // prints 78.539816... 
  }
}

You define Rectangle now. 

class Rectangle {
  Point location;
  int width, height; 
  double area() {
    return this.width * this.height;  
  }
  Rectangle(Point location, int width, int height) {
    this.location = location; 
    this.width = width;
    this.height = height;
  } 
}

Do you remember arrays and ArrayLists?

arrays are of a uniform type. They can have any size, but can't be resized.

ArrayLists are of a uniform type. Then can be resized automatically. 

One of them does not allow primitive types. 

class Andrew {
  public static void main(String[] args) {
    Circle c = new Circle(new Point(2, 3), 4);  
    Point p = new Point(-1, -2); 
    Rectangle r = new Rectangle(new Point(-5, 2), 5, 7); 
    Object[] a = new Object[3];
    a[0] = c;
    a[1] = p;
    a[2] = r; 
    System.out.println( java.util.Arrays.toString( a ) ); 
  }
}

This prints: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Andrew
[Circle@7adcdd17, Point@7f70bd5f, Rectangle@762f0bef]


Why is this possible? 

The class extension mechanism is at work here. 

See you on Wednesday. 

--