AP CSA 1.10 Debugging: Ignoring What Came Back

Unit 1: Using Objects and Methods · Lesson 1.10 · Debugging

Ignoring What Came Back

Calling Class Methods. This is not a blank editor: it is someone else's attempt. Find what is wrong, fix it, and submit to be graded against hidden test cases.

Why debugging is its own skill

A method that returns a value has done nothing useful until somebody catches it. Call it as a bare statement and the answer is computed, handed back, and dropped, leaving the original variable exactly as it was.

What is wrong with it

  1. The call to twice is written as a bare statement, so the doubled value is computed and discarded and the variable never changes. Assign the result back.
  2. The call to clamp passes its arguments but its return value is printed without ever being used to update the value, so the later line prints the unclamped number. Store the result before printing it.
  3. Both method bodies are correct. Everything to fix is at the call sites.

The program reads

A value, then a maximum.

The program should print

Three lines: the value doubled, that doubled value clamped to the maximum, and the clamped value printed again from the variable.

Worked examples

These show what the FIXED program should print. There are more cases you cannot see, and they use different values, so patching around just these numbers will fail.

Example 1 input
5 100
Example 1 output
10
10
10
Example 2 input
60 100
Example 2 output
120
100
100

Your answer

Main.java

Input for the Run button

Run sends whatever is in the code box below, bugs and all, so you can see the crash or the wrong answer for yourself before you fix anything.


  

  

Stuck?

Hint 1

Passing an int to a method passes a COPY of it. Nothing the method does to its parameter can reach the variable you passed in, so the only way the answer gets back to you is the return value.

Hint 2

twice(value); on its own line is legal Java and does real work whose result nobody keeps. If a method returns something and you are not storing or printing it, ask yourself why you called it.

Hint 3

The third printed line must come from the variable, not from another call, which is how the exercise checks that you actually stored the clamped result.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]