Overview
Two periods on two learning objectives: identify the correct method to call from documentation and method signatures (1.9.A), and describe how to call methods (1.9.B). Day 1 covers what a method is, the flow of control, procedural abstraction, and how a call’s arguments must match a signature (1.9.A.1, 1.9.A.2, 1.9.B.3, 1.9.B.5). Day 2 covers return values, void and non-void methods, call by value, and overloading (1.9.B.1–1.9.B.4), then puts everything together in one trace.
The CED’s suggested skill is 3.C, determining the result or output of code that contains procedural abstractions. Most of class time is prediction: students commit to an output or a “compiles / does not compile” before you run anything.
Topic boundaryStay inside the topic boundary
- Students read methods; they do not write their own methods until Unit 3. Every method in the deck and the puzzle worksheet is already written; students read headers, trace calls, and decide whether calls compile.
- Every method header starts with
public static. Tell students once that those words are explained in a later topic and can be read past for now. Math.sqrt,Math.pow,Math.abs, andMath.randomare called through the class name. Say what each does where it is used; Topic 1.10 explains calls through a class name fully, and Topic 1.11 covers the wholeMathclass.- The
Stringheaders forlength,indexOf, andsubstringappear only as documentation to read. Students do not callStringmethods until Topic 1.14, andsubstringis taught in Topic 1.15. - A label and a number still take a
printfollowed by aprintln; joining text and numbers is Topic 1.15.
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, methods and flow of control:
NeverCalled,ReminderTwice(the program on slide 5),Announcements,NestedCalls,DefinedFirst, andMissingParens. - Day 1, procedural abstraction:
SquareRoot,Ladder, andPowGuess. - Day 1, parameters and signatures:
TicketTotal,TicketVariables,ParameterOutside,PowOrder,SqrtInt,SqrtTwoArgs,SqrtString, andTicketSwapped. - Day 2, return values:
ReturnValues,MinutesBack,VoidInExpression,PrintVoid,IgnoredReturn, andIgnoredReturnFixed. - Day 2, call by value:
BonusCopy,BonusPoints, andSameName. - Day 2, overloading:
AbsOverload,Receipt,OrderOverload, andSameSignature. - Day 2 guided practice:
QuizReport. - Printed only (exit ticket):
ExitRefill. The puzzle worksheet and its key are separate Word files; their code is printed on the page.
The Read the Header deck covers both days: slides 1–12 on Day 1 and slides 13–21 on Day 2. Its speaker notes hold the answers to every Pause and Try slide. Before class, open NeverCalled, TicketTotal, and a scratch class in the IDE next to the deck. Compiler messages are quoted from a recent JDK; the wording can vary slightly between Java versions.
Day 1 · Warm-up: One name, many kinds of values
Show slide 2. All five lines call println with an int, a double, a String, and nothing at all. Give pairs two minutes on “How can one method accept all of these?” Collect guesses such as “it checks the type” and “there are several printlns,” and leave the question open: the answer is overloading, on Day 2 (slide 18). Read the goals on slide 3.
Teacher note
Keep this to five minutes. Tell students public static is explained in a later topic and can be read past, and that
void is explained on Day 2.
Day 1 · Direct instruction: Methods, blocks, calls, and the flow of control
Start Part 1 (slide 4).
- Predict:
NeverCalled. Show it and ask students to write its exact output. Many will write three lines, including “Bring your library book.” Run it: only two lines appear. Ask, “Line 11 is a perfectly good statement. Why did it not run?” - Block and method (1.9.A.1, slide 5). Any section of code enclosed in braces is a block. A method is a named block of code that runs only when it is called. In
NeverCalled, lines 10–12 are the block that belongs toprintReminder, and nothing calls it. Students have been calling methods since Topic 1.1:System.out.printlnis one, andmainis the method Java calls for them when the program starts. - A call. Slide 5 shows the same method with two calls;
ReminderTwiceis the runnable version. A call is the method’s name, parentheses, and a semicolon. The reminder appears twice, and its wording lives in one place. Because the method is in the same class asmain, the call is just its name; Topic 1.10 explains the full rules for calling methods. - Flow of control (1.9.B.5). The order statements actually run in is the flow of control. A call interrupts the top-to-bottom order: Java runs every statement in the method, then returns to the point immediately after the call. Run
Announcements: the two lunch lines appear betweenmain’s lines, exactly where the call was. Use the recipe picture: “make the sauce (see page 40)”, do page 40, come back to the next step. - Calls inside calls. Run
NestedCalls. WhenprintLunchMenufinishes, control returns toprintSchedule(whoever called it), not tomain. Each method returns to its caller. - Predict:
DefinedFirst. The method is written abovemain. Students predict, then run:First period,The bell rings.,Second period. The program always starts inmain, and where a method is written in the file does not matter. - The parentheses are the call (slide 6).
printReminder;does not compile:not a statement. RunMissingParensto show the message live.
Misconception: Methods run in the order they are written in the file
Students read a file from the top line to the bottom line, so they expect a method written above main to run first and a method that is never called to run anyway. NeverCalled and DefinedFirst are the fix: let students commit to the wrong prediction, then run the program. Give them one rule for every trace: start at the first line of main, and follow the calls.
Misconception: The method's name alone runs it
Students write printReminder; and expect the reminder. Slide 6 and MissingParens show that Java reports not a statement. Without parentheses, the name is just a name; the parentheses make it a call.
Day 1 · Direct instruction: Procedural abstraction
- What, not how (1.9.A.1). Ask, “How does
System.out.printlnget letters onto the screen?” Nobody knows, and everyone has used it since Topic 1.1. Procedural abstraction lets a programmer use a method by knowing what it does, even without knowing how it was written. A driver turns the steering wheel without knowing how the steering system is built. - Documentation is the whole view (slide 7). The slide shows the documentation comment and header for
Math.random: it needs nothing and gives back adoublefrom 0.0 up to, but not including, 1.0. Nobody in the room knows how it picks the number, and nobody needs to. Then say the documentation forMath.sqrtaloud: “Returns the positive square root of a,” with the precondition that a is not negative (Topic 1.8). RunSquareRoot(12.0, then1.4142135623730951). The code that finds square roots is nowhere in sight. - Reuse. Run
Ladder: the same method, a new problem (a ladder 3 meters up a wall and 4 meters out is5.0meters long). One well-documented method serves many programs. - Predict:
PowGuess.Math.pow(a, b)gives a raised to the power b. Students predictMath.pow(2, 3). Most say8; it prints8.0, because the documentation’s header,public static double pow(double a, double b), says the method gives back adouble. Procedural abstraction lets you skip the how, never the what.
Misconception: Guessing what a method does from its name
Students treat abstraction as permission not to read anything. They predict 8 for Math.pow(2, 3), or pass a negative number to Math.sqrt. Make “What goes in? What comes out? Any precondition?” the three questions students ask of every method’s documentation before they call it.
Day 1 · Direct instruction: Headers, parameters, arguments, and signatures
Start Part 2 (slide 8).
- Method header (1.9.A.2, slide 9). Take apart
public static void printTotal(double price, int qty):public static(later topic), the return typevoid(Day 2), the name, and the parameter list in parentheses. A parameter is a variable declared in the header that the method’s body can use; it is how values get into the method. - Method signature (1.9.A.2, slide 9). The signature is the method’s name and the ordered list of its parameter types:
printTotal(double, int). A method with no parameters has an empty list:random(). Walk the slide’s three rows, header to signature. Ask, “Why are the return type and the parameter names left out?” Because a call shows neither; the signature holds exactly what Java can check against a call. - Arguments (1.9.B.3, slide 10). An argument is a value passed into a method when it is called. On the slide,
printTotal(4.5, 3)startspriceas 4.5 andqtyas 3 and prints13.5. The first parameter gets the first argument, the second gets the second. RunTicketTotalfor the same idea with two calls: each call starts the method fresh with new values. - Arguments are values. Run
TicketVariables: the arguments are a variable and the expressiongroupSize + 1, and their names have nothing to do with the parameter names. Java evaluates each argument and passes the value (38.0, then47.5). Matching is by position, never by name. - Parameters stay in their method. Show
ParameterOutside:maintries to printticketsand getscannot find symbol. - Matching a call (1.9.B.3, slide 11). Arguments must be compatible with the signature in number, order, and type. An
intargument may go into adoubleparameter (widening, Topic 1.5):SqrtIntprints7.0. Adoublemay not go into anintparameter. On the slide,printTotal(3, 4.5)does not compile: 4.5 would go intoint qty, and Java reportsincompatible types: possible lossy conversion from double to int.TicketSwappedis the runnable version. ShowPowOrder:pow(2.0, 5.0)is32.0butpow(5.0, 2.0)is25.0. RunSqrtTwoArgsandSqrtStringfor the wrong-number and wrong-type messages.
Misconception: A parameter can be used anywhere in the program
Students expect tickets to exist in main after the call. A parameter belongs to its own method’s block. Point at ParameterOutside’s cannot find symbol, and ask: “Where was tickets declared?” If main needs the value, main keeps it in its own variable.
Misconception: Java matches arguments to parameters by name or by meaning
Students remember which values a method needs but not their order, and expect Java to sort them out. Java matches by position only. When the types differ, a swap is a compile error (slide 11, TicketSwapped); when both parameters have the same type, a swap compiles and gives a wrong answer (PowOrder). Have students write the signature next to every call they check.
Day 1 · Guided practice: Which calls compile?
Show slide 12: the header public static void printTotal(double price, int qty) and four calls. Pairs write the signature, then mark each call “compiles” or “does not compile” with a reason that names number, order, or type, and write the output of each call that compiles. Debrief one call at a time:
| Call | Compiles? | Why |
|---|---|---|
printTotal(2.0, 5); |
Yes | A double, then an int. It prints 10.0. |
printTotal(5, 2.0); |
No | Order: 2.0 would go into the int parameter qty. |
printTotal(2, 5); |
Yes | The int 2 widens to the double 2.0. It prints 10.0. |
printTotal(2.0); |
No | Number: one argument, two parameters (argument lists differ in length). |
Teacher note
Call C is the one to linger on: many students mark it wrong because 2 “is not a double.” The rule is “compatible with”
the parameter type, not “exactly the same as.” If computers are available, pairs can paste the printTotal method
from slide 10 into a scratch class and try all four calls in main.
Day 1 · Independent practice: Independent practice
Puzzle worksheet question 1 (Pencil Check: return types and signatures from headers) is homework. The rest of the worksheet uses return values, void methods, call by value, and overloading, so it waits for Day 2.
Day 1 · Closure: Closure
Ask students to finish three sentences in their notes: “A parameter is …”, “An argument is …”, and “A method signature is …”. Call on three students, one for each. Listen for “declared in the header”, “the value passed in a call”, and “the name and the ordered parameter types”. Hand out the Read the Header puzzle worksheet. Day 2 picks up the deck at slide 13.
Day 2 · Warm-up: Signatures from memory
Students write the signatures for public static int abs(int a) and public static void printTotal(double price, int qty), then decide which of printTotal(3, 4.5); and printTotal(4.5, 3); compiles. Answers: abs(int), printTotal(double, int), and only the second call. Re-teach the order rule with slide 11 if more than a few students miss the last part. Check puzzle worksheet question 1 quickly against the same rule.
Day 2 · Direct instruction: Void and non-void methods, return values
Start Part 3 (slide 13).
- Jump in, run, come back (1.9.B.5, slide 14). Trace
toIncheson the board. The second line ofmainneedstoInches(3)before it can add 4, so control jumps into the method, which printsconverting. The return statement sends 36 back and ends the method. Control returns to the middle of the same line, where the call is replaced by 36, and 36 + 4 is 40. Output:measuring,converting,40.MinutesBackis the same idea in the IDE (Converting, then135). - Two kinds of methods (1.9.B.1, 1.9.B.2, slide 15). The return type says which kind. A void method gives back nothing (a doorbell):
printTotal,System.out.println. Its call is used only as a statement on its own, never as part of an expression. A non-void method gives back a return value of exactly its return type (a calculator):toInchesreturns anint,sqrtandpowreturn adouble. - Using a return value. The caller must store it in a variable or use it in an expression. Show
ReturnValuesand replace each call with its value on the board:double side = 9.0;, thenSystem.out.println(15 + 5);. It prints9.0and20. Explain absolute value once: a number’s distance from zero. - Predict: a void call used as a value. Slide 15’s
double t = printTotal(4.5, 3);does not compile:incompatible types: void cannot be converted to double. RunVoidInExpressionfor the same message, andPrintVoid, which puts the call insideprintlnand gets'void' type not allowed here. - Predict: a return value thrown away. Slide 15’s
toInches(5);compiles, but its 60 is thrown away. ShowIgnoredReturn. It compiles and prints-8:Math.abs(temperature)returned 8, but nothing stored or used it.IgnoredReturnFixedstores the result and prints8.
Misconception: A void method's call can be stored or printed
Students see printTotal(4.5, 3) print 13.5 and assume the call “is” 13.5. Printing is not returning. A void method has no value to store, so its call must stand alone. Point to the return type before every call: void means a statement on its own line.
Misconception: Calling a non-void method is enough
Math.abs(temperature); compiles, so students believe it worked. The value came back and was thrown away. There is no error message, which makes this mistake harder to find than the void one. Ask of every non-void call: “Where does the returned value go?”
Day 2 · Direct instruction: Call by value
- Predict:
BonusCopy.mainpassesmyScore(80) toaddBonus, which adds 10 to its parameter and prints it. Students write both lines of output. Many predict90and90. Run it:90, then80. - Call by value (1.9.B.3, slide 16). Java passes arguments using call by value: each parameter is initialized with a copy of its argument’s value. The parameter is a separate variable, so changing it changes only the copy. Walk the slide’s table with one column for
countand one forn:addOneprints6, andmainstill prints5. The photocopy picture works: scribbling on the copy leaves the original untouched. - Same name, still two variables. Run
SameName(the guide’s version, wheremain’s variable is also calledscore): the output is the same asBonusPoints,Inside addBonus: 90andBack in main: 80. Each method sees only its own variable; the call copies the value from one to the other, and that is the only connection.
Misconception: The method changed its parameter, so my variable changed too
This is the most common mistake with methods. Students picture the variable itself traveling into the method. Use the two-column trace from slide 16 every time a variable is passed today, with the parameter’s column marked “gone” after the method finishes. Then ask: “If a method cannot change my variable, how could it get a new value back to me?” Answer: it returns the value, and the caller stores it (slide 14).
Day 2 · Direct instruction: Overloaded methods
Start Part 4 (slide 17).
- Same name, different signatures (1.9.B.4, slide 18). Two methods named
areahave different parameter lists, so different signatures:area(int)andarea(int, int).area(4)prints16andarea(4, 5)prints20. Methods are overloaded when several share a name but have different signatures. Java compares the call’s arguments with each signature and runs the one that matches. Back to the warm-up:printlnis overloaded the same way, withprintln(int),println(double),println(String),println(), and more. - Predict:
AbsOverload.Math.abshas two versions,abs(int)andabs(double).-7is anintliteral and-7.0is adoubleliteral, so they pick different versions:7,7.5,7.0. - Different number or order. The
Stringdocumentation listssubstring(int)andsubstring(int, int). If time allows, runReceipt(4.25, then8.5) for overloads that differ in number, andOrderOverload(Version 2) for overloads that differ only in the order of the types. - Not overloading. Slide 18’s last line: adding
double area(int length)fails, because its signature isarea(int)again.SameSignatureshows the same thing in the IDE: two methods namedhalfthat differ only in return type and parameter name, and Java reportsmethod half(int) is already defined. A call shows neither the return type nor the parameter names, so neither can tell two methods apart. - Pause and try (slide 19). Students decide which of three
showmethods each call runs. A runsshow(int), B runsshow(double), C runsshow(int, double)because 4 widens to 4.0, and D does not compile: 3.0 cannot go intoint n.
Misconception: Changing the return type or a parameter name makes a new method
Students think any change to a header creates an overload. Have them write the signatures of both headers side by side. If the signatures match, Java cannot tell the methods apart. Only the number, types, or order of the parameters can make two signatures different.
Day 2 · Guided practice: Trace QuizReport
Pairs open QuizReport in the IDE (or read it on the projector). First they make a small table on paper giving each header’s signature and saying whether it is void or non-void. Then they trace main with columns for quiz and curved and write the exact output before running it:
Score: 78
Score: 83
Points from goal: 7
Debrief with four questions: Where does control go at each call? (Line 6 jumps into addCurve, whose lines 16–17 return 83 to the middle of line 6.) Why does quiz still print 78 after score += points? (Call by value: score is a copy.) Why does line 6 store a result but lines 7 and 8 do not? (addCurve is non-void; printScore is void.) Which abs runs on line 9? (abs(int), because curved - 90 is an int.) The study guide’s “Putting it together” section has the full trace table.
Day 2 · Independent practice: Independent practice
Puzzle worksheet questions 2–9 are homework (about 25 minutes): tracing a call into a method and back, deciding which calls compile, a snippet pool, a void versus non-void face-off, overloaded abs headers, call by value, and a reflection on parameters and arguments.
Day 2 · Closure: Closure and exit ticket
Point to a card on slide 20 and have a student give an example of each word from today. Then show slide 21: pairs read public static String initials(String first, String last) aloud with the sentence starter, and you cold-call two pairs. Listen for “returns a String,” “needs two Strings in that order,” and “signature initials(String, String).” Then give the exit ticket (about five minutes). Question 1 checks flow of control, call by value, and return values (1.9.B.2, 1.9.B.3, 1.9.B.5); question 2 checks signatures and matching calls (1.9.A.2, 1.9.B.3). Re-teach the weaker one at the start of Topic 1.10.