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

Exercises 4 to 5 use following method: int puzzle (int base, int limit) if (base

ID: 3796047 • Letter: E

Question

Exercises 4 to 5 use following method: int puzzle (int base, int limit) if (base > limit) return -1; else if (base ==limit) return 1; else return base * puzzle (base + 1, limit).} Identity The base case (s) of the puzzle method the general case (s) of the puzzle method Constraints on the arguments passed to the puzzle method Show what would be written by the following calls to the recursive method puzzle. System. out. print1n (puzzle (14, 10)); System. out. print1n (puzzle (4, 7)); System. out. print1n (puzzle (0, 0));

Explanation / Answer

/*
a) base case:
when base is equal to limit return 1.

b) general case
return the multiplication of base and recursive call over the function with input
parameters limit and incrementing the value of base by 1

c) constraint is that limit should be greater than base, for the recursive
call to work, else the function return -1

*/

// JavaPuzzle.java
import java.util.Random;
import java.util.Scanner;

class JavaPuzzle
{
public static int puzzle(int base, int limit)
{
if(base > limit)
return -1;
else
{
if(base == limit)
return 1;
else
return base*puzzle(base + 1, limit);
}
}

public static void main(String args[])
{
// since base is greater than limit, return -1
System.out.println(puzzle(14,10));
//output: -1

// 1st recursive call, 4*puzzle(4+1,7)
// 2nd recursive call 4*5*puzzle(5+1,7)
// 3rd recursive call 4*5*6*puzzle(6+1,7)
// base case reached return 4*5*6*1 = 120
System.out.println(puzzle(4,7));
// output: 120

// base case since limit is equal to base, return 1
System.out.println(puzzle(0,0));
// output: 1
}
}   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote