Methods
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
- Extract repeated or named logic into a method with clear parameters and a return type.
- Explain pass-by-value for primitives and for references.
- Overload methods and explain how Java picks one.
- Describe the call stack and trace a sequence of method calls.
- Write a simple recursive method with a correct base case (factorial, sum of digits).
Lessons
- 01Why methods: naming and reuse11 min read
- 02Parameters and return values12 min read
- 03Method signatures and overloading11 min read
- 04Scope: where variables live12 min read
- 05The call stack12 min read
- 06Recursion basics12 min read
- 07Demo: building the calculator one method at a time12 min read
- 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
- Build a menu-driven calculator: add, subtract, multiply, divide, power, factorial, quit.
- Write each operation as its own method that returns a result and does not print.
- Make
poweriterative andfactorialrecursive. - Handle division by zero and negative factorial input.
- 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)
| ID | Outcome | Task |
|---|---|---|
| BEG-014-EX01 | 1 | Extract the repeated "area of a rectangle" code from a given program into area(w, h). |
| BEG-014-EX02 | 2 | Predict the output: a method does x = x * 2 on a parameter; the caller prints its own variable afterwards. Explain. |
| BEG-014-EX03 | 3 | Write overloaded print(int), print(String), print(int, int). Which is called for print("a")? |
| BEG-014-EX04 | 4 | Draw the call stack at the deepest point of a() calling b() calling c(). |
| BEG-014-EX05 | 5 | Write a recursive sumOfDigits(int n). |
| BEG-014-EX06 | 5 | Write 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 theStringversion; 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 inStackOverflowError. - 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.
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).