AP CSA 1.13 Exercise 1: Two Names, One Object

Unit 1: Using Objects and Methods · Lesson 1.13 · Exercise 1

Two Names, One Object

Object Creation and Storage (Instantiation). Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

A reference variable holds the address of an object, not the object. Copy the variable and you have two names for one thing, which is the single most useful fact in this unit and the one that surprises everybody.

What to write

  1. The Counter class is written for you. Do not change it.
  2. Read an integer start and an integer bump.
  3. Create one Counter with the starting value, then create a SECOND variable that refers to the SAME object.
  4. Call add(bump) through the first variable, then print the value read through the second variable.
  5. Now create a genuinely new Counter from the same starting value, call add(bump) on the first variable again, and print the new object value.

Your program reads

Two integers on separate lines: start, then bump.

Your program prints

Two lines: the value seen through the alias, and the value in the separately created object.

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
10
5
Example 1 output
15
10
Example 2 input
0
1
Example 2 output
1
0

Your answer

Main.java

Input for the Run button

Run sends whatever is in the input box below. Submitting runs your program against every test case, including hidden ones with different input.


  

  

Stuck?

Hint 1

Counter alias = first; does not build anything. It copies a reference, so both names point at one object.

Hint 2

Only the word new creates an object. Count the news in your answer: there should be exactly two.

Hint 3

The second printed line should be the starting value untouched, because add was never called on that object.

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]