Flylib.com

Books Software

 
 
 

Chapter Summary


[Page 340]

Chapter Summary

Technical Terms

baseline

concatenation

copy constructor

data structure

delimited string

delimiter

empty string

garbage collection

glyph

lexicographic order

logical font

off-by-one error

orphan object

physical font

read only

string

string index

string literal

token

unit indexed

zero indexed

Summary of Important Points

  • A String literal is a sequence of zero or more characters enclosed within double quotation marks. A String object is a sequence of zero or more characters, plus a variety of class and instance methods and variables .

  • A String object is created automatically by Java the first time it encounters a literal string , such as " Socrates " , in a program. Subsequent occurrences of the literal do not cause additional objects to be instantiated . Instead, every occurrence of the literal " Socrates " refers to the initial object.

  • A String object is created whenever the new operator is used in conjunction with a String() constructorfor example, new String("hello") .

  • The String concatenation operator is the overloaded + symbol; it is used to combine two String s into a single String : " hello " + " world " ==> " helloworld " .

  • String s are indexed starting at 0. The indexOf() and lastIndexOf() methods are used for finding the first or last occurrence of a character or substring within a String . The valueOf() methods convert a nonstring into a String . The length() method determines the number of characters in a String . The charAt() method returns the single character at a particular index position. The various substring() methods return the substring at particular index positions in a String .

  • The overloaded equals() method returns true if two String s contain the same exact sequence of characters. The == operator, when used on String s, returns true if two references designate the same String object.


  • [Page 341]
  • String objects are immutable . They cannot be modified.

  • A StringBuffer is a string object that can be modified using methods such as insert() and append() .

  • A StringTokenizer is an object that can be used to break a String into a collection of tokens separated by delimiters . The white space characterstabs, blanks, and new linesare the default delimiters.

  • The FontMetrics class is used to obtain the specific dimensions of the various Font s. It is useful when you wish to center text. Font s are inherently platform dependent. For maximum portability, it is best to use default fonts.



[Page 341 ( continued )]

Solutions to Self-Study Exercises

Solution 7.1

  1. silly

  2. silly

  3. silly stuff

Solution 7.2

  1. String str1 = "";

  2. String str2 = new String("stop");

  3. String str3 = str1 + str2;

Solution 7.3

  1. 15

  2. "551"

  3. "5175"

Solution 7.4

See Figure 7.25.

Figure 7.25. Answer to Exercise 7.4. Note that s1 is null because it has not been instantiated and has not been assigned a literal value.


Solution 7.5

  1. "45"

  2. "121"

  3. "X"


[Page 342]
Solution 7.6

  1. String.valueOf(100)

  2. String.valueOf('V');

  3. String s = new String(String.valueOf(X * Y));

Solution 7.7

  1. 1

  2. -1

Solution 7.8

  1. 16

  2. "16"

  3. 1

  4. 15

  5. 1

  6. 13

  7. 7

  8. 3

  9. 7

  10. 7

  11. 3

Solution 7.9

Evaluate the following expression:

String tricky = "abcdefg01234567";
tricky.indexOf(String.valueOf(tricky.indexOf("c")));
tricky.indexOf(String.valueOf(2));
tricky.indexOf("2");
Answer: 9


Solution 7.10

  1. "uvwxyz"

  2. "bcde"

  3. "xyz"

  4. "xy"

  5. "xyz"

Solution 7.11

  1. "uvwxyz"

  2. "bcde"

  3. "xyz"

  4. "xyz"

  5. "xyz"


[Page 343]
Solution 7.12

A class to test the string methods .


public class

StringProcessorTest {

public static void

main(String[] args) {
    KeyboardReader kb =

new

KeyboardReader();
    kb.prompt("Input a String or - stop - to quit: ");
    String str = kb.getKeyboardInput();

while

(!str.equals("stop")){
      kb.display("Testing printLetters()\n");
      StringProcessor.printLetters(str);
      kb.display("testing countChars()\n");
      kb.display("Total occurences of e = ");
      kb.display(StringProcessor.countChar(str,'e') + "\n");
      kb.display("Testing reverse()\n");
      kb.display(StringProcessor.reverse(str)+ "\n");
      kb.display("Testing capitalize()\n");
      kb.display(StringProcessor.capitalize(str) + "\n\n");
      kb.prompt("Input a String or  stop  to quit: ");
      str = kb.getKeyboardInput();
    }

// while

}

// main()

}

// StringProcessorTest class


Solution 7.13

Method to remove all blanks from a string:


// Pre: s is a non null string


// Post: s is returned with all its blanks removed


public

String removeBlanks(String s) {
  StringBuffer result =

new

StringBuffer();

for

(

int

k = 0; k < s.length(); k++)

if

(s.charAt(k) != ' ')

// If this is not a blank

result.append(s.charAt(k));

// append it to result


return

result.toString();
}


Solution 7.14

A Alpha Z Zero Zeroes a alpha bath bin z zero

Solution 7.15

To modify precedes so that it also returns true when its two string arguments are equal, just change the operator in the final return statement to <= :


if

(s1.charAt(k) <= s2.charAt(k) )

return true

;


Solution 7.16

  1. true

  2. true

  3. false

  4. false

  5. false

  6. true

  7. false

  8. false

  9. false


[Page 344]
Solution 7.17

The variables in TestStringEquals are declared static because they are used in static methods. Whenever you call a method directly from main() , it must be static because main() is static. Remember that static elements are associated with the class, not with its instances. So main() can only use static elements because they don't depend on the existence of instances.

Solution 7.18

  1. String s3 = s1.substring(s1.indexOf('n'))

    + s1.substring(0,s1.indexOf('n'));

  2. String s4 = s2.substring(6) + " " + s2.substring(0,5);

  3. String s5 = s2.substring(0,6) + s1.substring(0,3);