AP CSA 1.8 Exercise 1: Precondition, Postcondition

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

Precondition, Postcondition

Documentation with Comments. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

The grader checks what your program prints, and a comment prints nothing. Write them anyway: the habit is what the exam rewards on the free response, where a stated precondition is the difference between a correct answer and a rejected one.

What to write

  1. Read three integers: a starting balance in cents, a deposit in cents, and a number of equal withdrawals.
  2. Above your code, write a comment block stating the precondition (the number of withdrawals is at least 1) and the postcondition (what the program will have printed).
  3. Print the balance after the deposit.
  4. Print the size of one withdrawal, using integer division of the new balance by the number of withdrawals.
  5. Print the cents left over after those withdrawals.

Your program reads

Three integers on separate lines: balance, deposit, withdrawals.

Your program prints

Three lines: the balance after the deposit, the size of one withdrawal, and the remainder.

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
500
250
4
Example 1 output
750
187
2
Example 2 input
0
100
3
Example 2 output
100
33
1

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

A precondition is what has to be true before your code runs for it to be correct. Here it is the one that stops a divide by zero.

Hint 2

The deposit lands before the withdrawals are worked out, so compute the new balance once and use it twice.

Hint 3

Everything is in whole cents, so integer division and % are the right tools and nothing needs to be a double.

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]