CS1: Lecture 4 – 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 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:
- 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?
Here’s your TODO list of things to complete before next class:
- CS 148, your next lab is posted. You are invited to complete it early and get it checked off write at the beginning of lab on Thursday.
- CS 148, if you didn’t get your work checked off for lab 1 last week, we will do so in the first 10 minutes of lab 2.
- Homework 1 is officially assigned. After today we’ve touched upon all the ideas you’ll need to complete it. Get it submitted to GitLab before the end of September 23. Bear in mind the following:
- Make time goals rather than milestone goals. Commit to working on the homework an hour each day. Don’t say I’ll get this exercise done, because you don’t know how long it will take. Frequent and early commits will help you maximize the Early and Often Blugolds that are part of your grade.
- Talk to each other about the homework, but do not copy, transcribe, or even look at another person’s code. If the ideas don’t get into your brain so that you can write the code yourself, you are wasting your time taking this course. Take it when you’ve got the time and energy to properly learn the material.
- Make sure you check that all of your code is there by visiting gitlab.com. If you can’t see it there, then neither can I. Not committing and pushing properly is just like not turning in an essay.
- Read chapter 2 through section 2.2 in your book.
- Create an account on Practice-It!. 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!
P.S. It’s time for a haiku!
“That’s some nicehypot
“
Math said, “But after thisround
“
“Let’s go get somePI
“
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);
}
}