AP CSA 1.9 Exercise 1: Match the Signature

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

Match the Signature

Method Signatures. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

A signature is a promise about what goes in and what comes out. Three of these four methods take a type you do not have yet, so each call needs the right value handed to it in the right form.

What to write

  1. Read a decimal number d, then an integer n.
  2. Call Math.round(double), which returns a long, on d and print what it gives back.
  3. Call Math.abs(int) on n and print the result.
  4. Call Math.pow(double, double) with n and 2 and print the result. Notice it returns a double even when the answer is whole.
  5. Call String.valueOf(double) on d and print its length() instead of the String itself.

Your program reads

A decimal number, then an integer, each on its own line.

Your program prints

Four lines: the rounded value, the absolute value, n squared as a double, and how many characters d takes to write.

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
3.5
4
Example 1 output
4
4
16.0
3
Example 2 input
2.25
-7
Example 2 output
2
7
49.0
4

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

Math.pow always returns a double, so 4 squared prints as 16.0 and not 16. That is the signature telling you something.

Hint 2

Math.round(double) returns a long. Printing it looks like an int, but the type is not the same thing as the printed form.

Hint 3

String.valueOf(d) makes a String, and length() is called on that String. You can write it as one chained expression.

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]