# teaching machines

## CS 145 Lecture 5 – Math Cont’d

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

Dear students,

Here’s something that happens. Your professor, who has been writing code for a long time, walks through many coding examples with you in class. He carefully arranges everything to present a coherent and compelling story, intentionally leading you into problems but swooping in with some new idea to save the day. You feel powerful, but then you get home and try to write some program of your own. But the only problems you’ve seen before have been your instructor’s cage-tamed problems—not your own. What happens when you breed your vicious strains of problems? Nobody knows, because those people have disappeared.

We can’t let that happen. We need to breed problems right here and now, in front of each other. So, today we start with an exercise:

Your kid brother comes up with what he thinks is an awesome Guess-a-Number game. He thinks of a number. You guess. He tells you how far away you are from the right answer. For example, suppose he picks the number 3, but you guess 10. He says you’re 7 away. In the real fake game, you guess again until you get it right. But let’s simulate just one guess in a fake fake version of this game using Java.

Simulate one guess of this game. Write a main method on paper. Let the computer be your brother, who picks the target number. It gets the human’s guess and reports how far the human is from the target.

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

• 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’re building a device called the CircleSpitter, which randomly decorates walls with circles. You first generate two 2D coordinate pairs: an origin and a point on the perimeter. The painting hardware expects as input the equation of the circle. How can we generate these coordinates and the equation?
• At an old abandoned factory in what your family calls the No-zone, you find a bunch of old barrels. You think you might be able to use them to catch rainwater, the only stuff that’s safe to drink these days. But how many will you need to store an adequate supply for a whole year? Write a program to help you and any fellow survivor solve this problem.

See you next class!

Sincerely,

P.S. It’s Haiku Friday!

We’re wrong when we code
Wrong as a sculptor is wrong
At a ball of clay

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

#### Orangey.java

package lecture0916;

import java.util.Scanner;

public class Orangey {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Take a guess at what number I'm thinking of: ");
int guess = in.nextInt();
int lilbronum = (int) (Math.random() * 10);
int distance = Math.abs(guess - lilbronum);
System.out.println("You are " + distance + " away!");
}
}


#### Lambert.java

package lecture0916;

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

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


#### CircleSpitter.java

package lecture0916;

public class CircleSpitter {
public static void main(String[] args) {
double originX = Math.random();
double originY = Math.random();
double edgeX = Math.random();
double edgeY = Math.random();

double radius = Math.hypot(edgeX - originX, edgeY - originY);

System.out.printf("(x - %f) ^ 2 + (y - %f) ^ 2 = %f ^ 2", originX, originY, radius);
}
}