AP CSA 1.10 Exercise 1: Called on the Class

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

Called on the Class

Calling Class Methods. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

A class method is called on the class name, with no object anywhere. Once you can spot that in the call, the difference from the next lesson stops being a memorisation problem.

What to write

  1. Read two integers, a and b.
  2. Print Integer.compare(a, b), which is negative, zero or positive.
  3. Print Math.floorDiv(a, b), which rounds toward negative infinity instead of toward zero.
  4. Print Math.floorMod(a, b) and notice how it differs from a % b when a is negative.
  5. Print a % b on the last line so the two are next to each other.

Your program reads

Two integers on separate lines: a, then b. b is never zero.

Your program prints

Four lines: the comparison, the floor division, the floor remainder and the ordinary 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
17
5
Example 1 output
1
3
2
2
Example 2 input
3
8
Example 2 output
-1
0
3
3

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

You never write new Math() or new Integer(). These are class methods, so the class name is the whole receiver.

Hint 2

Integer.compare returns -1, 0 or 1 rather than the difference, so do not expect a - b.

Hint 3

For positive a and b, floorDiv and / agree and floorMod and % agree. The negative cases are the ones worth reading twice.

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]