A method signature consists of:
Methods
Defining and calling methods, signatures, formal vs. actual parameters, return types, void methods, the call stack, pass-by-value, overloading (and ambiguous calls), variable scope, method abstraction and stepwise refinement, with the random-character case study.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 61 slides
- 1 Methods
- 2 Objectives
- 3 Opening Problem
- 4 Problem
- 5 Problem
- 6 Solution
- 7 Defining Methods
- 8 Defining Methods
- 9 Method Signature
- 10 Formal Parameters
- 11 Actual Parameters
- 12 Return Value Type
- 13 Calling Methods
- 14 animation
- 15 animation
- 16 animation
- 17 animation
- 18 animation
- 19 animation
- 20 animation
- 21 animation
- 22 animation
- 23 animation
- 24 animation
- 25 CAUTION
- 26 Reuse Methods from Other Classes
- 27 Call Stacks
- 28 animation
- 29 animation
- 30 animation
- 31 animation
- 32 animation
- 33 animation
- 34 animation
- 35 animation
- 36 animation
- 37 animation
- 38 Passing Parameters
- 39 void Method Example
- 40 Pass by Value
- 41 Pass by Value, cont.
- 42 Modularizing Code
- 43 Overloading Methods
- 44 Ambiguous Invocation
- 45 Ambiguous Invocation
- 46 Scope of Local Variables
- 47 Scope of Local Variables, cont.
- 48 Scope of Local Variables, cont.
- 49 Scope of Local Variables, cont.
- 50 Scope of Local Variables, cont.
- 51 Method Abstraction
- 52 Benefits of Methods
- 53 Case Study: Generating Random Characters
- 54 Case Study: Generating Random Characters, cont.
- 55 Case Study: Generating Random Characters, cont.
- 56 Case Study: Generating Random Characters, cont.
- 57 Case Study: Generating Random Characters, cont.
- 58 The RandomCharacter Class
- 59 (Optional)
- 60 Implementation: Bottom-Up
- 61 Benefits of Stepwise Refinement
Opening Problem
Notes
Sum 1–10, 20–30, 35–45: the same code three times → write one method.
Problem
Notes
Problem repeated.
Problem
Notes
Problem repeated.
Solution
Notes
Solution: sum(int i1, int i2) reused three times.
Defining Methods
Notes
A method = modifiers, return type, name, parameters, body.
Method Signature
Exam angle
Actual Parameters
Exam angle
In max(3, 7), the values 3 and 7 are:
Calling Methods
Notes
Calling a method transfers control to it; return gives control back.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
CAUTION
Notes
A value-returning method must return a value on every path; the compiler rejects a missing return after if/else-if.
Trap if (n > 0) return 1; else if (n == 0) return 0; else if (n < 0) return -1; → compile error: the compiler can’t prove a return.
Call Stacks
Notes
Each call gets an activation record on the call stack (parameters and locals); popped on return.
Exam angle
Each method call creates a new activation record on the call stack.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
Passing Parameters
Notes
Arguments must match the parameters in order, number and compatible type.
Pass by Value
Notes
Pass by value: the method gets a COPY; changing the parameter doesn’t change the caller’s variable.
Exam angle
A method increments its int parameter. Does the caller’s variable change? Why?
Compare with the model answer
No. Java passes primitives by value: the method works on a copy, so the caller’s variable keeps its original value.
Pass by Value, cont.
Notes
swap(num1, num2) inside a method doesn’t swap the caller’s values.
Overloading Methods
Notes
Overloading: same name, different parameter lists (max(int,int), max(double,double)).
Trap Different return type alone does NOT overload a method.
Exam angle
Two methods can be overloaded if they differ only in return type.
- Parameter lists must differ.
Ambiguous Invocation
Notes
Ambiguous invocation: two equally good matches → compile error (max(1, 2) with max(int,double) and max(double,int)).
Scope of Local Variables
Exam angle
The scope of a variable declared in a for-loop header is:
Scope of Local Variables, cont.
Notes
A parameter is a local variable whose scope is the whole method.
Scope of Local Variables, cont.
Notes
A variable declared in a for header is in scope only inside the loop.
Scope of Local Variables, cont.
Notes
Same name allowed in non-nested blocks; not in nested blocks.
Scope of Local Variables, cont.
Notes
Scope examples.
Case Study: Generating Random Characters, cont.
Notes
Continued.
Case Study: Generating Random Characters, cont.
Notes
Continued.
Case Study: Generating Random Characters, cont.
Notes
Continued.
(Optional)
Notes
Stepwise refinement: top-down design of a calendar program.