- Objectives: define, call, pass parameters, overload, scope, abstraction.
- Sum 1–10, 20–30, 35–45: the same code three times → write one method.
- Problem repeated.
- Problem repeated.
- Solution: sum(int i1, int i2) reused three times.
- A method = modifiers, return type, name, parameters, body.
- public static int max(int num1, int num2) { … }
- Method signature = name + parameter list.
🎯 How this can come up · 1 question
A method signature consists of:
Correct: The method name and parameter list.
- Formal parameters: the variables in the method header.
- Actual parameters (arguments): the values passed in the call.
🎯 How this can come up · 1 question
In max(3, 7), the values 3 and 7 are:
Correct: Actual parameters (arguments).
- Return type: the type of the value returned; void returns nothing.
- Calling a method transfers control to it; return gives control back.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- A value-returning method must return a value on every path; the compiler rejects a missing return after if/else-if.
- Call static methods of other classes: ClassName.methodName(...).
- Each call gets an activation record on the call stack (parameters and locals); popped on return.
🎯 How this can come up · 1 question
Each method call creates a new activation record on the call stack.
True.
- Call stack trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Arguments must match the parameters in order, number and compatible type.
- void method example: printGrade(score).
- Pass by value: the method gets a COPY; changing the parameter doesn’t change the caller’s variable.
🎯 How this can come up · 1 question
A method increments its int parameter. Does the caller’s variable change? Why?
No. Java passes primitives by value: the method works on a copy, so the caller’s variable keeps its original value.
- swap(num1, num2) inside a method doesn’t swap the caller’s values.
- Modularize code: GCD and prime-number methods.
- Overloading: same name, different parameter lists (max(int,int), max(double,double)).
🎯 How this can come up · 1 question
Two methods can be overloaded if they differ only in return type.
False. Parameter lists must differ.
- Ambiguous invocation: two equally good matches → compile error (max(1, 2) with max(int,double) and max(double,int)).
- Ambiguous invocation example.
- Scope of a local variable: from its declaration to the end of its block.
🎯 How this can come up · 1 question
The scope of a variable declared in a for-loop header is:
Correct: The loop only.
- A parameter is a local variable whose scope is the whole method.
- A variable declared in a for header is in scope only inside the loop.
- Same name allowed in non-nested blocks; not in nested blocks.
- Scope examples.
- Method abstraction: use a method knowing only its signature (black box).
- Benefits: write once reuse, hide details, reduce complexity.
- Random characters: (char)('a' + Math.random() * ('z' − 'a' + 1)).
- Case study continued.
- Continued.
- Continued.
- Continued.
- RandomCharacter class with getRandomCharacter, getRandomLowerCaseLetter, getRandomDigitCharacter.
- Stepwise refinement: top-down design of a calendar program.
- Top-down vs. bottom-up implementation with stubs.
- Benefits: simpler programs, reuse, easier development, debugging and testing, teamwork.
No slides match this filter.