Java Reference
In-Depth Information
Write a test program that prompts the user to enter a decimal number and dis-
plays its binary equivalent.
*20.22
(
Decimal to hex
) Write a recursive method that converts a decimal number into
a hex number as a string. The method header is:
public static
String decimalToHex(
int
value)
Write a test program that prompts the user to enter a decimal number and dis-
plays its hex equivalent.
*20.23
(
Binary to decimal
) Write a recursive method that parses a binary number as a
string into a decimal integer. The method header is:
public static int
binaryToDecimal(String binaryString)
Write a test program that prompts the user to enter a binary string and displays
its decimal equivalent.
*20.24
(
Hex to decimal
) Write a recursive method that parses a hex number as a string
into a decimal integer. The method header is:
public static int
hexToDecimal(String hexString)
Write a test program that prompts the user to enter a hex string and displays its
decimal equivalent.
**20.25
(
String permutation
) Write a recursive method to print all the permutations of a
string. For example, for the string
abc
, the printout is
abc
acb
bac
bca
cab
cba
(
Hint
: Define the following two methods. The second is a helper method.)
public static void
displayPermutation(String s)
public static void
displayPermutation(String s1, String s2)
The first method simply invokes
displayPermutation(" ", s)
. The
second method uses a loop to move a character from
s2
to
s1
and recursively
invokes it with a new
s1
and
s2
. The base case is that
s2
is empty and prints
s1
to the console.
Write a test program that prompts the user to enter a string and displays all its
permutations.
**20.26
(
Create a maze
) Write an applet that will find a path in a maze, as shown in
Figure 20.13a. The maze is represented by an
8
*
8
board. The path must meet
the following conditions:
The path is between the upper-left corner cell and the lower-right corner
cell in the maze.
■
The applet enables the user to place or remove a mark on a cell. A path con-
sists of adjacent unmarked cells. Two cells are said to be adjacent if they are
horizontal or vertical neighbors, but not if they are diagonal neighbors.
■
The path does not contain cells that form a square. The path in Figure
20.13b, for example, does not meet this condition. (The condition makes a
path easy to identify on the board.)
■