teaching machines

CS 145 Lecture 4 – Types and the Math Class


  • what ?s
  • program this
  • expression evaluation
  • the numeric primitives
  • casting
  • methods from the Math class


  • Read chapter 1, section 1.4, and chapter 2 through section 2.2.
  • Create an account on Practice-It!, a Java quizzing website built by the authors of your textbook. Solve a few problems from chapters 1 or 2. Word on the street is that folks who have completed a lot of these problems at certain unspecified times will receive extra credit participation.
  • Write down 2-3 questions or observations and the names of the problems you solved on a 1/4 sheet.


We start today with a little segment I call Program This. I throw a problem at you and you spend a few moments solving it with your neighbor. Here goes:

You get a cryptic text saying, “Something big is going to happen in 1357 hours.” This kind of thing happens a lot, and you like to add a calendar entry on your phone for these targeted days reminding you to keep your eyes open. You decide to write a program to help turn the given number of hours into the time of this big something. How many full days away is it? At what time of the day will it occur?

Focus on the contents of your main method. Use ideas we’ve talked about so far in the class.

Now that we’ve introduced the idea of variables and types, let’s practice evaluating some expressions operation by operation. The Practice-It! website is a great source for challenges. Here’s an expression from the expressions2 problem, solved one operation at a time:

4.0 / 2 * 9 / 2
2.0 * 9 / 2
18.0 / 2

Note how the types propagate through the intermediate results. Here’s another one:

double x = 17 % 10 / 4;
double x = 7 / 4;
double x = 1;

Note that even though we are storing the expression in a double, the right-hand side gets evaluated first using integer division. We lose the fraction.

This seems like a good time to discuss the spectrum of numeric types: byte, short, int, long, float, and double. We’ll looking at changing between types, both implicitly and explicitly (through casting). We’ll also examine why so many different types exist by timing some mathematical operations.

Finally, we saw last time that we could give names to our data to make our expressions more meaningful. We’ll see today that we can give meaningful names to not only data, but entire operations. We may have a routine or process that we’d like to use again and again, liking finding the minimum of two numbers or determining the hypotenuse of a right triangle. Instead of having to write out the steps of these routines every time we need them, we’ll package them up in a big black box and invoke them succinctly through their reusable name. We’ll make use of some black boxes that are already available to us in the Math class by solving these problems:

  1. An online charitable organization is selling something name-your-price. If you pay above the average, you get an extra premium—some socks with pockets. You need to program your personal finance robot to always pay the next whole dollar amount above the average. How can you help it calculate how much to pay?
  2. You are given n pictures, all the same size. How can you lay them out so that there are as many rows as there are columns?
  3. Your kid brother has invented a guess-a-number game. You say the number, and he tells you how far away you are from the right number. For instance, suppose the number is 9. You guess 11. He says “2 away!” You guess 8, he says “1 away!”. You guess 7, he says “2 away!” Play one guess-response exchange.
  4. On day 0, 1 person gets a terrible disease. On day 1, this person infects 10 others. On day 2, these 10 people each infect 10 others, for a total of 100 new infections. On day three, the 100 each infect 10 others, for a total of 1000 new infections. How many days pass before half the US population (313.9 million) is infected?
  5. A knight is on a toroidal chessboard. That is, if it moves off the edge of the board, it comes back in on the opposite side. Suppose the knight is at position (c, r). What is its new position if it moves three up and one to the right?



package lecture0911;

public class MathProblems {
  public static void main(String[] args) {
    double average = 5.27;
    double nextWhole = Math.ceil(average);


Bugs were attacking!
I called Math.POW and now…
They’re ex-opponents


Leave a Reply

Your email address will not be published. Required fields are marked *