teaching machines

CS1: Lecture 4 – Math Methods

September 11, 2019 by . Filed under cs1, fall 2019, 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 the documentation for a class by searching java ClassName in my favorite search engine.

The Math class is full of what are called static methods. Static methods are chunks of code that we can run in the following way:

ClassName.methodName(parameter1, parameter2, ...)

All of these methods yield or return a value that is substituted into their place in our code. For example, if we say:

double a = 5;
double b = 6;
double c = 1;
double x1 = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);

The computer will whittle the expression for x1 down one subexpression at a time:

double x1 = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
double x1 = (-b + Math.sqrt(36 - 4 * a * c)) / (2 * a);
double x1 = (-b + Math.sqrt(36 - 16 * c)) / (2 * a);
double x1 = (-b + Math.sqrt(36 - 16)) / (2 * a);
double x1 = (-b + Math.sqrt(20)) / (2 * a);
...

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!

“That’s some nice hypot
Math said, “But after this round
“Let’s go get some PI

P.P.S. Here’s the code we wrote together in class…

Donation.java

package lecture0911.cs145;

import java.util.Scanner;

public class Donation {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("How much? ");
    double amount = in.nextDouble();
    double roundedUpAmount = Math.ceil(amount);
    double donation = roundedUpAmount - amount;
    System.out.printf("Donation: %.2f %s %d", donation, "Santa", 13);
  }
}

WalkSavings.java

package lecture0911.cs145;

import java.util.Scanner;

public class WalkSavings {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.println("a: ");
    double a = in.nextDouble();

    System.out.println("b: ");
    double b = in.nextDouble();

//    double hypotenuse = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    double hypotenuse = Math.hypot(a, b);
    double savings = a + b - hypotenuse;
    System.out.println(savings);
  }
}

Donation.java

package lecture0911.cs148;

import java.util.Scanner;

public class Donation {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("How much did you spend? ");
    double amount = in.nextDouble();
    double roundedUpAmount = Math.ceil(amount);
    double charity = roundedUpAmount - amount;
    System.out.printf("Donation: $%.2f %s %d", charity, "Master Chief", 13);
  }
}

Lit.java

package lecture0911.cs148;

import java.util.Scanner;

public class Lit {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Degrees: ");
    double degrees = in.nextDouble();
    double radians = Math.toRadians(degrees);
    double litness = Math.max(Math.cos(radians), 0);
    System.out.printf("Litness: %.2f%%%n", litness * 100);
  }
}