} package recursion; import java.util.Scanner; public class Recursion { // Class
ID: 3890410 • Letter: #
Question
}
package recursion; import java.util.Scanner; public class Recursion { // Class example public static int sum_up(int n) { if (n <=0 ) { return 0; } else { return n + sum_up(n-1); } } // Class example public static int sum_up_tail(int n) { return sum_up_tail(n, 0); } public static int sum_up_tail(int n, int tmp) { if (n <1 ) { return tmp; } else { return sum_up_tail(n-1, tmp+n); } } // Activity 4 int fact(int n) // Activity 4 int fib(int n) // Activity 4 int gcd(int num1, int num2) // Project 4 int power (int x, int y) // Project 4 balance(int x, int y) // Project 4 int Ackermann(int m, int n) // Project 4 playGuessingGame(int m) } import java.util.Scanner; import recursion.*; public class Activity4 { public static void main(String[] args) { import java.util.Scanner; import recursion.*; public class Project4 { public static void main(String[] args) { System.out.println("Recursion Tests:"); System.out.println(" Class examples:"); for (int ii=0;ii<10;ii++) { System.out.println("sum_up " + ii + ": " + Recursion.sum_up(ii)); } for (int ii=1;ii<10;ii++) { System.out.println("sum_up_tail " + ii + ": " + Recursion.sum_up_tail(ii));}
Programming Project - Recursion 1) Review the material about recursion 2) If you have not done so already, download the source code from https:/lgithub.com/CGCC-CS! 205activity4.git. The file Project4.java is a driver class that tests the methods from the activity & the methods below 3) Add the following methods to your Recursion class: A. A recursive Java method called power(x,y) to find x raised to the power of y. You can assume that y>=0. Note that your method must be recursive. You will receive no credit if you call Math.pow0. B. Add a recursive Java method called balance (x,y) to compute the floor of the average of x & y as follows: » If the two parameters are within 1 of each other, return the smaller number Otherwise, subtract one from the larger parameter and add one to the smaller parameter and return balance of those two numbers C. A recursive Java method called Ackermann (m,n) to compute the Ackerman function defined as follows: ifm=0 If m > 0 and n=0 A(m, n) = A(m-1, 1) A(m 1, A(m,n -1)) if m > 0 and n >0. The Ackermann function grows very quickly, so you may run into stack overflows when m>3. You can see a table of correct values here: https://en.wikipedia.org/wiki/ Ackermann function D. A method called playGuessingGame (m) that lets the user play a guessing game. The method will pick a random number between 0 & m and prompt the user enter a guess from 0 to m. If the user does not guess the number then they should be told whether their guess was higher or lower than the number then display a new range to choose from. Your playGuessingGame method should call a recursive helper method that prints the range the user should guess between. Uncomment all the test code in the Project4 driver class and include sample output for all the methods you wrote for the activity & project in your submission document. 4)Explanation / Answer
Given below is the completed class. Hope it helps.
package recursion;
import java.util.Random;
import java.util.Scanner;
public class Recursion {
Scanner keybd = new Scanner(System.in);
// Class example
public static int sum_up(int n) {
if (n <=0 ) {
return 0;
}
else {
return n + sum_up(n-1);
}
}
// Class example
public static int sum_up_tail(int n) {
return sum_up_tail(n, 0);
}
public static int sum_up_tail(int n, int tmp) {
if (n <1 ) {
return tmp;
}
else {
return sum_up_tail(n-1, tmp+n);
}
}
// Activity 4 int fact(int n)
// Activity 4 int fib(int n)
// Activity 4 int gcd(int num1, int num2)
// Project 4 int power (int x, int y)
public int power(int x, int y)
{
if(y <= 0) //base case
return 1;
else
return x * power(x, y-1);
}
// Project 4 balance(int x, int y)
public int balance(int x, int y)
{
if(x > y)
{
if(x - y <= 1) //diff is less than 1
return y;
else
return balance(x-1, y + 1); //increment smaller by 1 and decrment larger by 1
}
else //y is larger than x
{
if(y - x <= 1)
return x;
else
return balance(y-1, x +1);
}
}
// Project 4 int Ackermann(int m, int n)
public int Ackermann(int m, int n)
{
if(m == 0)
return n + 1;
else if(n == 0 && m > 0)
return Ackermann(m-1, 1);
else
return Ackermann(m-1, Ackermann(m, n-1));
}
// Project 4 playGuessingGame(int m)
public void playGuessingGame(int m)
{
Random random = new Random(System.currentTimeMillis());
int start = 1;
int end = m;
int guess = 1 + random.nextInt(end);
helper(guess, start, end);
}
private void helper(int guess, int start, int end)
{
int input;
if(end <= start)
return;
System.out.println("Guess a number between " + start + " and " + end );
input = keybd.nextInt();
if(guess == input)
System.out.println("You guesssed it!");
else if(guess < input)
{
System.out.println("Too high!");
helper(guess, start, input-1);
}
else
{
System.out.println("Too high!");
helper(guess, start, input-1);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.