teaching machines

CS 145 Lecture 4 – Math Class

September 14, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

Yesterday we had our first lab. I was very impressed by your positive attitudes toward learning and the way you helped each other. Lab days are a bit grueling for me, as I am on my feet from 8 AM to 5 PM being relied on by others, but your energy and friendliness make it tolerable. Thank you!

Remember in high school math and science classes where your teaacher always told you to show your work. Well, we can’t help but do that when we write a program, because our program is the work. What we don’t see, however, are the intermediate results when we execute a program. Today we start with a little exercise to develop a mental model of our program as it executes. We’re going to play a little game called Oi, which is like I/O, but backwards. We use a six-sided die (a d6) to generate statements in Java according to these rules:

If the first roll is a…

  1. Declare an int. Roll for the identifier.
  2. Print an expression. Roll for the expression.
  3. Assign an expression to a variable. Roll for the identifier and the expression.
  4. Declare and assign a Scanner named in.
  5. Declare and assign an int. Roll for the identifier and the expression.
  6. Same as 5.

If when rolling for an identifier, you roll a…

  1. Use a.
  2. Use b.
  3. Use c.
  4. Use d.
  5. Use e.
  6. Use f.

If when rolling for an expression, you roll a…

  1. Use +. Roll for two operands to add.
  2. Use -. Roll for two operands to subtract.
  3. Use *. Roll for two operands to multiply.
  4. Use /. Roll for two operands to divide.
  5. Use %. Roll for two operands to divide and compute the remainder.
  6. Use -. Roll for an operand to negate.

If when rolling for an operand, you roll a…

  1. Use in.nextInt().
  2. Roll for an expression.
  3. Roll for an expression.
  4. Use 4.
  5. Use 5.
  6. Roll for an identifier.

Together we’ll generate some programs together and see what crazy things emerge.

We next 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 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.

Here’s the code we wrote together in class…

Charity.java

package lecture0914;

import java.util.Scanner;

public class Charity {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    System.out.print("Enter cost: ");
    double cost = in.nextDouble();
    
    double ceiling = Math.ceil(cost);
    double gift = ceiling - cost;
    
    System.out.println(gift);
  }
}

Lambert.java

package lecture0914;

import java.util.Scanner;

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