Java Reference
In-Depth Information
14.4 (Comparing Portions of String s) Write an application that uses String method region-
Matches to compare two strings input by the user. The application should input the number of char-
acters to be compared and the starting index of the comparison. The application should state
whether the strings are equal. Ignore the case of the characters when performing the comparison.
14.5 (Random Sentences) Write an application that uses random-number generation to create
sentences. Use four arrays of strings called article , noun , verb and preposition . Create a sentence
by selecting a word at random from each array in the following order: article , noun , verb , preposi-
tion , article and noun . As each word is picked, concatenate it to the previous words in the sentence.
The words should be separated by spaces. When the final sentence is output, it should start with a
capital letter and end with a period. The application should generate and display 20 sentences.
The article array should contain the articles "the" , "a" , "one" , "some" and "any" ; the noun
array should contain the nouns "boy" , "girl" , "dog" , "town" and "car" ; the verb array should con-
tain the verbs "drove" , "jumped" , "ran" , "walked" and "skipped" ; the preposition array should
contain the prepositions "to" , "from" , "over" , "under" and "on" .
14.6 (Project: Limericks) A limerick is a humorous five-line verse in which the first and second lines
rhyme with the fifth, and the third line rhymes with the fourth. Using techniques similar to those de-
veloped in Exercise 14.5, write a Java application that produces random limericks. Polishing this ap-
plication to produce good limericks is a challenging problem, but the result will be worth the effort!
14.7 (Pig Latin) Write an application that encodes English-language phrases into pig Latin. Pig
Latin is a form of coded language. There are many different ways to form pig Latin phrases. For
simplicity, use the following algorithm:
To form a pig Latin phrase from an English-language phrase, tokenize the phrase into words
with String method split . To translate each English word into a pig Latin word, place the first
letter of the English word at the end of the word and add the letters “ay.” Thus, the word “jump”
becomes “umpjay,” the word “the” becomes “hetay,” and the word “computer” becomes “omputer-
cay.” Blanks between words remain as blanks. Assume the following: The English phrase consists of
words separated by blanks, there are no punctuation marks and all words have two or more letters.
Method printLatinWord should display each word. Each token is passed to method printLatin-
Word to print the pig Latin word. Enable the user to input the sentence. Keep a running display of
all the converted sentences in a text area.
14.8 (Tokenizing Telephone Numbers) Write an application that inputs a telephone number as a
string in the form (555) 555-5555 . The application should use String method split to extract the
area code as a token, the first three digits of the phone number as a token and the last four digits of
the phone number as a token. The seven digits of the phone number should be concatenated into
one string. Both the area code and the phone number should be printed. Remember that you'll have
to change delimiter characters during the tokenization process.
14.9 (Displaying a Sentence with Its Words Reversed) Write an application that inputs a line of
text, tokenizes the line with String method split and outputs the tokens in reverse order. Use space
characters as delimiters.
14.10 (Displaying String s in Uppercase and Lowercase) Write an application that inputs a line of
text and outputs the text twice—once in all uppercase letters and once in all lowercase letters.
14.11 (Searching String s) Write an application that inputs a line of text and a search character and
uses String method indexOf to determine the number of occurrences of the character in the text.
14.12 (Searching String s) Write an application based on the application in Exercise 14.11 that
inputs a line of text and uses String method indexOf to determine the total number of occurrences
of each letter of the alphabet in the text. Uppercase and lowercase letters should be counted together.
Store the totals for each letter in an array, and print the values in tabular format after the totals have
been determined.
 
Search WWH ::




Custom Search