teaching machines

CS 145 Lecture 7 – Careful coding

February 15, 2012 by . Filed under cs145, lectures, spring 2012.

Agenda

Code

Utilities.java

package preexam1;

public class Utilities {

  // i = get area of inside
  // o = get area of outside
  // area = o - i
  
  public static double getCircleArea(double radius) {
    return Math.PI * radius * radius;
  }

  /**
   * Gets the area of a ring defined by its inner and outer radii.
   * @param innieRadius The inner circle's radius
   * @param outieRadius The outer circle's radius
   * @return The area of the ring
   */
  public static double getRingArea(double innieRadius, double outieRadius) {
    return getCircleArea(outieRadius) - getCircleArea(innieRadius);
  }
  
  public static boolean equalEnough(double a, double b) {
    double difference = a - b;
    return Math.abs(difference) < 0.000001;
  }
  
  public static void main(String[] args) {
    System.out.println(0 == getCircleArea(0.0));
    System.out.println(Math.PI == getCircleArea(1.0));
//    System.out.println(113.097335529233 == getCircleArea(6.0));
    System.out.println(equalEnough(113.097335529233, getCircleArea(6.0)));
    
    System.out.println(0 == getRingArea(5.0, 5.0));
    System.out.println(equalEnough(34.5575191894877, getRingArea(5.0, 6.0)));
  }
}

Haiku

Don’t write Frankencode.
Lifeless code should not take life.
Test. Don’t trust yourself.