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

Write a recursive method public static int teddy(int initial, int goal, int incr

ID: 3778832 • Letter: W

Question

Write a recursive method public static int teddy(int initial, int goal, int increment) that simulates a variant of the “Teddy Bears” game between the player (you) and your friend, and returns the minimum number of steps you would need to end up with goal bears. You start with an initial number of stuffed bears. During each step of the game you may perform one of the following actions: (a) ask for and receive increment more bears from your friend; (b) give away increment of your bears to your friend; (c) if you have an even number of bears, you can give exactly half your bears to your friend; or (d) if you have an even number of bears, you can take an additional number of bears equal to half your current number of bears. If you fail to obtain the goal within 10 steps, the method returns -1. You may write helper methods if you like.

Method Call : Return Value

bears(10,4,2) : 2

bears(9,5,3) : -1

bears(15,4,2) : -1

bears(20,3,5) : -1

bears(40,5,6) : 3

bears(30,56,5) : -1

bears(30,55,4) : 3

Comment: Return value should be a number! Not a yes or no. Code should be in Java. Only the user takes turns, as demonstrated by the first case: 1. 10-2=8 2. 8/2=4.

Explanation / Answer

import java.util.Scanner;

public class ComputerBear

{

public static void main(String[ ] args)

{

Scanner stdins = new Scanner(System.in);

int initial, goal, increment, nVal;

System.out.println("Type the parameters for the game.");

do

{

initial = intQuery(stdins, "initial no. of bears: ");

goal = intQuery(stdins, "Goal no of bears: ");

increment = intQuery(stdins, "Increment for a-step: ");

nVal = intQuery(stdins, "Max no. of steps: ");

if ((initial < 0) || (goal < 0) || (increment < 0) || (nVal <0))

System.out.println("All the parameters given must be non-negative.");

else if (bearsV(initial, goal, increment, nVal))

System.out.println("Yes. The goal can be reached.");

else

System.out.println("There is no possibility for the goal to be reached.");

} while (queryCompute(stdins, "You can try other values if you would like to?"));

System.out.println("Thank you!!.");

}

public static int intQuery1(Scanner inputV1, String promptT1)

{

int ansR1;

do

{

System.out.print(promptT1);

try

{

ansR1 = inputV1.nextInt( );

return ansR1;

}

catch (Exception e)

{

System.out.print("There is an invalid response. Type an integer value: ");

}

} while (true);

}

public static boolean queryCompute(Scanner input, String promptS1)

{

String ansR1;

System.out.print(promptS1 + " [Y or N]: ");

answerR = input.nextLine( ).toUpperCase( );

while (!ansR1.startsWith("Y") && !ansR1.startsWith("N"))

{

System.out.print("Invalid response. Please type Y or N: ");

ansR1 = input.nextLine( ).toUpperCase( );

}

return ansR1.startsWith("Y");

}

public static boolean bearsV1(int initial, int goal, int increment, int n)

{

if (initial == goal)

return true;

else if (n == 0)

return false;

else if (bearsV(initial+increment, goal, increment, n-1))

return true;

else if ((initial % 2 == 0) && bearsV(initial/2, goal, increment, n-1))

return true;

else

return false;

}

}

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