Taking an Integer Apart

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Why were the innermost set of parentheses used in the statement:

System.out.println("The original : " + (quotient*3 + remainder) );

Answer:

The parentheses force evaluation of the entire arithmetical expression (quotient*3 + remainder). Then the result is converted to characters and appended to the string. Without the parentheses, the subexpressions are evaluated independently, converted to characters, and appended to the string.

Taking an Integer Apart

The integer division operator / and the remainder operator % take an integer apart.

  theInteger / divisor arrow quotient

  theInteger % divisor arrow remainder

The original integer can be put back together again:

  quotient * divisor + remainder  arrow theInteger

In many calculations, it is convenient to do everything with integers, so both / and % are needed.

QUESTION 15:

If you exchange 372 pennies for dollar bills, how many bills do you get? How many pennies are left over?