Howdy. 

There's an exam next Wed. 

Mon we have review. 

I posted the assignments for this week. 

1. Introduction 
2. Using Objects
3. Implementing Classes
4. Types, Values, Operators, Expressions, Assignments
5. Decisions (including nested decisions) 
6. Loops: while, do-while, for including nested loops 
7. Arrays and ArrayLists (simple data structures, including multi-dimensional arrays)

public class Fraction {
  private int den, num; 
  public Fraction(int num, int den) throws Exception {
    this.num = num; 
    this.den = den;
    if (this.den == 0) {
      throw new Exception(); 
    } else {
      // 21 / 27 start at 21 and go down one at a time, checking 
      int candidate = Math.min( Math.abs( this.num ), Math.abs( this.den ) ); 
      while ( this.num % candidate != 0 || this.den % candidate != 0 ) {
        candidate = candidate - 1; 
      } 
      this.num = this.num / candidate; 
      this.den /= candidate; // shortcut
    } 
  }
  public boolean equals(Fraction other) {
    return this.num * other.den == this.den * other.num;
  } 
  public String toString() {
    if (this.den == 1) 
      return this.num; 
    else 
      return this.num + "/" + this.den; 
  }
  public static void main(String[] args) throws Exception {
    Fraction a, b, c; 
    a = new Fraction(1, 2); 
    b = new Fraction(2, 4); 
    System.out.println(a.equals(b)); // true
    try {
      c = new Fraction(2, 0); 
    } catch (Exception e) {
      System.out.println( "Fractions can't have zero denominator. " + e ); 
    } 
    System.out.println( a.add(b) ); // 1
  }
}

If a and b are boolean variables can you prove this: 

   a && false    is the same as     false 

   a || false    is the same as     a

   a && true     is the same as     a

   a == false    is the same as     !a 

From left to right these are simplifications. 

DeMorgan's laws: 

   !(a && b)     is the same as     !a || !b

   !(a || b)     is the same as     !a && !b 

The proofs are withh truth tables. 

Let's assume we have class Point from last week. 

public class Circle {
  private Point center;
  private int radius; 
  public Circle(int x, int y, int r) {
    this.center = new Point(x, y);
    this.r = r;
  }
  public Circle(Point center, int radius) {
    this.center = center;
    this.radius = radius;
  }
  public boolean overlaps(Circle other) {
    return this.center.distanceTo(other.center) <= this.radius + other.radius;     
  }
}

I'll let you come up with a unit test for this. 

Rectangles overlap as in the exam from Summer of 2016. 

Let's write a program that reads numbers, one at a time. 

When you type "bye" the program ends. 

When it ends it prints the sum, the count and the average of all numebers entered. 

import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    int sum = 0, count = 0;
    String line;
    System.out.print("Type: "); 
    line = in.nextLine(); 
    while ( true ) {
      if (line.equalsIgnoreCase("bye"))
        break; 
      int num = Integer.parseInt(line); 
      sum += num; 
      count += 1; 
      System.out.println(count + " " + sum + " " + (double)sum / count); 
      System.out.print("Type: "); 
      line = in.nextLine();  
    }
    System.out.println("Thanks."); 
  }
}

Question: assume a program that asks the user for a number. 

Let's say the user agrees to type a positive number like 5.

Can you write code that would calculate the square root of 5. 

You can't use Math.sqrt(...) but you can use a loop. 

You can't use Math.pow(..., 0.5) either. 

--

     0-------------------------+----------------------6
     0------------+------------3
                 1.5-----+-----3  
                       2.25

And so on... 

So our code should model this process.

Here's our start:

import java.util.Scanner; 

public class SquareRoot {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    System.out.print("Type: ");     
    String line = in.nextLine(); 
    double num = Double.parseDouble(line); 
    // ... your code here 
  }
}

--