Overview
Two periods on one learning objective: write expressions that call the Math class’s methods and determine the value they produce (1.11.A). Day 1 covers why Math needs no import line and is called through its class name (1.11.A.1, 1.11.A.2), then abs, pow, and sqrt, including nested calls (1.11.A.2). Day 2 covers Math.random() and how arithmetic and casting turn its result into a random double or int in a stated range, with each endpoint inclusive or exclusive (1.11.A.2, 1.11.A.3).
The CED’s suggested skills are 2.C (write code that uses procedural abstractions) and 4.A (describe the behavior of code). Both days ask students to write expressions and to describe exactly which values an expression can produce.
Topic boundaryStay inside the topic boundary
- The five
Mathheaders in the CED are the whole inventory:abs(int),abs(double),pow,sqrt, andrandom. Do not show otherMathmethods. - Students do not write
ifstatements or loops (Unit 2). Random programs cannot check their own results, so students check them with stand-ins: a value the call could return, such as0.0or0.999, substituted in place of the call. “Stand-in” is the study guide’s name for the technique, not College Board vocabulary. - A label and a number still take a
printfollowed by aprintln; joining text and numbers is Topic 1.15. - Headers in the
Mathdocumentation start withpublic static.staticwas taught in Topic 1.10;publicis explained in a later topic and can be read past.
Teacher note
Every program below is in the topic’s examples/ folder. Files whose names end in .snippet.java hold only the statements that go inside main; paste them into the main of a scratch class to run them.
- Day 1, the
Mathclass:SquareTiles,MissingClassName, andLowercaseMath. - Day 1, absolute value:
GuessDistance,AbsTypes,AbsDiscarded, andAbsStored. - Day 1, powers and square roots:
PowOrder,PowToInt,PowFixed,Ladder,LadderWrong, andSqrtRound. - Day 1 guided practice:
MathPractice(the answers to “write the expression”). - Day 2 warm-up:
WarmUpNested. - Day 2,
Math.random:RandomSameandRandomArgument. - Day 2, random
doublevalues:TempRange,SignedRange, andPrecedenceWrong. - Day 2, random
intvalues:DieRoll,RangeCheck, andCastTooEarly. - Day 2 guided practice:
RangePractice(the answers, checked with stand-ins). - Printed only (exit ticket):
ExitMathandExitRangeCheck. The puzzle worksheet and its key are separate Word files; their code is printed on the page.
The Math Class Puzzles deck covers both days: slides 1–9 on Day 1 and slides 10–21 on Day 2. Its speaker notes hold the answers to every Pause and Try slide. Compiler messages are quoted from a recent JDK; the wording can vary slightly between Java versions. Before class, open SquareTiles and a scratch class in the IDE next to the deck.
Day 1 · Warm-up: Which tool for which job?
Show slide 2. Give pairs two minutes with the Java Quick Reference to pick one Math method for each of four jobs: how far apart 4 and 19 are (Math.abs(4 - 19)), the side of a square with area 64 (Math.sqrt(64)), 2 to the 10th power (Math.pow(2, 10)), and a coin flip (Math.random). Ask: “How would you find the side of the square using only +, -, *, /, and %?” Pairs can check guesses (8 * 8) but cannot compute the answer from 64; Java already has that algorithm written. Do not work out the coin flip yet; it is Day 2. Read the goals on slide 3.
Teacher note
Keep this to five minutes. The point is to feel the gap the Math class fills, not to invent a square-root algorithm.
If a student suggests trying numbers until one works, agree that it is a real algorithm, and point out that it is a
whole program for one small question.
Day 1 · Direct instruction: Java.lang and a class of only class methods
Start Part 1 (slide 4).
- No import line (1.11.A.1, slide 5). In Topic 1.4,
Scannerneededimport java.util.Scanner;.Mathneeds nothing. It is in the packagejava.lang, and every class injava.langis available by default: visible to every program without an import.Systemis injava.langtoo, which is whySystem.out.printlnhas never needed an import. - Only class methods (1.11.A.2). Every
Mathmethod hasstaticin its header, so every one is called asMath.methodName(arguments). Nothing has to be set up first. - The inventory (slide 5). Show the five headers from the CED, which are also the
Mathmethods on the Java Quick Reference students have during the AP exam:int abs(int x),double abs(double x),double pow(double base, double exponent),double sqrt(double x), anddouble random(). Today covers the first four; Day 2 coversrandom. - Run
SquareTiles. Point out the three things: no import line, the call starts withMath., and the returned12.0is stored insideand then used in4 * side, which prints48.0. - Predict:
MissingClassName. Ask, “Doessqrt(roomArea)compile, sinceMathis available by default?” It does not:cannot find symbol,symbol: method sqrt(double). Then showLowercaseMath:math.sqrt(...)givescannot find symbol,symbol: variable math, because Java is case-sensitive (Topic 1.1).
Misconception: Available by default means the class name can be left out
Students hear “available by default” and drop Math. from the call. “Available” means the class is visible without an import; the call still needs the class name, because Topic 1.10’s rule still holds: the class name is optional only inside the class that defines the method. Run MissingClassName and read location: class MissingClassName aloud: Java looked in the student’s class and found no sqrt.
Day 1 · Direct instruction: Absolute value and its two versions
- The idea. A guessing game’s target is 50. Guesses of 42 and 61 are off by
-8and11. For “how close,” the sign does not matter: the distances are 8 and 11. The absolute value is a number’s distance from zero, never negative. - Two headers, one name (1.11.A.2, slide 6).
absis overloaded (Topic 1.9):int abs(int x)anddouble abs(double x). Java picks the version whose parameter type matches the argument, so anintin gives anintout and adoublein gives adoubleout. Students predict the slide’s three lines before you reveal15,0.75,11; in the third, the argument9 - 20is worked out first, so the call is reallyabs(-11). - Run
GuessDistance. The argumentguess - targetis evaluated first;absreceives-8and returns8, then receives11and returns11. Ask: “WouldMath.abs(target - guess)change the answers?” No; the distance is the same either way. - Predict:
AbsTypes. Students write four lines of output:4,4.0,3.5,6. The first two differ only in the type of the argument. - Predict:
AbsDiscarded. Most students say15. It prints-15.absreceived a copy ofchange(call by value, Topic 1.9) and returned15, and nothing stored the return value.AbsStoredwriteschange = Math.abs(change);and prints15.
Misconception: Math.abs(x) makes x positive
The call reads like a command, so students expect the variable to change. It compiles with no warning, which makes the mistake hard to find. Every Math method returns a new value and leaves its arguments alone. Ask of every call: “Where does the returned value go?” If the answer is “nowhere,” the call had no effect the program can see.
Day 1 · Direct instruction: Powers, square roots, and nested calls
pow(1.11.A.2, slide 7).double pow(double base, double exponent)returnsbaseraised toexponent. The base is the number multiplied; the exponent is how many times. RunPowOrder:Math.pow(2, 3)is8.0andMath.pow(3, 2)is9.0. Arguments match parameters by position, so the first is always the base. Java has no power operator;2 ^ 3does not compute 8.- Always a
double(slides 7–8). Slide 7’sMath.pow(10, 3)prints1000.0andMath.sqrt(144)prints12.0, even though both answers are whole. Slide 8’sint big = Math.pow(10, 3);does not compile; a cast fixes it. ShowPowToIntfor the same error in the IDE:int area = Math.pow(side, 2);does not compile,possible lossy conversion from double to int, even though 49 is whole. The compiler goes by the return type in the header, not the value.PowFixedkeeps adouble(49.0) or casts on purpose (49). sqrt.double sqrt(double x)returns the nonnegative square root:Math.sqrt(20.25)is4.5, not-4.5. Treat a nonnegative argument as a precondition (Topic 1.8): a negative number has no real square root, andsqrtdoes not check for you. What Java returns for a negative argument is outside the course.- Nested calls: inside out. Show
Ladder: a ladder’s foot is 6 feet from a wall and its top is 8 feet up, so its length is the square root of (6² + 8²). Build the steps on the board:Math.pow(base, 2)is36.0,Math.pow(height, 2)is64.0, the sum is100.0,Math.sqrt(100.0)is10.0. An argument must have a value before the method that receives it can run. - Predict:
LadderWrong. It takes the square root of each piece and prints14.0. √36 + √64 = 6 + 8 = 14, but √(36 + 64) = 10. It is a logic error (Topic 1.1); the compiler accepts every call. - Casting a result. Slide 7’s last line,
(int) Math.pow(10, 3), prints1000.SqrtRoundprints6.6332495807108,6, and7for the square root of 44. The second line truncates with(int); the third uses the rounding pattern(int) (x + 0.5)from Topic 1.5. AMathresult is an ordinarydouble. - Pause and try (slide 9). Students write exactly what each line prints:
19.0(3 + 16.0),9.0(Math.sqrt(81)), and7(the cast truncates about 7.07). Ask who wrote19or9, and why.
Misconception: A whole-number answer can go in an int
Students store Math.pow(7, 2) or Math.sqrt(49) in an int because the answer “is” 49 or 7. The type of the result comes from the header, and pow and sqrt always return a double. Have students point to the return type in the header before they choose the variable’s type.
Misconception: The square root of a sum is the sum of the square roots
Students split Math.sqrt(a + b) into Math.sqrt(a) + Math.sqrt(b). Give them a test they can check by hand: legs 6 and 8 must give 10. LadderWrong gives 14, which is just 6 + 8: the ground distance plus the wall height, as if the ladder were bent at a right angle. One Math.sqrt( ... ) goes around the whole sum.
Day 1 · Guided practice: Write the expression
Write the four tasks below on the board. Pairs write one statement for each task on mini whiteboards, then hold them up:
| Task | Statement | Value |
|---|---|---|
| Print the distance between the scores 73 and 88. | System.out.println(Math.abs(73 - 88)); |
15 |
Store 1.5 to the power 2 in area. |
double area = Math.pow(1.5, 2); |
2.25 |
Store the side of a square whose area is 6.25 in side. |
double side = Math.sqrt(6.25); |
2.5 |
Store the diagonal of a 5-by-12 rectangle in diagonal. |
double diagonal = Math.sqrt(Math.pow(5, 2) + Math.pow(12, 2)); |
13.0 |
Run MathPractice to check the values. Expect int variables for tasks 2 to 4 (they do not compile; send students to the header), Math.abs(88 - 73) for task 1 (also correct), and Math.pow(2, 5) for task 4 (reversed arguments: that is 2⁵).
Teacher note
For task 4, have one pair write the inside-out steps on the board: 25.0, 144.0, 169.0, 13.0. A diagonal splits
the rectangle into two right triangles, the same shape as the ladder.
Day 1 · Independent practice: Independent practice
Hand out the Math Class Puzzles worksheet. Students start Part A, questions 1–2 (the value and type of pow, abs, and sqrt calls); circulate and check question 1: students who choose 15 assumed pow returns an int. Homework is questions 1–3, 7, and 8: finish Part A, the PowerUp code tiles, which statements compile, and the abs versus pow face-off. Questions 4–6, 9, and 10 use Math.random, so they wait for Day 2.
Day 1 · Closure: Closure
Point at Math.abs(-4), Math.abs(-4.0), Math.pow(2, 2), and Math.sqrt(16) on the board. For each, students hold up the value and the type: 4 (int), 4.0 (double), 4.0 (double), 4.0 (double). Only abs can return an int.
Day 2 · Warm-up: Predict three nested calls
Show WarmUpNested and have students write the exact output: 8.0, 27.0, 7. Line 1: 3 - 67 is -64, abs returns 64, sqrt returns 8.0. Line 2: Math.sqrt(9) is 3.0, and Math.pow(3.0, 3) is 27.0. Line 3: the cast applies to the value sqrt returned, about 7.07, and truncates it to 7.
Day 2 · Direct instruction: Math.random, endpoints, and stand-ins
Start Part 2 (slide 10).
- The header (1.11.A.2).
double random()has an empty parameter list, so the call isMath.random()with no arguments. Each call returns a new, unpredictabledouble. - The range (slide 11). The smallest possible result is
0.0, and0.0can occur; every result is less than1.0, and1.0cannot occur. Each end of a range is an endpoint; it is inclusive when the value can occur and exclusive when it cannot (1.11.A.3).Math.random()is 0.0 inclusive to 1.0 exclusive. - Stand-ins. You cannot predict the call’s value, so trace with values it could return.
0.0gives the low end; a value just under 1.0, such as0.999, shows the high end. - Run
RandomSame. It always prints0.0:rholds one value, and a value minus itself is0.0.Math.random() - Math.random()makes two calls with two different values, so it almost never gives0.0. When a program needs the same random value twice, store it once. - Predict:
RandomArgument.Math.random(6)does not compile:required: no arguments,found: int. There is no version ofrandomthat takes a range; the range comes from arithmetic after the call.
Misconception: Math.random(6) gives a number up to 6
Students who have used random functions elsewhere expect to pass the range. The header has an empty parameter list, and Topic 1.9 says arguments must match it in number and type. Show the compiler message, then promise the arithmetic that does the job.
Misconception: Math.random() can return 1.0
“Between 0 and 1” sounds like both ends are included. Point to slide 11’s number line (filled dot at 0.0, open dot at 1.0), say the range in words, “0.0 inclusive, 1.0 exclusive,” and ask for a value that is possible (0.0, 0.5, 0.999) and one that is not (1.0, -0.1). The exclusive 1.0 is the reason a die expression can never produce 7.
Day 2 · Direct instruction: Random int values in a range
- Stretch, cast, slide (1.11.A.3, slide 12). Build
(int) (Math.random() * 20) + 1one operation at a time and track the smallest and largest value:Math.random() * 20gives 0.0 up to just under 20.0;(int) (...)truncates it to one of 0 through 19 (Topic 1.5);+ 1gives 1 through 20. A die works the same way:(int) (Math.random() * 6) + 1gives 1 to 6. - Run
DieRoll. Stand-ins 0.0, 0.25, 0.5, and 0.875 giver * 6values of0.0,1.5,3.0, and5.25and rolls of1,2,4, and6. - Endpoints after the cast. The
doublerange had an exclusive 6.0; theintresults 0 through 5 have both endpoints inclusive. Describe anintrange by listing which whole numbers are possible. - The count (slide 13). For
lowtohigh, both inclusive, the multiplier is the count of whole numbers,high - low + 1:(int) (Math.random() * (high - low + 1)) + low. RunRangeCheckfor 10 to 20: with the stand-in0.999,* 11gives20, and the version with* 10gives19. The* 10version can never produce 20. Slide 13 does the same for an age from 50 through 60: count 11, so(int) (Math.random() * 11) + 50. - Exclusive ends for
intranges. “From 10 up to but not including 20” means 10 through 19: count 10. Decide which whole numbers are allowed, then count them. - Predict: the cast too early (slide 14).
(int) Math.random() * 20 + 1is1every time. The cast applies only toMath.random(), which always truncates to0.CastTooEarlyshows the same thing with a die. - Pause and try (slide 15). For each of three expressions, students give the type and the smallest and largest value. A,
(int) (Math.random() * 8) + 3, is anintfrom 3 through 10. B, with no cast, is adoublefrom 3.0 inclusive to 11.0 exclusive. C,(int) (Math.random() * 8 + 3), is also anintfrom 3 through 10: adding before the cast shifts the range first. B leads into the next section.
Misconception: The multiplier is the high end, or high minus low
Students write * 12 for 5 to 12, or * 7. Have them count the allowed values on their fingers for a small case (1 to 6 is six values) and then check the high end with the stand-in 0.999. RangeCheck shows the * 10 version stopping one short.
Misconception: The cast applies to the whole expression
(int) Math.random() * 6 + 1 looks like “cast the random roll.” A cast applies only to the value right after it, so it truncates Math.random() to 0 and the result is always 1. The program compiles and runs, which makes it a logic error. Parentheses around the product fix it: (int) (Math.random() * 6) + 1.
Day 2 · Direct instruction: Random double values in a range
- Leave out the cast (1.11.A.3, slide 16). For a random
double, multiply by the width of the range and add the low end, with no cast. The slide’s wait time,Math.random() * 2.5 + 1.0, runs from 1.0 inclusive up to 3.5 exclusive, the same kinds of endpoints asMath.random()itself. - Stretch, then slide. A weather simulation needs a temperature from 60.0 up to 75.0. Multiply to set the width:
Math.random() * 15gives 0.0 up to 15.0. Add to set the starting point:+ 60gives 60.0 up to 75.0. - Run
TempRange. Stand-ins 0.0, 0.5, and 0.75 give60.0,67.5, and71.25. Values just under 1.0 give values just under 75.0. The endpoints keep their kind: 60.0 inclusive, 75.0 exclusive. - The pattern.
Math.random() * width + low, where the width is high minus low. - A range below zero. A wind speed from −1.0 up to 1.0 has width 2 and low end −1:
Math.random() * 2 - 1.SignedRangegives-1.0,-0.5, and0.5for stand-ins 0.0, 0.25, and 0.75. - Predict:
PrecedenceWrong. With the stand-in0.5,r + 60 * 15prints900.5, because*happens before+(Topic 1.3).r * 15 + 60prints67.5.
Misconception: Writing the slide before the stretch
Students write Math.random() + 60 * 15, reading it left to right as “random, then 60 to 75.” Precedence computes 60 * 15 first, giving 900.0 up to 901.0. Teach the one-line check: substitute the stand-in 0.0, and the result must be the low end of the range.
Day 2 · Guided practice: Math in a program, then write the range expression
Start Part 3 (slide 17). Trace slide 18’s Square program with the input 2.5: Math.pow(side, 2) is 6.25, and Math.sqrt(area) is 2.5, back to the side. Point out the import for Scanner (java.util) and none for Math (java.lang).
Then show slide 19: a random card value from 2 through 14, both included. Pairs write the expression, (int) (Math.random() * 13) + 2, and check it with the stand-ins 0.0 and 0.999 (2 and 14). Then write the three tasks below on the board. Pairs write an expression for each task, then check both ends with stand-ins:
| Task | Expression | Possible values |
|---|---|---|
| A wait time from 0.0 up to but not including 10.0 seconds | Math.random() * 10 |
0.0 inclusive to 10.0 exclusive |
| A speed from 5.0 up to but not including 8.0 | Math.random() * 3 + 5 |
5.0 inclusive to 8.0 exclusive |
| A secret number from 1 to 100, both inclusive | (int) (Math.random() * 100) + 1 |
1, 2, …, 100 |
Run RangePractice to check them: with the stand-in 0.0 it prints 0.0, 5.0, and 1 (the low ends); with 0.5 the two double expressions give 5.0 and 6.5; with 0.999 the secret number gives 100 (the high end). Its other int line checks a step from −3 to 3, which is puzzle worksheet question 4; save it for going over the homework.
Teacher note
The card task is where most errors appear: * 12 (forgetting the + 1 in the count) or + 1 (the wrong low end).
Ask the pair whether 14 can ever appear, and what the stand-in 0.0 gives: the result must be 2.
Day 2 · Independent practice: Independent practice
Puzzle worksheet questions 4–6, 9, and 10 are homework: a snippet pool for a random int from −3 through 3, which values Math.random() and a cast can produce, a one-token fix for a die that never rolls 6, a Who Am I? on the five Math methods, and a reflection on why (int) (Math.random() * 4) can never be 4.
Day 2 · Closure: Closure
Point to a card on slide 20 and have a student give an example. Then show slide 21: pairs explain how to build a random int from 10 through 30 with the sentence starter (count 21 values, multiply by 21, cast the whole product with (int), add 10). Contrast it with a double: multiply by the width and add the low end, with no cast.
Day 2 · Exit ticket: Exit ticket
Give the exit ticket (about five minutes). Question 1 checks the value and type of abs, pow, and sqrt calls, including a nested call (1.11.A.2); question 2 checks writing a random int expression and its endpoints (1.11.A.3). Re-teach the weaker one at the start of Topic 1.12.