CS 145 Lecture 4 – Math Class
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…
- Declare an
int
. Roll for the identifier. - Print an expression. Roll for the expression.
- Assign an expression to a variable. Roll for the identifier and the expression.
- Declare and assign a
Scanner
namedin
. - Declare and assign an
int
. Roll for the identifier and the expression. - Same as 5.
If when rolling for an identifier, you roll a…
- Use
a
. - Use
b
. - Use
c
. - Use
d
. - Use
e
. - Use
f
.
If when rolling for an expression, you roll a…
- Use
+
. Roll for two operands to add. - Use
-
. Roll for two operands to subtract. - Use
*
. Roll for two operands to multiply. - Use
/
. Roll for two operands to divide. - Use
%
. Roll for two operands to divide and compute the remainder. - Use
-
. Roll for an operand to negate.
If when rolling for an operand, you roll a…
- Use
in.nextInt()
. - Roll for an expression.
- Roll for an expression.
- Use 4.
- Use 5.
- 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:
- 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. - 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. - Your kid sibling 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 you guess 10. He says your 7 away. You try again until you get the right answer. Let’s write a
main
that plays one exchange of this game. - 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?
Here’s your TODO list of things to complete before next class:
- Keep up with Piazza. I posted a clarification about
LemonAid
on homework 1. Ignore it at your peril. - Finish lab if you didn’t do so yesterday. Do not wait until 30 minutes before the next lab to do so.
- Look for a grade report—without much on it—in your email sometime tomorrow. I don’t use D2L.
- Homework 1 is due before September 23. When I say before September 23, I mean I will run the SpecChecker sometime on September 23. If I wake up at 12:03 AM that day, I reserve the right to run the SpecChecker.
See you next class!
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); } }