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.

368 Chapter 9 Strings<br />

Enter a genome string:<br />

no gene is found<br />

TGTGTGTATAT<br />

*9.28 (Calcula<strong>to</strong>r) Revise Listing 9.5, Calcula<strong>to</strong>r.java, <strong>to</strong> accept an expression in which<br />

the operands and opera<strong>to</strong>r are separated by zero or more spaces. For example, 3+4<br />

and 3 + 4 are acceptable expressions.<br />

*9.29 (Business: check ISBN-13) ISBN-13 is a new standard for identifying books. It<br />

uses the 13 digits d 1 d 2 d 3 d 4 d 5 d 6 d 7 d 8 d 9 d 10 d 11 d 12 d 13 . The last digit, d 13 , is a checksum,<br />

which is calculated from the other digits using the following formula:<br />

10 - (d 1 + 3d 2 + d 3 + 3d 4 + d 5 + 3d 6 + d 7 + 3d 8 + d 9 + 3d 10 + d 11 + 3d 12 ) % 10<br />

If the checksum is 10, replace it with 0. Your program should read the input as a<br />

string. Here are sample runs:<br />

Enter the first 12 digits of an ISBN-13 as a string:<br />

978013213080<br />

The ISBN-13 number is 9780132130806<br />

Enter the first 12 digits of an ISBN-13 as a string:<br />

978013213079<br />

The ISBN-13 number is 9780132130790<br />

*9.30 (Capitalize first letter of each word ) Write the following method that returns a<br />

new string in which the first letter in each word is capitalized.<br />

public static void title(String s)<br />

Write a test program that prompts the user <strong>to</strong> enter a string and invokes this<br />

method, and displays the return value from this method. Here is a sample run:<br />

Enter a string: london england 2015<br />

The new string is: London England 2015<br />

Note that words may be separated by multiple blank spaces.<br />

*9.31 (Swap case) Write the following method that returns a new string in which the<br />

uppercase letters are changed <strong>to</strong> lowercase and lowercase letters are changed <strong>to</strong><br />

uppercase.<br />

public static String swapCase(String s)<br />

Write a test program that prompts the user <strong>to</strong> enter a string and invokes this<br />

method, and displays the return value from this method. Here is a sample run:<br />

Enter a string: I'm here<br />

The new string is: i'M HERE

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

Saved successfully!

Ooh no, something went wrong!