CS101 · Chapter 6 · Annotated SlidesMethods
Original
Overview

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. 1 Methods
  2. 2 Objectives
  3. 3 Opening Problem
  4. 4 Problem
  5. 5 Problem
  6. 6 Solution
  7. 7 Defining Methods
  8. 8 Defining Methods
  9. 9 Method Signature
  10. 10 Formal Parameters
  11. 11 Actual Parameters
  12. 12 Return Value Type
  13. 13 Calling Methods
  14. 14 animation
  15. 15 animation
  16. 16 animation
  17. 17 animation
  18. 18 animation
  19. 19 animation
  20. 20 animation
  21. 21 animation
  22. 22 animation
  23. 23 animation
  24. 24 animation
  25. 25 CAUTION
  26. 26 Reuse Methods from Other Classes
  27. 27 Call Stacks
  28. 28 animation
  29. 29 animation
  30. 30 animation
  31. 31 animation
  32. 32 animation
  33. 33 animation
  34. 34 animation
  35. 35 animation
  36. 36 animation
  37. 37 animation
  38. 38 Passing Parameters
  39. 39 void Method Example
  40. 40 Pass by Value
  41. 41 Pass by Value, cont.
  42. 42 Modularizing Code
  43. 43 Overloading Methods
  44. 44 Ambiguous Invocation
  45. 45 Ambiguous Invocation
  46. 46 Scope of Local Variables
  47. 47 Scope of Local Variables, cont.
  48. 48 Scope of Local Variables, cont.
  49. 49 Scope of Local Variables, cont.
  50. 50 Scope of Local Variables, cont.
  51. 51 Method Abstraction
  52. 52 Benefits of Methods
  53. 53 Case Study: Generating Random Characters
  54. 54 Case Study: Generating Random Characters, cont.
  55. 55 Case Study: Generating Random Characters, cont.
  56. 56 Case Study: Generating Random Characters, cont.
  57. 57 Case Study: Generating Random Characters, cont.
  58. 58 The RandomCharacter Class
  59. 59 (Optional)
  60. 60 Implementation: Bottom-Up
  61. 61 Benefits of Stepwise Refinement
Slide 1 / 61

Methods

Slide 1: Methods
Slide 2 / 61

Objectives

Slide 2: Objectives
Slide 3 / 61

Opening Problem

Slide 3: Opening Problem

Notes

  1. Sum 1–10, 20–30, 35–45: the same code three times → write one method.

Slide 4 / 61

Problem

Slide 4: Problem

Notes

  1. Problem repeated.

Slide 5 / 61

Problem

Slide 5: Problem

Notes

  1. Problem repeated.

Slide 6 / 61

Solution

Slide 6: Solution

Notes

  1. Solution: sum(int i1, int i2) reused three times.

Slide 7 / 61

Defining Methods

Slide 7: Defining Methods

Notes

  1. A method = modifiers, return type, name, parameters, body.

Slide 8 / 61

Defining Methods

Slide 8: Defining Methods
Slide 9 / 61

Method Signature

Slide 9: Method Signature

Exam angle

PredictionQ1

A method signature consists of:

Slide 10 / 61

Formal Parameters

Slide 10: Formal Parameters
Slide 11 / 61

Actual Parameters

Slide 11: Actual Parameters

Exam angle

PredictionQ1

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

Slide 12 / 61

Return Value Type

Slide 12: Return Value Type
Slide 13 / 61

Calling Methods

Slide 13: Calling Methods

Notes

  1. Calling a method transfers control to it; return gives control back.

Slide 14 / 61

animation

Slide 14: animation

Notes

  1. Trace.

Slide 15 / 61

animation

Slide 15: animation

Notes

  1. Trace.

Slide 16 / 61

animation

Slide 16: animation

Notes

  1. Trace.

Slide 17 / 61

animation

Slide 17: animation

Notes

  1. Trace.

Slide 18 / 61

animation

Slide 18: animation

Notes

  1. Trace.

Slide 19 / 61

animation

Slide 19: animation

Notes

  1. Trace.

Slide 20 / 61

animation

Slide 20: animation

Notes

  1. Trace.

Slide 21 / 61

animation

Slide 21: animation

Notes

  1. Trace.

Slide 22 / 61

animation

Slide 22: animation

Notes

  1. Trace.

Slide 23 / 61

animation

Slide 23: animation

Notes

  1. Trace.

Slide 24 / 61

animation

Slide 24: animation

Notes

  1. Trace.

Slide 25 / 61

CAUTION

Slide 25: CAUTION

Notes

  1. A value-returning method must return a value on every path; the compiler rejects a missing return after if/else-if.

  2. 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.

