Exercises - SIUE Computer Science

Exercises: 1. What output will be produced by the following code? public class
Demo {. public static void main(String[] args) {. System.out.println("The output is:"
); ... This code is in Demo1.java. ..... 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.

Part of the document


Exercises:


1. What output will be produced by the following code?
public class Demo {
public static void main(String[] args) {
System.out.println("The output is:");
foo(23);
System.out.println();
}
public static void foo(int number) {
if (number > 0) {
foo(number / 2);
System.out.print(number % 2);
}
}
}


|Solution: |
| |
|The output is: |
|10111 |
| |
|This code is in Demo1.java. |


2. What output will be produced by the following code?
public class Demo {
public static void main(String[] args) {
System.out.println("The output is:");
bar(11156);
System.out.println();
}
public static void bar(int number) {
if (number > 0) {
int d = number % 10;
boolean odd = (number / 10) % 2 == 1;
bar(number / 10);
if (odd)
System.out.print(d / 2 + 5);
else
System.out.print(d / 2);
}
}
}


|Solution: |
| |
|The output is: |
|05578 |
| |
|This code is in Demo2.java. |

3. Write a recursive method that will compute the number of odd digits in a
number.

|Solution: |
| |
|public static long countOdd(long number){ |
|long result; |
|if(number == 0) |
|// base case |
|result = 0; |
|else { |
|long digit = number % 10; |
|if(digit < 0) |
|digit = -1 * digit; |
|if(digit % 2 == 1) |
|result = countOdd(number/10) + 1; |
|else |
|result = countOdd(number/10); |
|} |
| |
|return result; |
|} |
| |
|This code is in Methods.java. |

4. Write a recursive method that will compute the sum of the digits in a
positive number.

|Solution: |
| |
|public static long sumDigits(long number){ |
|long result; |
|if(number == 0) |
|// base case |
|result = 0; |
|else { |
|long digit = number % 10; |
|if(digit < 0) |
|digit = -1 * digit; |
|result = digit + sumDigits(number/10); |
|} |
| |
|return result; |
|} |
| |
|This code is in Methods.java. |

5. Complete a recursive definition of the following method:
/**
Precondition: n >= 0.
Returns 10 to the power n.
*/
public static int computeTenToThe(int n)
Use the following facts about xn:
xn = (xn/2)2 when n is even and positive
xn = x (x(n - 1)/2)2 when n is odd and positive
x0 = 1

|Solution: |
| |
|/** |
|* Precondition: n >= 0. |
|* Returns 10 to the power n. |
|*/ |
|public static int tenToThe(int n){ |
|int result; |
| |
|if(n==0){ |
|result = 1; |
|} else { |
|result = tenToThe(n/2); |
|result = result * result; |
|if(n%2 == 1){ |
|// n is odd we need to square then multiply by 10 |
|result = result * 10; |
|} |
|} |
|return result; |
|} |
| |
|This code is in Methods.java. |


6. Write a recursive method that will compute the sum of all the values in
an array.

|Solution: |
| |
|public static int sumArray(int [] data){ |
|return sumArray(data, data.length-1); |
|} |
| |
|public static int sumArray(int [] data, int last){ |
|int result; |
| |
|if(last < 0) |
|result = 0; // only one value in the subarray |
|else{ |
|result = data[last] + sumArray(data, last-1); |
|} |
| |
|return result; |
| |
|} |
| |
|This code is in Methods.java. |

7. Write a recursive method that will find and return the largest value in
an array of integers. Hint: Split the array in half and recursively find
the largest value in each half. Return the larger of those two values.

|Solution: |
| |
|public static int max(int [] data){ |
|return max(data, 0, data.length-1); |
|} |
| |
|public static int max(int [] data, int first, int last){ |
|int result; |
| |
|if(first == last) |
|result = data[first]; // only one value in the subarray |
|else{ |
|int mid = (last + first)/2; |
|int max1 = max(data, first, mid); |
|int max2 = max(data, mid+1, last); |
|if(max1 > max2) |
|result = max1; |
|else |
|result = max2; |
|} |
| |
|return result; |
| |
|} |
| |
|This code is in Methods.java.. |

8. W