Exams they will be returned to you this week. 

In labs this week we will start using silo. 

In Java programs are made out of classes. 

There's nothing else but classes. 

What is in a class?

class Anything {
  int a; // instance variable
  static int b; // static variable 
  Anything(int a) { // constructor 
    this.a = a;
  }
  void fun(int c) { // instance method
    this.a = this.a + Anything.b + c; 
  }
  public static void main(String[] args) {
      
  }
}

A class can contain:

  (a) instance members (non-static variables or procedures/methods)

  (b) static members (static variables or procedures/methods)

  (c) constructors (help with the initialization of instance variables) 

A program has one or more classes. 

Variables in Java: 

  (a) static variables 

  (b) instance variables 

  (c) local variables (of the kind you define in a method)

  (d) parameters (are like local variables) 

Please refer to static variables via the class name . (dot) name of variable. 

Please refer to instance variables via the reference to the object that contains them. 

That means something . (dot) and then the name. 

Inside an instance method you must use this to refer to the instance that holds the method. 

Define a class Point that represents points in the plane (on the screen). 

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x; 
    this.y = y;
  }
  double distanceTo(Point p) {
    int dx = this.x - p.x; 
    int dy = this.y - p.y; 
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  public static void main(String[] args) {
    Point a = new Point(); 
  } 
}

Now this doesn't work we discuss and come up with:

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

If we compile and run this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Point
0.0
1.4142135623730951


Define a class Line that represents a line between two points in the plane. 

Lines are supposed to be smart: when asked they are able to report their length. 

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 (3, 4)); 
    System.out.println( a.length() ); 
  }
}

Please model a triangle. Triangles can report their areas. 

class Triangle {
  Line a, b, c; 
  Triangle(Point a, Point b, Point c) {
     this.a = new Line(a, b); 
     this.b = new Line(a, c); 
     this.c = new Line(b, c);      
  }
  double area() {
    // http://mathworld.wolfram.com/HeronsFormula.html
    double s = 0.5 * ( a.length() + b.length() + c.length() ); 
    return Math.sqrt( s * (s - a.length()) * (s - b.length()) * (s - c.length()) ); 
  }
  public static void main(String[] args) {
    Triangle a = new Triangle( Point.origin, new Point(3, 0), new Point(0, 4) ); 
    System.out.println( a.area() ); // should print 6.0
  }
}

This is how it runs:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Triangle
6.0


Now let's remember arrays and ArrayLists. 

Arrays are of a uniform type and that includes primitive types. 

Arrays can be of any size, but they are not resizable. 

int a = new int[6]; // a is stuck with 6 cells 

Triangle b = new Triangle[12]; 

ArrayLists are far more flexible, memory management is automatic.

ArrayLists are also of uniform type, primitive types are excluded. 

You also need to import the class from java.util (fine). 

Please model a Circle. 

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

Do you remember arrays? Or ArrayLists? 

class Rectangle {
  Point location;
  int width, height; 
  Rectangle(Point location, int width, int height) {
     this.location = location; 
     this.width = width; 
     this.height = height;
  }
  double area() {
     return this.width * this.height;  
  }
  public static void main(String[] args) {
     Rectangle a = new Rectangle( new Point(-1, -2), 3, 5 ); 
     System.out.println( a.area() ); // prints 15
  }
}

Now I want to put one Rectangle and two Circles in an array(list)? 

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

If we run this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Tyler
[Point@546c5a0c, Circle@4a7e161d, Line@5689cb2c, Triangle@686ef99b, Rectangle@5ee472a0]


This is possible because of the class extension mechanism. 

Tonight I will e-mail you to tell you what to read for Wed. 

See you then? 

--