teaching machines

CS 1: Lecture 5 – Math Methods

September 15, 2017 by . Filed under cs1, cs145, cs148, fall 2017, lectures.

Dear students,

Today we introduce the Math class. This class has a bunch of pre-written recipes for computing various mathematical operations. We can see what it all provides by looking at its documentation. I usually find this documentation by searching java ClassName in my favorite search engine.

Let’s explore the methods that the Math class provides by solving some exercises that call upon it for help:

Here’s your TODO list of things to complete before next class:

See you next class!

Sincerely,

P.S.

It’s time for a haiku!

It’s ‘round toDegrees
cos there’s not asin of spring
I’m really powtan

P.P.S.

Here’s the code we wrote together in class…

Charity.java

package lecture0915;

import java.util.Scanner;

public class Charity {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.print("What is your bill? ");
    double bill = in.nextDouble();
    double roundedUpBill = Math.ceil(bill);
    double charity = roundedUpBill - bill;
    System.out.printf("You gave $%.2f today.%n", charity);
    System.out.println("You're a foobag.");
  }
}

Shortcut.java

package lecture0915;

import java.util.Scanner;

public class Shortcut {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Gimme your two distances: ");
    double d1 = in.nextDouble();
    double d2 = in.nextDouble();
    
//    Math.sqrt(d1 * d1 + d2 * d2)
    double hypotenuse = Math.hypot(d1, d2);
    System.out.println("You saved " + (d1 + d2 - hypotenuse));
  }
}

Lightedness.java

package lecture0915;

import java.util.Scanner;

public class Lightedness {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter angle: ");
    double degrees = in.nextDouble();
    
    double radians = Math.toRadians(degrees);
    double lightedness = Math.cos(radians);
    lightedness = Math.max(lightedness, 0);
    System.out.println(lightedness);
  }
}

DoubleFloat.java

package lecture0915;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.Random;

public class DoubleFloat {
  public static void main(String[] args) {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long start = bean.getCurrentThreadCpuTime();

    Random generator = new Random();
    double sum = 0;
    for (int i = 0; i < 400000000; ++i) {
      sum = sum + generator.nextDouble();
    }
    
    long end = bean.getCurrentThreadCpuTime();
    double elapsed = (end - start) / 1e9;
    System.out.println(elapsed);
  }
}

TreeHeight.java

package lecture0915;

import java.util.Scanner;

public class TreeHeight {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Angle and distance? ");
    double degrees = in.nextDouble();
    double distance = in.nextDouble();
    double radians = Math.toRadians(degrees);
    double height = distance * Math.tan(radians);
    System.out.println(height);
  }
}

Epidemic.java

package lecture0915;

public class Epidemic {
  public static void main(String[] args) {
    int nDays = 29;
    double nSickies = Math.pow(2, nDays);
    System.out.printf("%.0f", nSickies);
  }
}

ChessboardDistance.java

package lecture0915;

public class ChessboardDistance {
  public static void main(String[] args) {
    int ax = 0;
    int ay = 0;
    int bx = 2;
    int by = 4;
    
    int dx = Math.abs(ax - bx);
    int dy = Math.abs(ay - by);
    int distance = Math.max(dx, dy);
    System.out.println(distance);
  }
}