Welcome to lab six!

Levi Seth Jiebo Jerry Alex S Jared Vanessa Jiawei Mike C 
Arif D He He Jiahao Kaqin Yinan Arthi Joe B Andrew David 
Sunghyun Zhiwei Xing W Alex Q Chenrui He Rajeev Matt L 
Rerajitha M Sid C Haoyang Andrew R Brandon P Kevin S

public class Rectangle {
  private Point topLeft; 
  int width, height; 
  public Rectangle(Point a, int w, int h) {
    this.topLeft = a; 
    this.width = w;
    this.height = h; 
  }
  public boolean overlaps(Rectangle another) {
    return false;  
  }
}

public class Rectangle {
  
  public static boolean overlaps(Rectangle a, Rectangle b) {
    // cheating 
    return a.overlaps(b); // sorry, that was not the main point here 
  }
  
  private Point topLeft;
  // y increases to the North and x to the East 

  int width, height; 
  // this is how far East and South the Rectangle extends

  public Rectangle(Point a, int w, int h) {
    this.topLeft = a; 
    this.width = w;
    this.height = h; 
  }

  public boolean overlaps(Rectangle another) {
    return ! ( another.topLeft.getY() - another.height > this.topLeft.getY() ||
              // this condition says the another is entirely to our North 
               another.topLeft.getY() < this.topLeft.getY() - this.height || 
              // completely to the South 
              another.topLeft.getX() > this.topLeft.getX() + this.width || 
              // entirely to the East of us
              another.topLeft.getX() + another.width < this.topLeft.getX()
              // entirely to our West
             );     
  }
  
  public static void main(String[] args) {
    Rectangle a = new Rectangle(new Point(10, 10), 10, 10); 
    // bottom right corner of this rectangle is (20, 0)
    Rectangle b = new Rectangle(new Point(5, 5), 3, 3); 
    System.out.println( b.overlaps(a) ); // true 
    System.out.println( Rectangle.overlaps(a, b) ); // true 
    
  }
}

public class Point {
  private int x, y; 
  public Point(int x, int y) {
    this.x = x;
    this.y = y; 
  }
  public int getX() { return this.x; } // accessor (getter) 
  public int getY() { return this.y; } // accessor (getter) 
  public double distanceTo(Point other) {
    int dx = this.x - other.x; 
    int dy = this.y - other.y;
    return Math.sqrt( Math.pow(dx, 2) + Math.pow(dy, 2) ); 
  }
  public static void main(String[] args) {
    Point a = new Point(0, 0);
    Point b = new Point(1, 1); 
    System.out.println( a.distanceTo(b) ); // 1.4142135623... 
  }
}

Now we download Bluej

http://bluej.org/download/files/bluej-317.jar

Kevin also asked about this:

Date: Wed, 25 Jan 2012 13:58:15 -0500 (EST)
From: Adrian German (dgerman@cs.indiana.edu)
To: C212 Spring 2012 Distribution List (dgerman@indiana.edu)
Subject: example from class this morning


Here's the simplest example I can come up with that shows that values 
of Integer type have a distinctly different nature than ints. Please compile
and run this program:

class Nine {
   public static void main(String[] args) {
     Integer a = new Integer(2);
     Integer b = new Integer(2);
     System.out.println( "If we calculate " + a + " - " + b + " we obtain: " + (a - b) );
     System.out.println( "If we evaluate " + a + " == " + b + " we obtain: " + (a == b));
  }
}

The program reports that 2 - 2 evaluates to 0 (zero) yet 2 == 2 evaluates to false. I hope 
you find this a bit unsettling. In the morning I could not at first come up with anything as 
surprising since Integer objects are in fact immutable so we had to wait to introduce arrays 
and use them to show the effect of sharing. I will include this example in the notes and will 
also discuss it in the pm class that starts in 25 minutes.

Both java.lang.Integer and int are Java types. While int is a primitive type, Integer is a 
reference (user-defined) type. Integer values are in fact objects, instances that don't fit 
into a variable like the values of primitive types do. Instead a pointer is kept in the 
variable for them.

The java.lang.Integer provides a way to wrap values of primitive type (e.g., int) in objects. 
Yet much effort has been placed by the developers of Java into making Integer objects's behavior 
(via operator overloading) every bit as similar as those of their primitive type counterparts. For 
this reason I think it's going to be impossible to come up with anything simpler than the code that
I am sending to you right now (class Nine, above).

In other words if you try this instead:

class Ten {
   public static void main(String[] args) {
     Integer a = 2;
     Integer b = 2;
     System.out.println( "If we calculate " + a + " - " + b + " we obtain: " + (a - b) );
     System.out.println( "If we evaluate " + a + " == " + b + " we obtain: " + (a == b));
  }
}

... you wouldn't be able to tell the difference between int and Integer.

As always, I welcome any comments, questions, concerns, better examples or any other challenges 
that you might want to communicate to me. All the best,

Sincerely,
Adrian German

--