AP CSA 1.2 Debugging: The Wrong Container

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

The Wrong Container

Variables and Data Types. 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

An int cannot hold half of anything. Store a measurement that has a fractional part in one and the fraction is gone before you get a chance to print it, silently and permanently. Choosing the type is choosing what the program is able to remember.

What is wrong with it

  1. The average is declared as an int, so it throws away the fractional part of every measurement. Declare it as a double and compute it with double division so the fraction survives.
  2. The running total is reset to 0 in the middle of the additions, so only the last two values are counted. Remove the stray reset.
  3. The count and the reading loop are correct.

The program reads

Exactly four integer measurements.

The program should print

Two lines: the total of the four values, then their average as a double.

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
1 2 3 4
Example 1 output
10
2.5
Example 2 input
10 20 30 40
Example 2 output
100
25.0

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

Declaring average as a double is only half the fix. total / 4 is int divided by int, which truncates BEFORE the result is ever stored, so a double variable receives an already-damaged number. Divide by 4.0 to force real division.

Hint 2

A double prints with a decimal point, so 7 becomes 7.0 in the output. That is expected and correct here.

Hint 3

The reset in the middle is the kind of line that survives a copy and paste. Trace total after each statement and it is obvious where the first two values went.

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]