Java Reference
In-Depth Information
**9.18
(
Implement the
String
class
) The
String
class is provided in the Java library.
Provide your own implementation for the following methods (name the new class
MyString2
):
public
MyString2(String s);
public int
compare(String s);
public
MyString2 substring(
int
begin);
public
MyString2 toUpperCase();
public char
[] toChars();
public static
MyString2 valueOf(
boolean
b);
*9.19
(
Common prefix
) Write a method that returns the longest common prefix of two
strings. For example, the longest common prefix of
distance
and
disinfection
is
dis
. The header of the method is:
public static
String prefix(String s1, String s2)
If the two strings don't have a common prefix, the method returns an empty string.
Write a
main
method that prompts the user to enter two strings and displays their
longest common prefix.
9.20
(
Implement the
Character
class
) The
Character
class is provided in the Java
library. Provide your own implementation for this class. Name the new class
MyCharacter
.
**9.21
(
New string
split
method
) The
split
method in the
String
class returns an
array of strings consisting of the substrings split by the delimiters. However, the
delimiters are not returned. Implement the following new method that returns an
array of strings consisting of the substrings split by the matching delimiters,
including the matching delimiters.
public static
String[] split(String s, String regex)
For example,
split("ab#12#453", "#")
returns
ab
,
#
,
12
,
#
,
453
in an array
of
String
, and
split("a?b?gf#e", "[?#]")
returns
a
,
b
,
?
,
b
,
gf
,
#
, and
e
in an array of
String
.
**9.22
(
Implement the
StringBuilder
class
) The
StringBuilder
class is provided
in the Java library. Provide your own implementation for the following methods
(name the new class
MyStringBuilder1
):
public
MyStringBuilder1(String s);
public
MyStringBuilder1 append(MyStringBuilder1 s);
public
MyStringBuilder1 append(
int
i);
public int
length();
public char
charAt(
int
index);
public
MyStringBuilder1 toLowerCase();
public
MyStringBuilder1 substring(
int
begin,
int
end);
public
String toString();
**9.23
(
Financial: credit card number validation
) Rewrite Programming Exercise 5.31
using a string input for the credit card number. Redesign the program using the
following methods:
/** Return true if the card number is valid */
public static boolean
isValid(String cardNumber)
/** Get the result from Step 2 */
public static int
sumOfDoubleEvenPlace(String cardNumber)