Skip to Content
Java How to Program, Early Objects, 11th Edition
book

Java How to Program, Early Objects, 11th Edition

by Paul J. Deitel, Harvey Deitel
June 2017
Beginner
1296 pages
69h 23m
English
Pearson
Content preview from Java How to Program, Early Objects, 11th Edition

Exercises

  1. 18.7 What does the following code do?

    1   public int mystery(int a, int b) {
    2      if (b == 1) {
    3         return a;
    4      }
    5      else {
    6         return a + mystery(a, b - 1);
    7      }
    8   }
    
  2. 18.8 Find the error(s) in the following recursive method, and explain how to correct it (them). This method should find the sum of the values from 0 to n.

     1   public int sum(int n) {
    2      if (n == 0) {
    3         return 0;
    4      }
    5      else {
    6         return n + sum(n);
    7      }
    8   }
    
  3. 18.9 (Recursive power Method) Write a recursive method power(base, exponent) that, when called, returns

    base exponent

    For example, power(3,4) =3*3*3*3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step should use the relationship

    base exponent=basebase exponent1

    and the terminating condition ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17

Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17

Kishori Sharan, Adam L. Davis
Learning Java, 5th Edition

Learning Java, 5th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck
Java in a Nutshell, 8th Edition

Java in a Nutshell, 8th Edition

Benjamin J. Evans, Jason Clark, David Flanagan

Publisher Resources

ISBN: 9780134751962