Staging · for review, not public

Week 2 · Tuesday, Jan 12 · 2.2

Casting and limits

A cast changes a value's type: (int) cuts off the decimal part and (double) turns an int into a double, and every number type has limits on how big and how exact it can be.

Minutes for each part of a 90-minute class.

Today we will

  • A cast puts a type in parentheses before a value: (double) 7 is 7.0, and (int) 9.87 is 9.
  • (int) truncates: it cuts off the decimal part. It does not round.
  • A cast applies to the value right after it, so (double) 7 / 2 is 3.5 but (double) (7 / 2) is 3.0.
  • To round a double that is not negative, add 0.5 and then cast: (int) (x + 0.5).
  • An int has a smallest and largest value, Integer.MIN_VALUE and Integer.MAX_VALUE. Going past them is integer overflow.
  • A double is stored in a limited amount of memory, so some results are slightly off. That is round-off error.

Terms

casttruncateroundInteger.MAX_VALUEInteger.MIN_VALUEinteger overflowround-off error

AP topics 1.5 · Learning objectives 1.5.A, 1.5.B, 1.5.C · Skills 2.A, 3.A, 4.A

0–10 min

Do Now

Warm up the moment you sit down. One code when you finish.

Open the Do Now
10–35 min

Demonstration

Chromebooks open. Follow along on the Follow along jobs in We Do / You Do while Mr. Muggivan works the examples.

10–75 min

We Do / You Do

Follow along, then Part A (with help), Part B (on your own) and stretch. One code for the whole block.

Open We Do / You Do
75–90 min

Exit Ticket

A few exam-style items to show what you learned today. One code.

Opens only in the last 15 minutes of class on this day.

Missed class? The Do Now and We Do / You Do stay open. Do them any time and submit your codes.