CS101 · Annotated SlidesChapter 6 — Methods
Chapter 6 · Annotated slides

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.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up

Methods

CS101 · Chapter 6
1
Slide 2: Objectives
Exam weight Low
  1. Objectives: define, call, pass parameters, overload, scope, abstraction.
Slide 3: Opening Problem
Exam weight Low
  1. Sum 1–10, 20–30, 35–45: the same code three times → write one method.
Slide 4: Problem
Exam weight Low
  1. Problem repeated.
Slide 5: Problem
Exam weight Low
  1. Problem repeated.
Slide 6: Solution
Exam weight Low
  1. Solution: sum(int i1, int i2) reused three times.
Slide 7: Defining Methods
Exam weight Low
  1. A method = modifiers, return type, name, parameters, body.
Slide 8: Defining Methods
Exam weight Low
  1. public static int max(int num1, int num2) { … }
Slide 9: Method Signature
Exam weight High
  1. Method signature = name + parameter list.
🎯 How this can come up · 1 question
MCQ

A method signature consists of:

Slide 10: Formal Parameters
Exam weight Low
  1. Formal parameters: the variables in the method header.
Slide 11: Actual Parameters
Exam weight High
  1. Actual parameters (arguments): the values passed in the call.
🎯 How this can come up · 1 question
MCQ

In max(3, 7), the values 3 and 7 are:

Slide 12: Return Value Type
Exam weight Low
  1. Return type: the type of the value returned; void returns nothing.
Slide 13: Calling Methods
Exam weight Low
  1. Calling a method transfers control to it; return gives control back.
Slide 14: animation
Exam weight Low
  1. Trace.
Slide 15: animation
Exam weight Low
  1. Trace.
Slide 16: animation
Exam weight Low
  1. Trace.
Slide 17: animation
Exam weight Low
  1. Trace.
Slide 18: animation
Exam weight Low
  1. Trace.
Slide 19: animation
Exam weight Low
  1. Trace.
Slide 20: animation
Exam weight Low
  1. Trace.
Slide 21: animation
Exam weight Low
  1. Trace.
Slide 22: animation
Exam weight Low
  1. Trace.
Slide 23: animation
Exam weight Low
  1. Trace.
Slide 24: animation
Exam weight Low
  1. Trace.
Slide 25: CAUTION
Exam weight Medium
  1. A value-returning method must return a value on every path; the compiler rejects a missing return after if/else-if.
Exam trapif (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.
Slide 26: Reuse Methods from Other Classes
Exam weight Low
  1. Call static methods of other classes: ClassName.methodName(...).
Slide 27: Call Stacks
Exam weight High
  1. Each call gets an activation record on the call stack (parameters and locals); popped on return.
🎯 How this can come up · 1 question
True / False

Each method call creates a new activation record on the call stack.

Slide 28: animation
Exam weight Low
  1. Call stack trace.
Slide 29: animation
Exam weight Low
  1. Trace.
Slide 30: animation
Exam weight Low
  1. Trace.
Slide 31: animation
Exam weight Low
  1. Trace.
Slide 32: animation
Exam weight Low
  1. Trace.
Slide 33: animation
Exam weight Low
  1. Trace.
Slide 34: animation
Exam weight Low
  1. Trace.
Slide 35: animation
Exam weight Low
  1. Trace.
Slide 36: animation
Exam weight Low
  1. Trace.
Slide 37: animation
Exam weight Low
  1. Trace.
Slide 38: Passing Parameters
Exam weight Low
  1. Arguments must match the parameters in order, number and compatible type.
Slide 39: void Method Example
Exam weight Low
  1. void method example: printGrade(score).
Slide 40: Pass by Value
Exam weight High
  1. 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
Written

A method increments its int parameter. Does the caller’s variable change? Why?

Slide 41: Pass by Value, cont.
Exam weight Low
  1. swap(num1, num2) inside a method doesn’t swap the caller’s values.
Slide 42: Modularizing Code
Exam weight Low
  1. Modularize code: GCD and prime-number methods.
Slide 43: Overloading Methods
Exam weight High
  1. Overloading: same name, different parameter lists (max(int,int), max(double,double)).
Exam trapDifferent return type alone does NOT overload a method.
🎯 How this can come up · 1 question
True / False

Two methods can be overloaded if they differ only in return type.

Slide 44: Ambiguous Invocation
Exam weight Low
  1. Ambiguous invocation: two equally good matches → compile error (max(1, 2) with max(int,double) and max(double,int)).
Slide 45: Ambiguous Invocation
Exam weight Low
  1. Ambiguous invocation example.
Slide 46: Scope of Local Variables
Exam weight High
  1. Scope of a local variable: from its declaration to the end of its block.
🎯 How this can come up · 1 question
MCQ

The scope of a variable declared in a for-loop header is:

Slide 47: Scope of Local Variables, cont.
Exam weight Low
  1. A parameter is a local variable whose scope is the whole method.
Slide 48: Scope of Local Variables, cont.
Exam weight Low
  1. A variable declared in a for header is in scope only inside the loop.
Slide 49: Scope of Local Variables, cont.
Exam weight Low
  1. Same name allowed in non-nested blocks; not in nested blocks.
Slide 50: Scope of Local Variables, cont.
Exam weight Low
  1. Scope examples.
Slide 51: Method Abstraction
Exam weight Low
  1. Method abstraction: use a method knowing only its signature (black box).
Slide 52: Benefits of Methods
Exam weight Low
  1. Benefits: write once reuse, hide details, reduce complexity.
Slide 53: Case Study: Generating Random Characters
Exam weight Low
  1. Random characters: (char)('a' + Math.random() * ('z' − 'a' + 1)).
Slide 54: Case Study: Generating Random Characters, cont.
Exam weight Low
  1. Case study continued.
Slide 55: Case Study: Generating Random Characters, cont.
Exam weight Low
  1. Continued.
Slide 56: Case Study: Generating Random Characters, cont.
Exam weight Low
  1. Continued.
Slide 57: Case Study: Generating Random Characters, cont.
Exam weight Low
  1. Continued.
Slide 58: The RandomCharacter Class
Exam weight Low
  1. RandomCharacter class with getRandomCharacter, getRandomLowerCaseLetter, getRandomDigitCharacter.
Slide 59: (Optional)
Exam weight Low
  1. Stepwise refinement: top-down design of a calendar program.
Slide 60: Implementation: Bottom-Up
Exam weight Low
  1. Top-down vs. bottom-up implementation with stubs.
Slide 61: Benefits of Stepwise Refinement
Exam weight Low
  1. Benefits: simpler programs, reuse, easier development, debugging and testing, teamwork.