Exercises

  1. Write the if statements that call different methods do0_9(), do10_99(), and do100_999() based on the value of i being in the range 0-9, 10-99, or 100-999. Don't forget to allow for other values of i, including negative values.

  2. Write a switch statement that corresponds to the if statement in question 1. Which is more compact? Which is easier to read?

  3. Write a method that counts the number of “1” bits in an int value. You can get successive bits from the end of an int by checking to see if the int is an odd number. An int is an even number if a division by two followed by a multiplication by two gives you back the original number (truncation of a fraction gives a smaller result for an odd number). If it is an odd number, the ...

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.