Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Integer Operations | Primitive Data Types
Java Data Types

Write a program that does the following:

  • Hardcode two integers in your program.
  • Add the two integers.
  • Print the sum to standard output.
  • If the sum overflows the range of a 32-bit signed integer, print Warning: Integer overflow instead of the sum.

The entry point is the main method of the Main class in the com.example package.

Notes:

  • Do not use any input functionality.
  • The valid range for a 32-bit signed integer is from -2147483648 to 2147483647.
  • Use the Integer.MAX_VALUE (maximum) and Integer.MIN_VALUE (minimum) constants to check the range boundaries.
  • It's recommended to perform the addition by casting to long first, to avoid overflow during the calculation itself, then compare the result against Integer.MAX_VALUE / Integer.MIN_VALUE.
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

Challenge: Integer Operations

Stryg for at vise menuen

Opgave

Swipe to start coding

Write a program that does the following:

  • Hardcode two integers in your program.
  • Add the two integers.
  • Print the sum to standard output.
  • If the sum overflows the range of a 32-bit signed integer, print Warning: Integer overflow instead of the sum.

The entry point is the main method of the Main class in the com.example package.

Notes:

  • Do not use any input functionality.
  • The valid range for a 32-bit signed integer is from -2147483648 to 2147483647.
  • Use the Integer.MAX_VALUE (maximum) and Integer.MIN_VALUE (minimum) constants to check the range boundaries.
  • It's recommended to perform the addition by casting to long first, to avoid overflow during the calculation itself, then compare the result against Integer.MAX_VALUE / Integer.MIN_VALUE.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt