Solutions to Self-Study Exercises


[Page 341 (continued)]

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. 0

  2. 1

  3. -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);




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