AP CSA 3.2 Debugging: The Getter With a Memory

Unit 3: Class Creation · Lesson 3.2 · Debugging

The Getter With a Memory

Impact of Program Design. 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

Calling a getter twice returns two different answers. Nothing changed in between, and no error is raised. A method that quietly modifies the object it was only supposed to report on is one of the hardest bugs to find, because the act of investigating it changes the result.

What is wrong with it

  1. getTotal adds the shipping charge into the running total every time it is called, so the second call returns a larger number than the first. Make getTotal compute and return the value WITHOUT changing any field.
  2. addItem is supposed to add one item price to the order. It also increments the item count by two instead of one. Fix the count.
  3. The constructor and getCount are correct.

The program reads

A shipping charge, then a count of items, then that many item prices.

The program should print

Four lines: the item count, the total, the total again, and the total a third time. All three totals must match.

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
500 3
100 200 300
Example 1 output
3
1100
1100
1100
Example 2 input
0 1
42
Example 2 output
1
42
42
42

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

The harness prints the total three times deliberately. A method that only reports should give the same answer every time it is asked. If the three lines climb, the method is not reporting, it is changing.

Hint 2

The fix is to compute the sum in the return statement instead of storing it. itemTotal should only ever be changed by addItem, which is the method whose job is to change it.

Hint 3

This distinction has a name worth knowing: a method that changes the object is a mutator, and one that only reports is an accessor. Mixing the two is what makes an object impossible to reason about.

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]