Lab 13 10 pts Week 5 Due May 1

Lab 13: Reward Calculator

Methods calling methods, the call stack, and pass-by-value

method-composition call-stack pass-by-value
Clone from GitHub
3 checkpoints 5 autograder 2 timeliness
Prerequisites: lesson-1-5 lesson-1-11

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) – return baseLevel * difficulty * 10.
  • goldPerMember(int totalGold, int partySize) – return totalGold / 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:

  1. Call calculateXP(level, difficulty) to get base XP.
  2. Call bonusXP(baseXP, bonusMultiplier) to get the bonus amount.
  3. Call goldPerMember(totalGold, partySize) to get each member’s share.
  4. 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

  1. Test bottom-up. Verify calculateXP and goldPerMember before writing questSummary.
  2. Trace the call stack on paper. Write down which method is active at each point. When questSummary calls bonusXP, which calls nothing else, the stack is: main -> questSummary -> bonusXP.
  3. Demonstrate pass-by-value. Write a quick test: assign int x = 10;, call a method that sets x = 99 on its parameter, print x in main. It is still 10.
  4. 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