Howdy. 

Warmup: design Point. A Point is a pair of two coordinates. Points are 
supposed to be able to determine how far they are to any other Point, as 
long as this other point is specified. 

Then design Circle. A Circle is a location with a radius. Circles are
supposed to be able to determine if they overlap. 

Design Fraction. A Fraction is a pair of two numbers, called numerator
and denominator, of which the second one is not supposed to be zero. Like
BigDecimal objects Fractions are supposed to be able to add, subtract, etc.
(multiply, divide) with each other. 

public class Point {
  private int x, y; 
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public static Point origin = new Point(); 
  public Point() {
    this(0, 0); 
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y;
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  public static void main(String[] args) {
    Point a, b;
    a = new Point(3, 4); 
    b = Point.origin; 
    System.out.println(a.distanceTo(b)); // 5.0 
    System.out.println(b.distanceTo(new Point())); // 0.0       
  }
}

Let's now do Fraction. 

public class Fraction {
  private int num, den;
  public Fraction(int num, int den) {
    this.num = num; 
    this.den = den; 
  }
  public String toString() {
    return this.num + "/" + this.den; 
  }
  public Fraction add(Fraction other) {
    int n = this.num * other.den + this.den * other.num;
    int d = this.den * other.den;
    Fraction answer = new Fraction(n, d); 
    return answer; 
  }
  public static void main(String[] args) {
    Fraction a = new Fraction(1, 2); 
    Fraction b = new Fraction(2, 3);
    Fraction c = a.add(a); 
    Fraction d = a.add(b); 
    System.out.println( c ); // 4/4
    System.out.println( d ); // 7/6
    
  }
}

What if a Fraction is created with a denominator of 0 (zero). 

public class Fraction {
  private int num, den;
  public Fraction(int num, int den) throws Exception {
    this.num = num; 
    this.den = den; 
    if (den == 0) {
      throw new Exception(); 
    } else {
      // here we should simplify the fraction; how? 
    }
  }
  public String toString() {
    return this.num + "/" + this.den; 
  }
  public Fraction add(Fraction other) throws Exception  {
    int n = this.num * other.den + this.den * other.num;
    int d = this.den * other.den;
    Fraction answer = new Fraction(n, d); 
    return answer; 
  }
  public static void main(String[] args) throws Exception {
    Fraction a = new Fraction(1, 2); 
    Fraction b = new Fraction(2, 3);
    Fraction c = a.add(a); 
    Fraction d = a.add(b); 
    System.out.println( c ); // 4/4
    try { 
      Fraction e = new Fraction(3, 0); 
    } catch (Exception e) {
      System.out.println(e + ": 3/0 avoided."); 
    }
    System.out.println( d ); // 7/6
    
  }
}

For Wednesday:

  (a) define a static method gcd

  (b) use it in the constructor to simplify the fraction 

See you on Wed.



--