|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.ObjectBearGame
public class BearGame
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.
| 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 |
|---|
public BearGame()
| Method Detail |
|---|
public static void main(java.lang.String[] args)
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.
public static int intQuery(java.util.Scanner input,
java.lang.String prompt)
prompt - a prompt to printinput - a Scanner to read input from
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
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.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||