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.

second program passes an unspecified number of integers delimited by one space<br />

in a string <strong>to</strong> the main method and displays their <strong>to</strong>tal. Name the two programs<br />

Exercise9_14a and Exercise9_14b, as shown in Figure 9.15.<br />

<strong>Programming</strong> Exercises 365<br />

FIGURE 9.15<br />

The program adds all the numbers passed from the <strong>com</strong>mand line.<br />

*9.15 (Find the number of uppercase letters in a string) Write a program that passes a<br />

string <strong>to</strong> the main method and displays the number of uppercase letters in the string.<br />

Comprehensive<br />

**9.16 (Implement the String class) The String class is provided in the <strong>Java</strong> library.<br />

Provide your own implementation for the following methods (name the new class<br />

MyString1):<br />

public MyString1(char[] chars);<br />

public char charAt(int index);<br />

public int length();<br />

public MyString1 substring(int begin, int end);<br />

public MyString1 <strong>to</strong>LowerCase();<br />

public boolean equals(MyString1 s);<br />

public static MyString1 valueOf(int i);<br />

**9.17 (Guess the capitals) Write a program that repeatedly prompts the user <strong>to</strong> enter a<br />

capital for a state. Upon receiving the user input, the program reports whether the<br />

answer is correct. Assume that 50 states and their capitals are s<strong>to</strong>red in a twodimensional<br />

array, as shown in Figure 9.16. The program prompts the user <strong>to</strong><br />

answer all states’ capitals and displays the <strong>to</strong>tal correct count. The user’s answer is<br />

not case-sensitive.<br />

Alabama<br />

Montgomery<br />

Alaska<br />

Juneau<br />

Arizona<br />

Phoenix<br />

... ...<br />

... ...<br />

FIGURE 9.16<br />

A two-dimensional array s<strong>to</strong>res states and their capitals.<br />

Here is a sample run:<br />

What is the capital of Alabama? Mon<strong>to</strong>gomery<br />

The correct answer should be Montgomery<br />

What is the capital of Alaska? Juneau<br />

Your answer is correct<br />

What is the capital of Arizona? ...<br />

...<br />

The correct count is 35

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

Saved successfully!

Ooh no, something went wrong!