Write a recursive method public static int teddy(int initial, int goal, int incr
ID: 3780246 • 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 MUST BE A NUMBER!!! Not a yes or no.
Code should be in Java.
THIS IS NOT THE RIGHT ANSWER^^^
TeddyBear java 1 import j util. Scanner; 3 public class TeddyBear f public static void main(String[] args) Scanner stdin new Scanner (System. in int initial, goal, increment, n system out.printin ("Please type the following parameters for the game."); 10 do 11 initial intQuery (stdin Initial number of bears goal intQuery (stdin, Goal number of bears 13 increment intQuery (stdin Increment for an a-step: 14 n intQuery (stdin, Maximum number of steps 15 if ((initial 0) (goal 0) increment 0) (n 0)) 16 System.out.println("All parameters must be non-negative 17 else if (bears (initial, goal, increment, n)) 18 system. out.println("Yes. The goal can be reached."); 19 else 20 System. out.println("No. The goal cannot be reached."); 21 while (query (stdin, Do you want to try other values 23 System.out.println("Thank you beary much."); 24 25 26 27e public static int intQuery (Scanner input, String prompt) int answer 28 29 do f 30 System.out.print (prompt); 31 try 32 answer input.nextInt() 33 34 return answer catch (Exception e) 35 system out. rint ("Invalid response. Please type an integer value 36 37 while (true); 38 39 public static boolean query (Scanner input, String prompt) t 41 String answer; 42 43 System.out.print (prompt [Y or N answer input next Line().toUppercase 45 while answer startswith("Y") && answer startsWith ("N")) 46 system ("Invalid response Please type Y or N: 47 answer input nextLine .toUpperCase(); 49 50 return answer. startsWith("Y"); 51 52 53 R Problems Javadoc Declaration O Console Teddy Bear ava Application] C:Pro am FilesNavaydkl 80,101 exe (Dec 2, 2016, 5:14:07 AM Please type the following parameters for the game. of heExplanation / Answer
Answer:
import java.util.Scanner;
public class BearGameeCompute
{
public static void main(String[ ] args)
{
Scanner stdins = new Scanner(System.in);
int initialS, goalVal, incrementVal, nVal;
System.out.println("Please type the following parameters for the game.");
do
{
initialS = intQuery(stdins, "initialS number of bears: ");
goalVal = intQuery(stdins, "Goal number of bears: ");
incrementVal = intQuery(stdins, "Increment for an a-step: ");
nVal = intQuery(stdins, "Maximum number of steps: ");
if ((initialS < 0) || (goalVal < 0) || (incrementVal < 0) || (nVal <0))
System.out.println("All parameters must be non-negative.");
else if (bearsV(initialS, goalVal, incrementVal, nVal))
System.out.println("Yes. The goal can be reached.");
else
System.out.println("No. The goal cannot be reached.");
} while (queryCompute(stdins, "Do you want to try other values?"));
System.out.println("Thank you beary much.");
}
public static int intQuery(Scanner inputV, String promptT)
{
int answerR;
do
{
System.out.print(promptT);
try
{
answerR = inputV.nextInt( );
return answerR;
}
catch (Exception e)
{
System.out.print("Invalid response. Please type an integer value: ");
}
} while (true);
}
public static boolean queryCompute(Scanner inputVal, String promptS)
{
String answerR;
System.out.print(promptS + " [Y or N]: ");
answerR = inputVal.nextLine( ).toUpperCase( );
while (!answerR.startsWith("Y") && !answerR.startsWith("N"))
{
System.out.print("Invalid response. Please type Y or N: ");
answerR = inputVal.nextLine( ).toUpperCase( );
}
return answerR.startsWith("Y");
}
public static boolean bearsV(int initialS, int goalVal, int incrementVal, int n)
{
if (initialS == goalVal)
return true;
else if (n == 0)
return false;
else if (bearsV(initialS+incrementVal, goalVal, incrementVal, n-1))
return true;
else if ((initialS % 2 == 0) && bearsV(initialS/2, goalVal, incrementVal, n-1))
return true;
else
return false;
}
}
Sample Output:
C:jdk1.6in>java BearGameeCompute
Please type the following parameters for the game.
initialS number of bears: 10
Goal number of bears: 2
Increment for an a-step: 1
Maximum number of steps: 4
No. The goal cannot be reached.
Do you want to try other values? [Y or N]: Invalid response. Please type Y or N:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.