Practice Programs

Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.

  1. Write a static recursive method that returns the number of digits in the integer passed to it as an argument of type int. Allow for both positive and negative arguments. For example, −120 has three digits. Do not count leading zeros. Embed the method in a program, and test it.

  2. Write a static recursive method that returns the sum of the integers in the array of int values passed to it as a single argument. You can assume that every indexed variable of the array has a value. Embed the method in a test program.

  3. One of the most common examples of recursion is an algorithm to calculate the

Get Java: An Introduction to Problem Solving and Programming, 8th Edition 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.