Lab 13: Reward Calculator
Methods calling methods, the call stack, and pass-by-value
Learning Objectives
After completing this lab, you will be able to:
- Compose methods by having one method call another
- Trace the call stack to understand execution order
- Explain pass-by-value and why modifying a parameter does not affect the caller
- Overload methods to provide convenient default-parameter behavior
What You’re Building
A quest reward calculator for a simple RPG scenario. Players complete quests that award XP (experience points) and gold. The gold is split among party members. You will write methods that call other methods, building up a questSummary method that composes all the smaller pieces.
Concepts and Common Misconceptions
| Concept | What students get wrong |
|---|---|
| Method composition | Duplicating logic instead of calling an existing method. If calculateXP(int level, int difficulty) exists, the overloaded calculateXP(int level) should call it, not rewrite the formula. |
| Call stack | Not understanding that each method call creates a new frame. When questSummary calls calculateXP, the questSummary frame pauses until calculateXP returns. |
| Pass-by-value | Expecting that void doubleXP(int xp) { xp = xp * 2; } changes the caller’s variable. It does not. Primitives are copied. |
| Return vs. modify | Writing a void method and trying to “send back” a value by modifying a parameter. Use return instead. |
Checkpoint 1: calculateXP and goldPerMember (1 pt)
Write two static methods:
calculateXP(int baseLevel, int difficulty)– returnbaseLevel * difficulty * 10.goldPerMember(int totalGold, int partySize)– returntotalGold / partySize(integer division).
Test from main:
System.out.println(calculateXP(5, 3)); // 150
System.out.println(goldPerMember(100, 4)); // 25
Debugging tip: If goldPerMember(100, 3) returns 33 instead of 33.33, that is correct – integer division truncates. The autograder expects integer division here.
Checkpoint 2: Overloaded calculateXP (1 pt)
Add an overloaded version: calculateXP(int baseLevel) that calls the two-parameter version with a default difficulty of 1.
public static int calculateXP(int baseLevel) {
return calculateXP(baseLevel, 1);
}
This pattern is how Java simulates default parameters. The one-parameter version delegates to the two-parameter version.
Also add bonusXP(int xp, double multiplier) that returns (int)(xp * multiplier). This will be used in Checkpoint 3.
Debugging tip: If the overloaded call causes a stack overflow, you are accidentally calling the one-parameter version from itself instead of the two-parameter version. Check your argument count.
Checkpoint 3: questSummary (1 pt)
Write questSummary(int level, int difficulty, int totalGold, int partySize, double bonusMultiplier) that composes all previous methods and prints a formatted summary:
Quest Complete!
XP earned: 150
Bonus XP: 225
Gold per member: 25
Inside questSummary:
- Call
calculateXP(level, difficulty)to get base XP. - Call
bonusXP(baseXP, bonusMultiplier)to get the bonus amount. - Call
goldPerMember(totalGold, partySize)to get each member’s share. - Print all results.
Debugging tip: If your bonus XP looks wrong, check the cast. (int)(150 * 1.5) is 225, but (int) 150 * 1.5 casts 150 first (no effect) then multiplies, giving a double that does not compile as an int return.
How to Debug
- Test bottom-up. Verify
calculateXPandgoldPerMemberbefore writingquestSummary. - Trace the call stack on paper. Write down which method is active at each point. When
questSummarycallsbonusXP, which calls nothing else, the stack is:main -> questSummary -> bonusXP. - Demonstrate pass-by-value. Write a quick test: assign
int x = 10;, call a method that setsx = 99on its parameter, printxinmain. It is still 10. - Match the autograder output format exactly. The autograder checks string output, so
"XP earned: 150"must not have extra spaces or different capitalization.
Scoring Breakdown
| Component | Points |
|---|---|
| Checkpoint 1: calculateXP and goldPerMember | 1 |
| Checkpoint 2: Overloaded calculateXP + bonusXP | 1 |
| Checkpoint 3: questSummary composing all methods | 1 |
| Autograder correctness | 5 |
| Timeliness (on-time submission) | 2 |
| Total | 10 |