Class BearGame

java.lang.Object
  extended by BearGame

public class BearGame
extends java.lang.Object

The 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 Summary
BearGame()
           
 
Method Summary
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 bears method.
static boolean query(java.util.Scanner input, java.lang.String prompt)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BearGame

public BearGame()
Method Detail

main

public static void main(java.lang.String[] args)
The main method interactively gets information from the user to activate the bears method. It then prints a message indicating whether the game's goal can be reached. The String argument (args) is not used in this implementation.


intQuery

public static int intQuery(java.util.Scanner input,
                           java.lang.String prompt)
Print a prompt, then read and return an integer.

Parameters:
prompt - a prompt to print
input - 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.

query

public static boolean query(java.util.Scanner input,
                            java.lang.String prompt)

bears

public static boolean bears(int initial,
                            int goal,
                            int increment,
                            int n)
Determines whether the goal can be reached in the Teddy Bear game.

Parameters:
initial - the initial number of bears in the game
goal - the goal that must be reached to win the game
increment - the number of new bears that you receive when you ask for more bears
n - 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.