public class BearGame
extends java.lang.Object
BearGame Java application illustrates the use of
 the bears method that uses recursion to determine whether it is
 possible to win a silly game.
 Java Source Code for this class: http://www.cs.colorado.edu/~main/applications/BearGame.java
| Constructor and Description | 
|---|
| BearGame() | 
| Modifier and Type | Method and Description | 
|---|---|
| static boolean | bears(int initial,
     int goal,
     int increment,
     int n)Determines whether the goal can be reached in the Teddy Bear game. | 
| static int | intQuery(java.util.Scanner input,
        java.lang.String prompt)Print a prompt, then read and return an integer. | 
| static void | main(java.lang.String[] args)The main method interactively gets information from the user to
 activate the  bearsmethod. | 
| static boolean | query(java.util.Scanner input,
     java.lang.String prompt) | 
public static void main(java.lang.String[] args)
bears method. It then prints a message
 indicating whether the game's goal can be reached.args - not used in this implementationpublic static int intQuery(java.util.Scanner input,
                           java.lang.String prompt)
prompt - a prompt to printinput - a Scanner to read input from
 Postcondition:
   The prompt has been printed to System.out. Then an
   integer has been read and returned with intInputLine.public static boolean query(java.util.Scanner input,
                            java.lang.String prompt)
public static boolean bears(int initial,
                            int goal,
                            int increment,
                            int n)
initial - the initial number of bears in the gamegoal - the goal that must be reached to win the gameincrement - the number of new bears that you receive when you ask for more bearsn - the maximum number of steps permitted to win the game
 Precondition:
   All parameters should be non-negative.
 Postcondition:
   The method has determined whether it is possible to reach the goal in
   the following "Teddy Bear" game: In the game, your friend gives you a
   certain number of bears. The number of bears is called
   initial, and your goal is to end up with a particular
   number of bears, called the goal number. At any point in 
   the game you have two choices: (a) You can ask for (and receive) 
   increment more bears, or (b) if you have an even number of
   bears, then you may give half of them back to your friend. Each time you
   do (a) or (b), that is called a step in the game. The return value is
   true if and only if the goal can be reached in n steps
   or fewer.