Java Reference
In-Depth Information
*12.6
(
NumberFormatException
) Listing 6.8 implements the
hex2Dec(String
hexString)
method, which converts a hex string into a decimal number.
Implement the
hex2Dec
method to throw a
NumberFormatException
if the
string is not a hex string.
*12.7
(
NumberFormatException
) Write the
bin2Dec(String binaryString)
method to convert a binary string into a decimal number. Implement the
bin2Dec
method to throw a
NumberFormatException
if the string is not a
binary string.
*12.8
(
HexFormatException
) Exercise 12.6 implements the
hex2Dec
method to
throw a
NumberFormatException
if the string is not a hex string. Define
a custom exception called
HexFormatException
. Implement the
hex2Dec
method to throw a
HexFormatException
if the string is not a hex string.
*12.9
(
BinaryFormatException
) Exercise 12.7 implements the
bin2Dec
method
to throw a
BinaryFormatException
if the string is not a binary string.
Define a custom exception called
BinaryFormatException
. Implement the
bin2Dec
method to throw a
BinaryFormatException
if the string is not a
binary string.
*12.10
(
OutOfMemoryError
) Write a program that causes the JVM to throw an
OutOfMemoryError
and catches and handles this error.
Sections 12.10-12.12
**12.11
VideoNote
HexFormatException
(
Remove text
) Write a program that removes all the occurrences of a specified
string from a text file. For example, invoking
java Exercise12_11 John filename
removes the string
John
from the specified file. Your program should get the
arguments from the command line.
**12.12
(
Reformat Java source code
) Write a program that converts the Java source
code from the next-line brace style to the end-of-line brace style. For example,
the following Java source in (a) uses the next-line brace style. Your program
converts it to the end-of-line brace style in (b).
public class
Test
{
public class
Test {
public static void
main(String[] args) {
// Some statements
public static void
main(String[] args)
{
}
}
// Some statements
}
}
(a) Next-line brace style
(b) End-of-line brace style
Your program can be invoked from the command line with the Java source-
code file as the argument. It converts the Java source code to a new format. For
example, the following command converts the Java source-code file
Test.java
to the end-of-line brace style.
java Exercise12_12 Test.java
*12.13
(
Count characters, words, and lines in a file
) Write a program that will count
the number of characters, words, and lines in a file. Words are separated by
whitespace characters. The file name should be passed as a command-line
argument, as shown in FigureĀ 12.13.
Search WWH ::
Custom Search