Exercises


[Page 344 (continued)]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 7.1

Explain the difference between the following pairs of terms:

  1. Unit indexing and zero indexing.

  2. Data structure and data type.

  3. StringBuffer and String.

  4. String and StringTokenizer.

  5. Declaring a variable and instantiating a String.

  6. A Font and a FontMetrics object.

Exercise 7.2

Fill in the blanks.

  1. When the first character in a string has index 0, this is known as________.

  2. A sequence of characters enclosed within quotes is known as a________.

Exercise 7.3

Given the String str with the value "to be or not to be that is the question," write Java expressions to extract each of the substrings shown below. For each substring, provide two sets of answers: one that uses the actual index numbers of the substringsfor example, the first "to" goes from 0 to 2and a second, more general solution that will also retrieve the substring from the following string "it is easy to become what you want to become." (Hint: In the second case, use length() and indexOf() along with substring() in your expressions. If necessary, you may use local variables to store intermediate results. The answer to (a) is provided as an example.)

  1. the first "to" in the string

    str.substring(0, 2)                                      // Answer 1 str.substring(str.indexOf("to"), str.indexOf("to") + 2)  // Answer 2 


  2. [Page 345]
  3. the last "to" in the string

  4. the first "be" in the string

  5. the last "be" in the string

  6. the first four characters in the string

  7. the last four characters in the string

Exercise 7.4

Identify the syntax errors in each of the following, assuming that s is the literal string "exercise":

  1. s.charAt("hello")

  2. s.indexOf(10)

  3. s.substring("er")

  4. s.lastIndexOf(er)

  5. s.length

Exercise 7.5

Evaluate each of the following expressions, assuming that s is the literal string "exercise":

  1. s.charAt(5)

  2. s.indexOf("er")

  3. s.substring(5)

  4. s.lastIndexOf('e')

  5. s.length()

Exercise 7.6

Write your own equalsIgnoreCase() method using only other String methods.

Exercise 7.7

Write your own String equality method without using the String class's equals() method. (Hint: Modify the precedes() method.)

Exercise 7.8

Even though Java's String class has a built-in toLowerCase() method, write your own implementation of this method. It should take a String parameter and return a String with all its letters written in lowercase.

Exercise 7.9

Write a method that converts its String parameter so that letters are written in blocks five characters long. For example, consider the following two versions of the same sentence:

Plain :   This is how we would ordinarily write a sentence. Blocked : Thisi showw ewoul dordi naril ywrit easen tence 


Exercise 7.10

Design and implement an applet that lets the user type a document into a TextArea and then provides the following analysis of the document: the number of words in the document, the number of characters in the document, and the percentage of words that have more than six letters.

Exercise 7.11

Design and write an applet that searches for single-digit numbers in a text and changes them to their corresponding words. For example, the string "4 score and 7 years ago" would be converted into "four score and seven years ago".

Exercise 7.12

A palindrome is a string that is spelled the same way backward and forward. For example, mom, dad, radar, 727 and able was i ere i saw elba are all examples of palindromes. Write a Java applet that lets the user type in a word or phrase and then determines whether the string is a palindrome.


[Page 346]
Exercise 7.13

Write a maze program that uses a string to store a representation of the maze. Write a method that accepts a String parameter and prints a two-dimensional representation of a maze. For example, the maze shown here, where O marks the entrance and exit, can be generated from the following string:

String: XX_XXXXXXXX__XXX_XXXX_XX____XXX_XX_XX_XXX____X____XXXXXXXX_X   0 XX XXXXXXX X  XXX XXX X XX    XX X XX XX XX X    X    0 XXXXXXXX X 


Exercise 7.14

Write a method that takes a delimited string to store a name and address from which you can print a mailing label. For example, if the string contains "Sam Penn:14 Bridge St.:Hoboken, NJ 01881", the method should print the label shown in the margin.

Sam Penn
14 Bridge St.
Hoboken, NJ 01881


Exercise 7.15

Design and implement an applet that plays Time Bomb with the user. Here's how the game works. The computer picks a secret word and then prints one asterisk for each letter in the word: * * * * *. The user guesses at the letters in the word. For every correct guess, an asterisk is replaced by a letter: * e * * *. For every incorrect guess, the time bomb's fuse grows shorter. When the fuse disappears, after, say, six incorrect guesses, the bomb explodes. Store the secret words in a delimited string and invent your own representation for the time bomb.

Exercise 7.16

Challenge: The global replace function is a string-processing algorithm found in every word processor. Write a method that takes three String arguments: a document, a target string, and a replacement string. The method should replace every occurrence of the target string in the document with the replacement string. For example, if the document is "To be or not to be, that is the question", and the target string is "be", and the replacement string is "see", the result should be, "To see or not to see, that is the question".

Exercise 7.17

Challenge: Design and implement an applet that plays the following game with the user. Let the user pick a letter between A and Z. Then let the computer guess the secret letter. For every guess the player has to tell the computer whether it's too high or too low. The computer should be able to guess the letter within five guesses. Do you see why?

Exercise 7.18

Challenge: A list is a sequential data structure. Design a List class that uses a comma-delimited Stringsuch as, "a,b,c,d,12,dog"to implement a list. Implement the following methods for this class:

void addItem( Object o );       // Use Object.toString() String getItem(int position); String toString(); void deleteItem(int position); void deleteItem(String item); int getPosition(String item); String getHead();               // First element List getTail();                 // All but the first element int length();                   // Number of items 



[Page 347]
Exercise 7.19

Challenge: Use a delimited string to create a PhoneList class with an instance method to insert names and phone numbers, and a method to look up a phone number when a user provides a person's name. Since your class will take care of looking things up, you don't have to worry about keeping the list in alphabetical order. For example, the following string could be used as such a directory:

mom:860-192-9876::bill g:654-0987-1234::mary lancelot:123-842-1100 


Exercise 7.20

Design and implement an applet or application that displays a multiline message in various fonts and sizes input by the user. Let the user choose from among a fixed selection of fonts, sizes, and styles.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net