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.

364 Chapter 9 Strings<br />

Enter a string: 1800flowers<br />

18003569377<br />

*9.8 (Binary <strong>to</strong> decimal ) Write a method that parses a binary number as a string in<strong>to</strong> a<br />

decimal integer. The method header is:<br />

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

For example, binary string 10001 is 17 (1 * 2 4 + 0 * 2 3 + 0 * 2 2 + 0 * 2<br />

+ 1 = 17). Therefore, binaryToDecimal("10001") returns 17. Note that<br />

Integer.parseInt("10001", 2) parses a binary string <strong>to</strong> a decimal value.<br />

Do not use this method in this exercise.<br />

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

corresponding decimal integer value.<br />

Section 9.4<br />

**9.9 (Binary <strong>to</strong> hex) Write a method that parses a binary number in<strong>to</strong> a hex number.<br />

The method header is:<br />

public static String binaryToHex(String binaryValue)<br />

VideoNote<br />

Number conversion<br />

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

the corresponding hexadecimal value.<br />

**9.10 (Decimal <strong>to</strong> binary) Write a method that parses a decimal number in<strong>to</strong> a binary<br />

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

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

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

the corresponding binary value.<br />

**9.11 (Sort characters in a string) Write a method that returns a sorted string using the<br />

following header:<br />

public static String sort(String s)<br />

For example, sort("acb") returns abc.<br />

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

string.<br />

**9.12 (Anagrams) Write a method that checks whether two words are anagrams. Two<br />

words are anagrams if they contain the same letters in any order. For example,<br />

silent and listen are anagrams. The header of the method is:<br />

public static boolean isAnagram(String s1, String s2)<br />

Write a test program that prompts the user <strong>to</strong> enter two strings and, if they are<br />

anagrams, displays two strings are anagrams, and displays two strings<br />

are not anagrams if they are not anagrams.<br />

Section 9.5<br />

*9.13 (Pass a string <strong>to</strong> check palindromes) Rewrite Listing 9.1 by passing the string as a<br />

<strong>com</strong>mand-line argument.<br />

*9.14 (Sum integers) Write two programs. The first program passes an unspecified number<br />

of integers as separate strings <strong>to</strong> the main method and displays their <strong>to</strong>tal. The

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

Saved successfully!

Ooh no, something went wrong!