9:30 AM 9/20/2012

Lecture Ten

Classes are containers: static and non-static members. 

Members = data + methods. 

One special static member is a method: main.

Literals, types, operators, expressions, variables, arrays. 

If, while, for, do-while, break, continue, return. Methods. 

Blueprints: using Java classes to model. Objects. 

public class Student {
  public String name; 
  public Student(String name) {
    this.setName(name); 
  }  
  public void eat(String food) {
    this.name = this.name + food;
  }
  public void talk() {
    System.out.println("Howdy, I am " + this.name);    
  } 
  public void setName(String name) {
    this.name = name;
  }
  public static void main(String[] args) {
    Student a = new Student("Larry"); // local variable 
    a.talk(); 
    a.setName("Larry Bird "); 
    a.talk(); 
    a.eat("bagels"); 
    a.talk(); 
    a.eat("shrimp"); 
    a.talk(); 
  }
}


In Java we have four kinds of variables: 

  local variables 
  parameters (names the ingredients, inputs to a method)
  instance variables (defined in class outside methods, not static)
  static variables (static variables defined in class outside methods)

1. Circles


public class Circle {
  public int x, y; // instance variables 
  int radius;
  public Circle(int x, int y, int r) {
    this.x = x;
    this.y = y;
    this.radius = r; 
  }  
  public boolean overlap(Circle c) {
    // find out the distance between centers
    // if that distance is smaller than the c.radius + this.radius: true    
  }
  // find a way here to test it 
  public static void main(String[] args) {
    Circle a; 
    a = new Circle(2, 3, 4); 
    int a = 3; 
    Scanner maggie; 
    maggie = new Scanner(System.in); 
    int[] b = new int[10]; 
    // int[10] b; not correct syntax 
  } 
}


2. Rectangles


3. Point

public class Point {
  public int x, y; 
  public Point(int x, int y) {
    this.x = x;
    this.y = y; 
  }
  public double distanceTo(Point other) {
    double dx = this.x - other.x; 
    double dy = this.y - other.y;
    return Math.sqrt( dx * dx + dy * dy );  
  }
  public static void main(String[] args) {
    Point romney, obama; 
    romney = new Point(45, 2); 
    obama = new Point(30, 16); 
    double d = romney.distanceTo(obama); 
    System.out.println( d ); 
    System.out.println( obama.distanceTo(romney) ); 
    System.out.println( obama.distanceTo(obama) ); 
   
  }
}


4. Redefine the model Circles 


public class Circle {
  public Point center;
  public int radius;
  public Circle(Point c, int r) {
    this.center = c;
    this.radius = r;    
  }
  public double area() {
    return Math.PI * Math.pow(this.radius, 2);
  }
  public boolean overlap(Circle other) {
    double d = this.center.distanceTo(other.center); 
    if (d <= this.radius + other.radius) {
      return true; 
    } else {
      return false; 
    } 
  }
  public static void main(String[] args) {
    Circle a = new Circle(new Point(2, 3), 4); 
    Circle b = ...;
    System.out.println( a.overlap(b) );
  } 
}

5. I have Point. Can I define a Line? What is Line? 

public class Line {
  public Point a, b; // instance variables, they define the structure
  public Line(Point one, Point another) {
    this.a = one; 
    this.b = another; 
  } // this is the constructor: no type, same name as the class
  public double length() {
    return this.a.distanceTo(this.b);
  }
}


Modeling has these stages:

  (a) write the data that defines the instance 
  (b) define a way to initialize the structure: define constructor 
  (c) define the desired behavior: instance methods 

6. Triangle


  (a) three points define a triangle 
  (b) write a constructor for that 
  (c) public double area() {
        double a = the first side's length, 
               b = the second side's length,   
               c = the third side's length.
        double s = (a + b + c) / 2; 
        return Math.sqrt( s * (s - a) * (s - b) * (s - c) ); 
      }