# teaching machines

## CS 1: Lecture 5 – Math Methods

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:

• Lately I’ve gone into some shops and they’ve asked me if I wanted to donate some money to a cause by raising my bill up to the nearest whole dollar. Let’s write a main that asks for an expense amount and prints out how much goes to charity.
• In computer graphics, the lightedness of a surface depends on the angle between the surface’s normal (a vector pointing out from the surface at a 90-degree angle) and a vector to the light source. When that angle is 0, how lit is the surface? What happens as the angle grows? Let’s plot angle-vs-lightedness and write a main to calculate it. This model is called Lambert’s Law.
• You are trying to get from point A to point B. You could walk across the grass and get there directly. Or you take the sidewalk, which veers off in a different direction but then turns at a 90-degree angle to reach point B. How much distance do you save by walking directly?
• On day 0, patient zero acquires a disease, infects two others, and dies. On day 1, the two infected folks each infect two others, and die. On day 2… How many people are sick after 10 days? How many days until the United States is wiped out?
• How tall is a tree? (Could it reach your house if it fell?)
• The chessboard distance between points A and B is the number of moves that a king must make to go from one to the other. Kings can move in any direction, even diagonally, but only one square at a time. What’s an algorithm that computes the chessboard distance between two points?

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

• Revisit the code that we’ve written together in class, which I append to the bottom of each lecture post.
• Do not make multiple forks of the template repository. You only need one for the entire semester. Each homework will go into a single package of your one repository. If you do make a new fork for some reason, please email me with the new address. I will continue to grade your old one unless you tell me otherwise.
• Create an account on https://practiceit.cs.washington.edu. Solve as many of the first ten BJP4 Chapter 2 Self-Check problems as you can. Write down your answers to just problem 2.4 (entitled expressions2) on a quarter sheet.

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);

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);
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();

lightedness = Math.max(lightedness, 0);
System.out.println(lightedness);
}
}


#### DoubleFloat.java

package lecture0915;

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

public class DoubleFloat {
public static void main(String[] args) {

Random generator = new Random();
double sum = 0;
for (int i = 0; i < 400000000; ++i) {
sum = sum + generator.nextDouble();
}

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 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);
}
}