19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Write a test program that prompts the user <strong>to</strong> enter a decimal number and displays<br />

its binary equivalent.<br />

*20.22 (Decimal <strong>to</strong> hex) Write a recursive method that converts a decimal number in<strong>to</strong><br />

a hex number as a string. The method header is:<br />

public static String decimalToHex(int value)<br />

Write a test program that prompts the user <strong>to</strong> enter a decimal number and displays<br />

its hex equivalent.<br />

*20.23 (Binary <strong>to</strong> decimal) Write a recursive method that parses a binary number as a<br />

string in<strong>to</strong> a decimal integer. The method header is:<br />

public static int binaryToDecimal(String binaryString)<br />

Write a test program that prompts the user <strong>to</strong> enter a binary string and displays<br />

its decimal equivalent.<br />

*20.24 (Hex <strong>to</strong> decimal) Write a recursive method that parses a hex number as a string<br />

in<strong>to</strong> a decimal integer. The method header is:<br />

public static int hexToDecimal(String hexString)<br />

Write a test program that prompts the user <strong>to</strong> enter a hex string and displays its<br />

decimal equivalent.<br />

**20.25 (String permutation) Write a recursive method <strong>to</strong> print all the permutations of a<br />

string. For example, for the string abc, the prin<strong>to</strong>ut is<br />

abc<br />

acb<br />

bac<br />

bca<br />

cab<br />

cba<br />

(Hint: Define the following two methods. The second is a helper method.)<br />

public static void displayPermutation(String s)<br />

public static void displayPermutation(String s1, String s2)<br />

The first method simply invokes displayPermutation(" ", s). The<br />

second method uses a loop <strong>to</strong> move a character from s2 <strong>to</strong> s1 and recursively<br />

invokes it with a new s1 and s2. The base case is that s2 is empty and prints s1<br />

<strong>to</strong> the console.<br />

Write a test program that prompts the user <strong>to</strong> enter a string and displays all its<br />

permutations.<br />

**20.26 (Create a maze) Write an applet that will find a path in a maze, as shown in<br />

Figure 20.13a. The maze is represented by an 8 * 8 board. The path must meet<br />

the following conditions:<br />

■ The path is between the upper-left corner cell and the lower-right corner<br />

cell in the maze.<br />

■ The applet enables the user <strong>to</strong> place or remove a mark on a cell. A path consists<br />

of adjacent unmarked cells. Two cells are said <strong>to</strong> be adjacent if they are<br />

horizontal or vertical neighbors, but not if they are diagonal neighbors.<br />

■ The path does not contain cells that form a square. The path in Figure<br />

20.13b, for example, does not meet this condition. (The condition makes a<br />

path easy <strong>to</strong> identify on the board.)<br />

<strong>Programming</strong> Exercises 763

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!