AP CSA 3.9 Debugging: Assigning a Parameter to Itself

Unit 3: Class Creation · Lesson 3.9 · Debugging

Assigning a Parameter to Itself

this Keyword. 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

The setter looks completely reasonable and does absolutely nothing. When a parameter has the same name as a field, the bare name means the parameter, so the assignment copies the parameter onto itself and the field never changes.

What is wrong with it

  1. setName assigns name to name, which is the parameter to itself, leaving the field untouched. Use this to name the field explicitly on the left side.
  2. setAge has the same problem written a different way: it assigns the field to the parameter, backwards. Fix the direction and qualify the field with this.
  3. The constructor already uses this correctly. Use it as your model.

The program reads

A name, an age, then a replacement name and a replacement age.

The program should print

Four lines: the original name and age, then the name and age after both setters have been called.

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
Ada 36
Grace 45
Example 1 output
Ada
36
Grace
45
Example 2 input
Alan 41
Alan 41
Example 2 output
Alan
41
Alan
41

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

Inside a method, a bare name always means the closest thing with that name, and a parameter is closer than a field. this.name is the only way to say you meant the field. The compiler accepts name = name; because it is a legal, if pointless, statement.

Hint 2

Read every assignment as: the thing on the LEFT is being changed. In setAge the field is on the right, so the field is only being read. The parameter is what gets changed, and it disappears when the method returns.

Hint 3

The constructor in this class is already written correctly. Both setters should end up looking exactly like the lines inside it.

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]