Slide 26 / 61

Reuse Methods from Other Classes

Slide 26: Reuse Methods from Other Classes
Slide 27 / 61

Call Stacks

Slide 27: Call Stacks

Notes

  1. Each call gets an activation record on the call stack (parameters and locals); popped on return.

Exam angle

PredictionQ1

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

Slide 28 / 61

animation

Slide 28: animation
Slide 29 / 61

animation

Slide 29: animation

Notes

  1. Trace.

Slide 30 / 61

animation

Slide 30: animation

Notes

  1. Trace.

Slide 31 / 61

animation

Slide 31: animation

Notes

  1. Trace.

Slide 32 / 61

animation

Slide 32: animation

Notes

  1. Trace.

Slide 33 / 61

animation

Slide 33: animation

Notes

  1. Trace.

Slide 34 / 61

animation

Slide 34: animation

Notes

  1. Trace.

Slide 35 / 61

animation

Slide 35: animation

Notes

  1. Trace.

Slide 36 / 61

animation

Slide 36: animation

Notes

  1. Trace.

Slide 37 / 61

animation

Slide 37: animation

Notes

  1. Trace.

Slide 38 / 61

Passing Parameters

Slide 38: Passing Parameters

Notes

  1. Arguments must match the parameters in order, number and compatible type.

Slide 39 / 61

void Method Example

Slide 39: void Method Example
Slide 40 / 61

Pass by Value

Slide 40: Pass by Value

Notes

  1. Pass by value: the method gets a COPY; changing the parameter doesn’t change the caller’s variable.

Exam angle

PredictionQ1

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.

Slide 41 / 61

Pass by Value, cont.

Slide 41: Pass by Value, cont.

Notes

  1. swap(num1, num2) inside a method doesn’t swap the caller’s values.

Slide 42 / 61

Modularizing Code

Slide 42: Modularizing Code
Slide 43 / 61

Overloading Methods

Slide 43: Overloading Methods

Notes

  1. Overloading: same name, different parameter lists (max(int,int), max(double,double)).

  2. Trap Different return type alone does NOT overload a method.

Exam angle

PredictionQ1

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

Slide 44 / 61

Ambiguous Invocation

Slide 44: Ambiguous Invocation

Notes

  1. Ambiguous invocation: two equally good matches → compile error (max(1, 2) with max(int,double) and max(double,int)).

Slide 45 / 61

Ambiguous Invocation

Slide 45: Ambiguous Invocation
Slide 46 / 61

Scope of Local Variables

Slide 46: Scope of Local Variables

Exam angle

PredictionQ1

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

Slide 47 / 61

Scope of Local Variables, cont.

Slide 47: Scope of Local Variables, cont.

Notes

  1. A parameter is a local variable whose scope is the whole method.

Slide 48 / 61

Scope of Local Variables, cont.

Slide 48: Scope of Local Variables, cont.

Notes

  1. A variable declared in a for header is in scope only inside the loop.

Slide 49 / 61

Scope of Local Variables, cont.

Slide 49: Scope of Local Variables, cont.

Notes

  1. Same name allowed in non-nested blocks; not in nested blocks.

Slide 50 / 61

Scope of Local Variables, cont.

Slide 50: Scope of Local Variables, cont.

Notes

  1. Scope examples.

Slide 51 / 61

Method Abstraction

Slide 51: Method Abstraction
Slide 52 / 61

Benefits of Methods

Slide 52: Benefits of Methods
Slide 53 / 61

Case Study: Generating Random Characters

Slide 53: Case Study: Generating Random Characters
Slide 54 / 61

Case Study: Generating Random Characters, cont.

Slide 54: Case Study: Generating Random Characters, cont.
Slide 55 / 61

Case Study: Generating Random Characters, cont.

Slide 55: Case Study: Generating Random Characters, cont.

Notes

  1. Continued.

Slide 56 / 61

Case Study: Generating Random Characters, cont.

Slide 56: Case Study: Generating Random Characters, cont.

Notes

  1. Continued.

Slide 57 / 61

Case Study: Generating Random Characters, cont.

Slide 57: Case Study: Generating Random Characters, cont.

Notes

  1. Continued.

Slide 58 / 61

The RandomCharacter Class

Slide 58: The RandomCharacter Class
Slide 59 / 61

(Optional)

Slide 59: (Optional)

Notes

  1. Stepwise refinement: top-down design of a calendar program.

Slide 60 / 61

Implementation: Bottom-Up

Slide 60: Implementation: Bottom-Up
Slide 61 / 61

Benefits of Stepwise Refinement

Slide 61: Benefits of Stepwise Refinement