Java Reference
In-Depth Information
second program passes an unspecified number of integers delimited by one space
in a string to the
main
method and displays their total. Name the two programs
Exercise9_14a
and
Exercise9_14b
, as shown in Figure 9.15.
F
IGURE
9.15
The program adds all the numbers passed from the command line.
*9.15
(
Find the number of uppercase letters in a string
) Write a program that passes a
string to the
main
method and displays the number of uppercase letters in the string.
Comprehensive
**9.16
(
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
MyString1
):
public
MyString1(
char
[] chars);
public char
charAt(
int
index);
public int
length();
public
MyString1 substring(
int
begin,
int
end);
public
MyString1 toLowerCase();
public boolean
equals(MyString1 s);
public static
MyString1 valueOf(
int
i);
**9.17
(
Guess the capitals
) Write a program that repeatedly prompts the user to enter a
capital for a state. Upon receiving the user input, the program reports whether the
answer is correct. Assume that
50
states and their capitals are stored in a two-
dimensional array, as shown in Figure 9.16. The program prompts the user to
answer all states' capitals and displays the total correct count. The user's answer is
not case-sensitive.
Alabama
Montgomery
Alaska
Juneau
Arizona
Phoenix
...
...
...
...
F
IGURE
9.16
A two-dimensional array stores states and their capitals.
Here is a sample run:
What is the capital of Alabama?
The correct answer should be Montgomery
What is the capital of Alaska?
Your answer is correct
What is the capital of Arizona? ...
...
The correct count is 35
Montogomery
Juneau