Lab 12: Simple GUIs. 

Prepare for the exam on Wednesday. 

Let's design a Circle, a Triangle, a Rectangle.

public class Circle {

}

public class Triangle {

}

import java.util.Arrays; 

public class Example {
  public static void main(String[] args) {
    Circle c; 
    Triangle t; 
    c = new Circle(); 
    t = new Triangle(); 
    Object[] a = new Object[2];
    a[0] = c; 
    a[1] = t; 
    System.out.println(Arrays.toString(a)); 
  }
}

(a) If a class does not extend another class explicitly it extends Object. 

(b) If a class does not list a constructor Java gives you a default no-arg constructor. 

(c) 

Ben Nariman Mary Murun Aarani Daohui JeVante
Dustin Yuying Sai Qi Chun xixiong@iu.edu Zack 
Emma Sunghyun Grant Chase Jiongran Michael 
Stephen K Peter F Peter W Young Hwan LaBrian Namit

import java.util.Arrays; 

public class Example {
  public static void main(String[] args) {
    Circle c; 
    Triangle t; 
    c = new Circle(); 
    t = new Triangle(); 
    Shape[] a = new Shape[2];
    a[0] = c; 
    a[1] = t; 
    System.out.println(Arrays.toString(a)); 
    System.out.println( a[0].area() ); 
  }
}

public class Shape {
  public double area() { return Math.random(); }  
}

public class Circle extends Shape {

}

public class Triangle extends Shape { 
   
}

--

import java.util.Arrays; 

public class Example {
  public static void main(String[] args) {
    Circle c; 
    Triangle t; 
    c = new Circle(2); 
    t = new Triangle(new Point(0, 0), new Point(0, 3), new Point(4, 0)); 
    Shape[] a = new Shape[2];
    a[0] = c; 
    a[1] = t; 
    System.out.println(Arrays.toString(a)); 
    for (Shape s : a) 
      System.out.println( s.area() ); 
  }
}

public class Shape {
  public double area() { return Math.random(); }  
}

public class Circle extends Shape {
  int radius; 
  public Circle(int radius) {
    this.radius = radius;  
  }
  public double area() {
    return Math.PI * this.radius * this.radius;  
  }
}

public class Triangle extends Shape { 
  Point a, b, c; 
  public Triangle(Point a, Point b, Point c) {
    this.a = a; 
    this.b = b; 
    this.c = c; 
  }
  public double area() {
    double a = (new Line(this.b, this.c)).length(); 
    double b = (new Line(this.a, this.c)).length(); 
    double c = (new Line(this.a, this.b)).length(); 
    double s = (a + b + c) / 2; 
    return Math.sqrt( s * (s - a) * (s - b) * (s - c) ); 
  }
}

public class Circle extends Shape {
  int radius; 
  public Circle(int radius) {
    this.radius = radius;  
  }
  public double area() {
    return Math.PI * this.radius * this.radius;  
  }
}

public class Line {
  Point a, b; 
  public Line(Point a, Point b) {
    this.a = a;
    this.b = b;
  }
  public double length() {
    return this.a.distanceBetween(b);  
  }
}

public class Point {
  private int x, y; 
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public double distanceBetween(Point other) {
    int dx = this.x - other.x; 
    int dy = this.y - other.y; 
    return Math.sqrt( dx * dx + dy * dy ); 
  }
}

import java.util.Arrays; 

public class Example {
  public static void main(String[] args) {
    Circle c; 
    Triangle t; 
    c = new Circle(2); 
    t = new Triangle(new Point(0, 0), new Point(0, 3), new Point(4, 0)); 
    Shape[] a = new Shape[2];
    a[0] = c; 
    a[1] = t; 
    System.out.println(Arrays.toString(a)); 
    for (Shape s : a) 
      System.out.println( s.area() ); 
  }
}

--

Note that at this stage we have 

(a) polymorphism 

(b) inheritance

(c) dynamic method lookup 

--

public abstract class Shape {
  public abstract double area();  
}

--

import java.util.Arrays; 

public class Example {
  public static void main(String[] args) {
    Circle c; 
    Triangle t; 
    c = new Circle(new Point(1, 1), 2); 
    t = new Triangle(new Point(0, 0), new Point(0, 3), new Point(4, 0)); 
    Shape[] a = new Shape[2];
    a[0] = c; 
    a[1] = t; 
    System.out.println(Arrays.toString(a)); 
    for (Shape s : a) 
      System.out.println( s.area() ); 
  }
}

public abstract class Shape {
  Point location;
  public Shape(Point p) {
    this.location = p; 
  }
  public abstract double area();  
}

public class Circle extends Shape {
  int radius; 
  public Circle(Point center, int radius) {
    super(center); 
    this.radius = radius;  
  }
  public double area() {
    return Math.PI * this.radius * this.radius;  
  }
}

public class Triangle extends Shape { 
  Point a, b, c; 
  public Triangle(Point a, Point b, Point c) {
    super(a);  
    this.a = a; 
    this.b = b; 
    this.c = c; 
  }
  public double area() {
    double a = (new Line(this.b, this.c)).length(); 
    double b = (new Line(this.a, this.c)).length(); 
    double c = (new Line(this.a, this.b)).length(); 
    double s = (a + b + c) / 2; 
    return Math.sqrt( s * (s - a) * (s - b) * (s - c) ); 
  }
}

public class Line {
  Point a, b; 
  public Line(Point a, Point b) {
    this.a = a;
    this.b = b;
  }
  public double length() {
    return this.a.distanceBetween(b);  
  }
}

public class Point {
  private int x, y; 
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public double distanceBetween(Point other) {
    int dx = this.x - other.x; 
    int dy = this.y - other.y; 
    return Math.sqrt( dx * dx + dy * dy ); 
  }
}

--