Java Reference
In-Depth Information
Enter a string: 1800flowers
18003569377
*9.8
( Binary to decimal ) Write a method that parses a binary number as a string into a
decimal integer. The method header is:
public static int binaryToDecimal(String binaryString)
2 4
2 3
2 2
For example, binary string 10001 is 17
Therefore, binaryToDecimal("10001") returns 17 . Note that
Integer.parseInt("10001", 2) parses a binary string to a decimal value.
Do not use this method in this exercise.
Write a test program that prompts the user to enter a binary string and displays the
corresponding decimal integer value.
(1
*
+
0
*
+
0
*
+
0
*
2
+
1
=
17).
Section 9.4
**9.9
( Binary to hex ) Write a method that parses a binary number into a hex number.
The method header is:
public static String binaryToHex(String binaryValue)
Write a test program that prompts the user to enter a binary number and displays
the corresponding hexadecimal value.
**9.10
( Decimal to binary ) Write a method that parses a decimal number into a binary
number as a string. The method header is:
VideoNote
Number conversion
public static String decimalToBinary( int value)
Write a test program that prompts the user to enter a decimal integer value and dis-
plays the corresponding binary value.
**9.11
( Sort characters in a string ) Write a method that returns a sorted string using the
following header:
public static String sort(String s)
For example, sort("acb") returns abc .
Write a test program that prompts the user to enter a string and displays the sorted
string.
**9.12
( Anagrams ) Write a method that checks whether two words are anagrams. Two
words are anagrams if they contain the same letters in any order. For example,
silent and listen are anagrams. The header of the method is:
public static boolean isAnagram(String s1, String s2)
Write a test program that prompts the user to enter two strings and, if they are
anagrams, displays two strings are anagrams , and displays two strings
are not anagrams if they are not anagrams.
Section 9.5
*9.13
( Pass a string to check palindromes ) Rewrite Listing 9.1 by passing the string as a
command-line argument.
*9.14
( Sum integers ) Write two programs. The first program passes an unspecified num-
ber of integers as separate strings to the main method and displays their total. The
 
 
Search WWH ::




Custom Search