AP CSA 3.9 Exercise 1: Returning Yourself

Unit 3: Class Creation · Lesson 3.9 · Exercise 1

Returning Yourself

The this Keyword. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

this is a reference to the object the method was called on, which means a method can hand the object back. Do that and calls chain, which is a real pattern and not a trick.

What to write

  1. Write a class named Robot with a private name and a private x position.
  2. The constructor takes a name and a starting x, and its parameters must have the SAME names as the instance variables, so this is required to tell them apart.
  3. move(dx) adds dx to x and returns this robot, so calls can be chained.
  4. rename(name) changes the name and returns this robot.
  5. describe returns the name, then an @ sign, then x, with no spaces.

Your program reads

The grader supplies the input. Your class does not read anything.

Your program prints

Your class prints nothing. The grader prints what your methods return.

The class the grader will call

Your answer must declare exactly these, spelled exactly like this. The grader creates the object and calls the methods itself, so a method under a different name is a method it cannot find.

class Robot
Robot(String name, int x)
Robot move(int dx)
Robot rename(String name)
String getName()
int getX()
String describe()

Do not write a class named Main and do not write a main method. The grader supplies its own.

Worked examples

These are the cases you can see. There are more you cannot, and they use different values, so an answer that prints these numbers as constants will fail.

Example 1 input
Ada 0 3 4 Bo
Example 1 output
Ada@0
Ada@7
7
Bo@7
Bo
true
Example 2 input
Zip 10 -10 0 Zip
Example 2 output
Zip@10
Zip@0
0
Zip@0
Zip
true

Your answer

Main.java

Input for the Run button

Run uses the sample main shown below, which is not the one that grades you. Submitting runs your class against the real cases, including hidden ones.

The sample main the Run button uses
public class Main {
  public static void main(String[] args) {
    Robot r = new Robot("Ada", 0);
    System.out.println(r.move(3).move(4).describe());
    System.out.println(r.rename("Bo").describe());
  }
}


  

  

Stuck?

Hint 1

this.name = name; assigns the parameter to the field. Writing name = name; assigns the parameter to itself and leaves the field empty.

Hint 2

return this; is what makes r.move(3).move(4) work: the first move hands back the same robot for the second one to be called on.

Hint 3

The last check compares references with ==, so returning a new Robot instead of this prints false.

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]