Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LAB 1b: Triple Call Counter [ TripleCallCounter.java ] 15 points. The following

ID: 3751704 • Letter: L

Question

LAB 1b: Triple Call Counter [ TripleCallCounter.java ] 15 points.


The following recursive equation:


T(n) = T(n - 1) + T(1) + T(n - 1)


with the following base case conditions:


T(0) = 0


T(1) = 1


has a programming solution that works very similar to the structure of the solveTowers() algorithm from 1a. It too has three recursive calls, and a base case that performs a single command when it is used.


The actual solution to this recursive equation is the found in the following way:


T(2) = T(2 - 1) + T(1) + T(2 - 1) = 1 + 1 + 1


T(3) = T(3 - 1) + T(1) + T(3 - 1) = T(2) + T(1) + T(2) = 3 + 1 + 3


T(4) = T(4 - 1) + T(1) + T(4 - 1) = T(3) + T(1) + T(3) = 7 + 1 + 7


T(5) = T(5 - 1) + T(1) + T(5 - 1) = T(4) + T(1) + T(4) = 15 + 1 + 15

Write a method called solveTripleCounter() that takes a single integer parameter that is greater than or equal to 0 and returns the value of the formula for T(n) using a recursive calling and programming logic structure like that used in 1a. solveTrippleCounter() should return an int value that is equal to the value of T(n). solveTripleCounter() should be included inside a class you provide named TripleCallCounter and should have a public static void main() method. The main() method should be used to pass the value of the parameter needed to solveTripleCounter(), and should end after the solveTripleCounter() once method has completed. Use hardcoded values for a single integer value used in main() that is passed as the parameter to solveTripleCounter().

As a part of the comments for solveTripleCounter() provide a comment that explains what the algebraic equation is for n. That is, write the regular, non-recursive, equation that shows how T(n) is related to the value of n.  Hint: the equation has as a number raised to the power of n in it, and also a subtraction of a constant number in it.

Explanation / Answer

public class TripleCallCounter{

static int solveTripleCounter(int n){

if (n == 0 || n == 1)

return n;

else

return (2*solveTripleCounter(n-1) + 1);

}

public static void main(String[] args){

int n, result = 0;

Scanner scanner = new Scanner(System.in);

n = scanner.nextInt();

result = solveTripleCounter(n);

System.out.println("T(n) = " + result);

}

}