Skip to content
Ameen Abozaid
BEG-014

Methods

  • 8 Lessons
  • 94 min of reading
  • 5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Parameters
  • return values
  • signatures
  • overloading
  • scope
  • call stack
  • recursion basics

Learn first

94 min of reading · 5 h of practice (estimate)

Learning outcomes

  1. Extract repeated or named logic into a method with clear parameters and a return type.
  2. Explain pass-by-value for primitives and for references.
  3. Overload methods and explain how Java picks one.
  4. Describe the call stack and trace a sequence of method calls.
  5. Write a simple recursive method with a correct base case (factorial, sum of digits).

Lessons

  1. 01Why methods: naming and reuse11 min read
  2. 02Parameters and return values12 min read
  3. 03Method signatures and overloading11 min read
  4. 04Scope: where variables live12 min read
  5. 05The call stack12 min read
  6. 06Recursion basics12 min read
  7. 07Demo: building the calculator one method at a time12 min read
  8. 08Demo: tracing recursion on the call stack12 min read

Lab and exercises

Lab

BEG-014-LAB1 · Command-line calculator (mini-project 2 of 3)

Outcomes: 1–5 · Time: ~3 h

  1. Build a menu-driven calculator: add, subtract, multiply, divide, power, factorial, quit.
  2. Write each operation as its own method that returns a result and does not print.
  3. Make power iterative and factorial recursive.
  4. Handle division by zero and negative factorial input.
  5. Keep reading input and printing in separate methods from the math.

Acceptance: no duplicated logic; division by zero handled; recursion has a base case.

Exercises (assessment: six exercises plus the calculator)

IDOutcomeTask
BEG-014-EX011Extract the repeated "area of a rectangle" code from a given program into area(w, h).
BEG-014-EX022Predict the output: a method does x = x * 2 on a parameter; the caller prints its own variable afterwards. Explain.
BEG-014-EX033Write overloaded print(int), print(String), print(int, int). Which is called for print("a")?
BEG-014-EX044Draw the call stack at the deepest point of a() calling b() calling c().
BEG-014-EX055Write a recursive sumOfDigits(int n).
BEG-014-EX065Write recursive factorial(n) and say what happens without the base case.
Show solutions
  • EX01 static double area(double w, double h) { return w * h; }, called wherever the code was repeated.
  • EX02 The caller's variable is unchanged; Java passes a copy of the value. (For an object reference the copy points to the same object.)
  • EX03 print("a") matches the String version; Java picks the signature matching the argument types.
  • EX04 Bottom to top: main, a, b, c (top, running).
  • EX05 if (n < 10) return n; return n % 10 + sumOfDigits(n / 10); Example: 123 gives 6.
  • EX06 if (n <= 1) return 1; return n * factorial(n - 1); Without the base case it recurses forever and ends in StackOverflowError.
  • Outcome 1's "clear parameters and return type" and the pass-by-value of references are also checked in the quiz and the calculator.

Quiz

Outcomes: O1 extract methods · O2 pass-by-value · O3 overloading · O4 call stack · O5 recursion.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1A method that returns nothing has return type:O1
  2. 2A good method usually:O1
  3. 3A method changes its int parameter. The caller's variable:O2
  4. 4Java passes arguments:O2
  5. 5Overloaded methods must differ in:O3
  6. 6max(2.5, 1.0) with max(int,int) and max(double,double) calls:O3
  7. 7The call stack holds:O4
  8. 8When a method returns, its frame is:O4
  9. 9A recursive method needs:O5
  10. 10Recursion without a base case ends in:O5

Practical part

Six exercises BEG-014-EX01 to EX06 plus the command-line calculator. Pass: at least five exercises correct and a calculator with no duplicated logic, handled division by zero, and a recursive factorial with a base case.

Common mistakes

Methods that both compute and print; missing base case (stack overflow); expecting a method to change a caller's primitive.

Assessment

6 exercises plus the calculator.

Career relevance

Method design is the unit of clean code (JNR-003) and testing (BEG-045).

Type to search levels, phases, modules and lessons.