Overview
Students write their own methods for the first time (3.5.A). In Topics 1.9 to 1.15 they called methods someone else wrote; in Topic 3.3 they saw short methods with no parameters; in Topic 3.4 they wrote constructors. Day 1 is the void method and the mutator (3.5.A.1, 3.5.A.6, 3.5.A.7): a method that hands nothing back, whose call is a statement on its own, and which uses its parameters to change instance variables. Day 2 is the non-void method and the accessor (3.5.A.2, 3.5.A.3, 3.5.A.5): a method that returns one value compatible with its return type, so the call becomes that value, and an accessor hands back a copy of an instance variable’s value. Day 3 is the return keyword as “leave now” (3.5.A.4), including inside an if and a loop, and call by value for primitive parameters (3.5.A.8).
One question runs through all three days, from the slide 2 warm-up to the slide 22 closure: does this method give something back, or does it change the object?
Stay inside the topic boundary. Every parameter and return value is a primitive (int, double, boolean). Passing and returning object references is Topic 3.6, writing static methods and class variables is Topic 3.7, local-variable scope as a named idea is Topic 3.8, and this is Topic 3.9. If a student asks why a parameter can’t share an instance variable’s name, say that Topic 3.9 answers it and keep the names different today.
Teacher note
The slides are the Give It Back deck (22 slides, with speaker notes); slide numbers below refer to it. Day 1 uses slides 1–8, Day 2 uses slides 9–13, and Day 3 uses slides 14–22. The Give It Back puzzle worksheet (9 questions, about 30 minutes) is split across the three days: question 7 in class on Day 1, questions 2 and 1 in class on Day 2 and question 5 as Day 2 homework, question 3 in class on Day 3, and questions 4, 6, 8, and 9 as Day 3 homework. The Think First box at the top of Part A asks for a guess that question 8 settles; have students write the guess on Day 1.
All demonstration files are in the topic’s examples/ folder. Open them in the IDE before class so each demo is one click.
- Day 1 demonstrations:
FirstVoidMethod,MissingReturnType(compile error),AddSteps,ResetSteps,TwoTrackers,MutatorWrong. - Day 2 demonstrations:
StepsToGoal,GoalChecks,Accessors,AccessorCopy,AccessorAssign(compile error),VoidAsValue(compile error),LossyReturn(compile error). - Day 3 demonstrations:
BadgeLevel,EarlyVoidReturn,DaysToGoal,Unreachable(compile error),MissingReturn(compile error),CopyTrace,DepositSameName,ParameterAsResult. - Printed only (exit ticket):
ExitTicketTraceandExitPlant.
Day 1 · Warm-up: Ask it, or tell it?
Show slide 2. Pairs get two minutes to sort four requests you could make of a water bottle. Requests 1 (“How many ounces are left?”) and 3 (“Are you empty?”) give an answer back and leave the bottle alone; requests 2 (“Drink 10 ounces”) and 4 (“Refill to the top”) change the bottle and give nothing back. Write the two columns on the board and keep them up all week: that split is the whole topic. Before moving on, hand out the worksheet and have students write their Think First guess in the margin: does changing an int parameter to 0 change the caller’s variable?
Day 1 · Direct instruction: Void and non-void headers
- Goals (slide 3). Read the six goals: 1–2 are today’s, 3–4 are Day 2’s, and 5–6 are Day 3’s. Slide 4 opens Part 1.
- Two kinds of header (3.5.A.1, 3.5.A.2, slide 5). A void method does not return a value; its header has
voidbefore the name, as inpublic void drink(int amount). A non-void method returns a single value; its header puts that value’s type wherevoidwould go, as inpublic int getOunces(). Point at the two calls on the slide:b.drink(10);is a statement on its own line, andb.getOunces()is a value you print or store. Connect the two columns to the warm-up’s two columns. - A void method in a class. Run
FirstVoidMethod:printSummaryis written insideStepTracker, uses the private instance variablesteps, and is called with a dot frommain. The output shows the call running in the middle ofmainand control coming back to the next line. RunTwoTrackersto show that the same method prints each object’s ownsteps. - Every header needs a return type. Show
MissingReturnTypeand its error,invalid method declaration; return type required. “Nothing comes back” is said with the wordvoid; only a constructor has no return type, and its name matches the class.
Misconception: A void method doesn't need a return type
Students who hear “void returns nothing” leave the return type out entirely. Java rejects the header with return type required. Have students read every header in the order the slide 5 box gives: return type, then name, then parameters. Only a constructor skips the return type, and that’s how Java tells it apart from a method.
Day 1 · Direct instruction: A mutator changes the object
- The worked example (3.5.A.6, 3.5.A.7, slide 6). Read
WaterBottlewith the class.drink(int amount)receives a value through its parameter and uses it to do its job (3.5.A.7): it subtractsamountfromounces, then theifstopsouncesat 0. A method that changes an object’s instance variables is a mutator, and a mutator is often void. - Trace
mainon the board before you reveal the output.ouncesstarts at 24;drink(10)makes 14;drink(30)makes -16, and theifsets it to 0. Output: 14, then 0. Ask: “Which line changedounces?” The answer is the assignment withounceson the left. - Demo. Run
AddSteps(two calls toaddStepsbuild on each other: 6700, then 7500), thenResetSteps, a mutator with no parameter that setsstepsto 0 (prints 300). A mutator can take parameters or not; what makes it a mutator is that it changes instance variables.
Day 1 · Direct instruction: Changing the parameter changes nothing
Show slide 7. Both versions of addTime compute minutes + coins * 15, but the bug stores the answer in the parameter coins, which disappears when the method ends. After addTime(2) the bug leaves minutes at 0 and the fix makes it 30. Then run MutatorWrong: it compiles and prints Steps so far: 4200, as if the walk never happened. Ask students to point at the variable on the left of = in each version; that’s the one that changes.
Misconception: Storing the result in the parameter
Students think of “the variable in the method” as the one that matters, so they assign to the parameter. The code compiles and computes the right number, and the object never changes. Teach the check: in a mutator, the left side of the assignment must be an instance variable. Worksheet question 7 is this exact bug.
Day 1 · Guided practice: Turn it up
- Slide 8 (pause and try). Students trace
Dimmeron whiteboards. Answer:10, then6.Dimmerhas no constructor, solevelstarts at its default, 0 (Topic 3.4).up(3)makes 3;up(9)makes 12 and theifcaps it at 10;up(-4)makes 6. Each call changes the same object’slevel. - Worksheet question 7 (one-token fix) in pairs. Line 7 should read
points = points + bonus;: change the firstbonustopoints. The original stores 15 in the parameter, which disappears whenaddBonusends, sogetPointsstill returns 10. Ask pairs to say why in one sentence, not only which token.
Teacher note
On slide 8, watch for students who write 12 for the first line: they ran the addition but skipped the if. Ask them to read the method body one line at a time, in order, for each call.
Day 1 · Closure: Closure
Students finish this sentence in their notes: “A mutator __ the object and is usually __.” (changes; void). Collect a few aloud and keep the Day 2 half of the slide 22 starter for tomorrow.
Day 2 · Warm-up: What comes back?
Show StepsToGoal without running it. Students write both lines of output on whiteboards. Answer: Steps left: 5800 and Laps left: 11 (5800 / 500 with int division). Then ask: “In the second line, what did tracker.stepsToGoal() turn into?” The value 5800. That’s today’s idea.
Day 2 · Direct instruction: The call becomes the returned value
- Return by value (3.5.A.3, slides 9–10). In a non-void method,
returnevaluates its expression and sends back that value; this is return by value. On slide 10,r.area()evaluateswidth * height, 24, and the call is replaced by 24, soa + 1prints 25.ratio()returns(double) width / height, 1.5; the cast happens before the division (Topic 1.5). - Compatible with the return type. The returned value must fit the header’s return type:
intforarea,doubleforratio. RunGoalChecks:milesWalkedreturns adouble(2.1), andreachedGoalreturns abooleanthat anifuses directly (Topic 2.3). - What the caller does with it. A returned value can be stored, printed, or used in a bigger expression. If the caller ignores it, the value is simply lost.
Misconception: The return type doesn't have to match
Students pick the return type first and then write any expression. Show LossyReturn: steps / 2000.0 is a double, the header says int, and Java reports possible lossy conversion from double to int. Java won’t silently drop part of a value. Fix it by making the return type double. An int value returned from a double method is fine, as worksheet question 6, method 3, shows.
Day 2 · Direct instruction: An accessor hands back a copy
- Accessor (3.5.A.5, slide 11). An accessor is a non-void method that lets code outside the class get a copy of an instance variable’s value. The instance variable stays private; the accessor is the window into it. Run
Accessors:getStepsandgetGoalread the values, andaddStepsis the mutator between them. - A copy, not the variable. Trace slide 11:
leftgets a copy of 24,left = 0changes only the copy, and the output is0 24. RunAccessorCopy(prints 0, then 4200) to see it again. - A call is a value, not a variable. Show the bottom of slide 11 and
AccessorAssign:b.getOunces() = 0;giveserror: unexpected type. You can’t assign to a value. To change the object, call a mutator.
Misconception: Changing a value through an accessor
Students expect tracker.getSteps() = 0; or int s = tracker.getSteps(); s = 0; to change the object. The first doesn’t compile, and the second changes only the caller’s copy. Ask: “Which method in this class changes steps?” If there isn’t one, nothing outside the class can change it, and that’s the point of private instance variables.
Day 2 · Direct instruction: The call must match the return type
Show slide 12 and run both errors. VoidAsValue stores a void call (int shown = tracker.printSummary();): void cannot be converted to int; the slide’s System.out.println(b.drink(5)); gives 'void' type not allowed here. There’s nothing to print or store, so the fix is b.drink(5); on its own line. The right half is slide 10’s rule broken: ounces / 2.0 is a double in an int method. Give students the slide’s two questions to ask of every method: does it return anything, and if so, does the returned value fit the return type?
Misconception: Using a void call as a value
Students print or store every call because they’ve used Math and String methods that way. A void call has no value, so System.out.println(b.drink(5)); doesn’t compile. Have students look up the return type in the header before they decide whether a call goes on its own line or inside an expression.
Day 2 · Guided practice: Pick the header
- Slide 13 (pause and try). Students hold up A, B, C, or D. Answer: C,
public boolean hasWater(). The body returnsounces > 0, aboolean. A: a void method can’t return a value. B: abooleancan’t be converted toint. D: every method needs a return type. - Worksheet question 2 (pencil check) in pairs.
countgoes 8, 8, 16, 16, 7, 7, and the calls return nothing, 8, nothing,true, nothing,false. Only theaddrows changecount. Ask pairs to label each method in theCounterclass: mutator, accessor, or neither (isEvenreturns a value computed fromcountand changes nothing).
Day 2 · Closure: Closure
Students fill in worksheet question 1 (face-off) alone, then compare with a partner. Key words: non-void, copy, void, instance variables, the instance variable stays the same, argument. Question 5 (snippet pool) is homework; the holes are amount, double, and temp < 18.0.
Day 3 · Warm-up: Where does it stop?
Show EarlyVoidReturn without running it. Students write the output. Answer: Ignored: -300, Added: 600, 4800. The return; on the first call ends the method before steps changes. Point out that a void method can say return; with no value; it can’t return a value, because its header promised nothing comes back. (The Fair Question box on the worksheet before question 5 says the same.)
Day 3 · Direct instruction: The first return that runs wins
- Return ends the method (3.5.A.4, slides 14–15). The
returnkeyword sends control back to the point where the method was called. Areturninside anifends the whole method, not just theif. - Trace
fine()for the three books on slide 15.-2hits the firstreturn 0.3skips both ifs and returns3 * 25, 75.30hitsreturn 500, anddaysLate * 25never runs. Output: 0, 75, 500. Then runBadgeLevel(2, 3, 1): the order of the ifs matters, because the firstreturnthat runs wins.
Day 3 · Direct instruction: A return inside a loop
Slide 16. A return inside a loop stops the loop and the method. Fill in the trace table with the class: with weekly 15, total reaches 105 in week 7, so weeksUntil(100) returns 7 and the loop never sees week 8. For a goal of 1000, the loop runs all 52 weeks without a match, ends normally, and return -1; runs. Run DaysToGoal (3, then -1) for the same pattern. Ask: “Why does the method need the return -1; after the loop?” Because a non-void method must return a value even when the loop never finds a match.
Day 3 · Direct instruction: Every path needs a return, and nothing after it
Show slide 17 and run Unreachable and MissingReturn. Code right after a return in the same block can never run, and Java reports unreachable statement; move the print above the return. A non-void method must return a value on every path; isRich has none when the if is false, and Java reports missing return statement; add return false; after the if.
Misconception: return means remember the answer
Students write code after a return, expecting it to run, because they think of return as storing the answer and carrying on. return means leave now. Have students say “leave now” out loud when they reach a return in a trace.
Misconception: An if with a return covers every case
Students write one return inside an if and stop, so the method has no value when the condition is false. Java rejects it with missing return statement. Ask, for each method: “If every if is false, which return runs?” There must be one.
Day 3 · Direct instruction: Parameters get copies
- Call by value (3.5.A.8, slides 18–19). When the argument is a primitive, the parameter starts as a copy of the argument’s value. Trace slide 19:
scoreis 250,pointsstarts as a copy of 250,points = points / 10makes the copy 25, and 25 tickets are added.scoreis still 250. Output:250 25. Two separate variables; only the copy changes. - Demo. Run
CopyTrace:collectdoubles its parameter twice over two calls,giftstays 25, andraisedis 200. Then runDepositSameName: the parameter has the same name asmain’s variable,cash = 0inside the method, andmain’scashis still 20.0. Same name, different variable. - Sending an answer back. Run
ParameterAsResult: a void method stores its answer in a parameter, andmainprints 0. The way a method sends a primitive answer back is a return value:int left = tracker.stepsToGoal();. Now settle the Day 1 Think First guess: no, the caller’s variable never changes.
Misconception: Changing the parameter changes the argument
Students picture the parameter as the caller’s variable under a new name, especially when the names match. It is a separate variable that starts as a copy. Draw two boxes on the board, one in main and one in the method, and copy the value across at the call. Worksheet question 8’s lie is this misconception.
Misconception: A void method can fill in a variable for the caller
Students expect a method to “fill in” the variable it was given, as ParameterAsResult tries to. The copy and its new value vanish when the method ends. If the caller needs a primitive answer, the method must return it and the caller must store it.
Day 3 · Guided practice: Two copies in one program
- Slide 20 (pause and try). Students find every copy. Answer:
6 9.getGallonsreturns a copy of 3;maindoubles its own copy to 6.fillreceives a copy of 6, adds it togallons(3 + 6 = 9), then sets its parameter to 0, which changes nothing outside the method. - Worksheet question 3 (you’re the JVM) in pairs. Answer: A,
3 -1.unlock(3)returns on guess 3 withtriesat 3.unlock(9)never matches:triesgoes to 8, the loop finishes, andreturn -1;runs. Students who chose C saw thattriesreaches 8 but missed whichreturnran.
Day 3 · Closure: Closure and exit ticket
Slide 21 lists the vocabulary; read it quickly as a check. Then slide 22: partners explain the difference between a mutator and an accessor, with a header for each, using the sentence starter (“A mutator changes the object and is usually void, but an accessor returns a copy of an instance variable’s value”). Give the exit ticket in the last few minutes; finish it at the start of the next class if needed. Score each question separately: question 1 checks return inside an if and call by value, question 2 void and non-void calls, and question 3 recognizing a correct mutator and non-void method. Re-teach the weakest one at the start of Topic 3.6.
Day 3 independent practice (homework)
Worksheet questions 4 (code tiles), 6 (you’re the compiler), 8 (two truths and a lie), and 9 (who am I?), about 15 minutes. Key points: in question 4 the decoy tile cost = coins - cost; changes only the parameter; in question 6 only method 3 compiles (it prints 3.0); in question 8 claim 3 is the lie.