AP CSA 4.7 Exercise 1: Parse It, Compare It, Bound It

AP CSA Hub Unit 4 4.1 â–¼ Lesson Ex 1 Ex 2 Quiz 4.2 â–¼ Lesson Ex 1 Ex 2 Quiz 4.3 â–¼ Lesson Ex 1 Ex 2 Quiz 4.4 â–¼ Lesson Ex 1 Ex 2 Quiz 4.5 â–¼ Lesson Ex 1 Ex 2 Quiz 4.6 â–¼ Lesson Ex 1 Ex 2 Quiz 4.7 â–¼ Lesson Ex 1 Ex 2 Quiz 4.8 â–¼ Lesson Ex 1 Ex 2 Quiz 4.9 â–¼ Lesson Ex 1 Ex 2 Quiz 4.10 â–¼ Lesson Ex 1 Ex 2 Quiz 4.11 â–¼ Lesson Ex 1 Ex 2 Quiz 4.12 â–¼ Lesson Ex 1 Ex 2 Quiz 4.13 â–¼ Lesson Ex 1 Ex 2 Quiz 4.14 â–¼ Lesson Ex 1 Ex 2 Quiz 4.15 â–¼ Lesson Ex 1 Ex 2 Quiz 4.16 â–¼ Lesson Ex 1 Ex 2 Quiz 4.17 â–¼ Lesson Ex 1 Ex 2 Quiz

Unit 4: Data Collections · Lesson 4.7 · Exercise 1

Parse It, Compare It, Bound It

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

Why this one is worth doing

Scanner.next() always hands back a String, even when the text is nothing but digits, so turning it into a number you can do arithmetic on is a step of its own. This exercise makes that step explicit and asks you to compare the results as Integer objects rather than with a bare > sign.

What to write

  1. Read an integer count n, then n more tokens as text. Convert each one to an int with Integer.parseInt and store the results in an int array.
  2. Print the sum of the values.
  3. Find the largest value by boxing each element with Integer.valueOf and comparing with compareTo, not with > or <. Print it.
  4. Print true when doubling the sum would overflow a 32 bit int, meaning it would exceed Integer.MAX_VALUE, and false otherwise. Use the Integer.MAX_VALUE constant; do not hardcode the number.
  5. Read one more token as text and convert it to a double with Double.parseDouble. Print it.

Your program reads

An integer count n, then n numeral tokens, then one more token that is a decimal number.

Your program prints

The sum, the largest value found by compareTo, the overflow check, and the parsed decimal.

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
4
3 -1 4 -1
2.5
Example 1 output
5
4
false
2.5
Example 2 input
1
0
3.14
Example 2 output
0
0
false
3.14

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

Integer.parseInt turns a String of digits into an int. Scanner.next() always returns a String, even when the text looks like a number, which is why the conversion is needed at all.

Hint 2

compareTo returns negative, zero or positive, not true or false. boxed.compareTo(largest) > 0 means boxed is the bigger of the two.

Hint 3

Cast the sum to long before doubling it, or the overflow check happens too late: an int that has already overflowed cannot tell you that it overflowed.

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]