java 课后答案09reviewstringtextio

上传人:第*** 文档编号:31075527 上传时间:2018-02-04 格式:DOC 页数:7 大小:79.50KB
返回 下载 相关 举报
java 课后答案09reviewstringtextio_第1页
第1页 / 共7页
java 课后答案09reviewstringtextio_第2页
第2页 / 共7页
java 课后答案09reviewstringtextio_第3页
第3页 / 共7页
java 课后答案09reviewstringtextio_第4页
第4页 / 共7页
java 课后答案09reviewstringtextio_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《java 课后答案09reviewstringtextio》由会员分享,可在线阅读,更多相关《java 课后答案09reviewstringtextio(7页珍藏版)》请在金锄头文库上搜索。

1、Chapter 9 Strings and Text I/O1.s1 = s2 = trues2 = s3 = falses1.equals(s2) = trues2.equals(s3) = pareTo(s2) = pareTo(s3) = 0s1 = s4 = trues1.charAt(0) = Ws1.indexOf(j) = -1s1.indexOf(to) = 8s1.lastIndexOf(a) = 14s1.lastIndexOf(o, 15) = 9s1.length() = 16s1.substring(5) = me to Java!s1.substring(5, 11

2、) = me tos1.startsWith(Wel) = trues1.endsWith(Java) = trues1.toLowerCase() = welcome to java!s1.toUpperCase()= WELCOME TO JAVA! Welcome .trim() = Welcomes1.replace(o, T) = WelcTme tT Java!s1.replaceAll(o, T) = WelcTme tT Java!s1.replaceFirst(o, T) = WelcTme tT Java!s1.toCharArray() returns an array

3、of characters consisting of W, e, l, c, o, m, e, , t, o, , J, a, v, a(Note that none of the operation causes the contents of a string to change)2. String s = new String(new string);Answer: CorrectString s3 = s1 + s2;Answer: CorrectString s3 = s1 - s2;Answer: Incorrects1 = s2Answer: Correct s1 = s2An

4、swer: IpareTo(s2);Answer: Correctint i = s1.length();Answer: Correctchar c = s1(0);Answer: Incorrectchar c = s1.charAt(s1.length();Answer: Incorrect : its out of bounds, even if the preceding problem is fixed.3. The output isWelcome to JavaWelcabcme tabc JavaHint: No method in the String class can c

5、hange the content of the string. String is an immutable class.4. Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual.boolean isEqual = s1.equals(s2); Check whether s1 is equal to s2 ignoring case and assign the result to a Boolean variable isEqual.boolean isEqual = s1

6、.equalsIgnoreCase(s2); Compare s1 with s2 and assign the result to an int variable x.int x = pareTo(s2); Compare s1 with s2 ignoring case and assign the result to an int variable x.int x = pareToIgnoreCase(s2); Check whether s1 has prefix AAA and assign the result to a Boolean variable b.boolean b =

7、 s1.startsWith(AAA); Check whether s1 has suffix AAA and assign the result to a Boolean variable b.boolean b = s1.endsWith(AAA); Assign the length of s1 to an int variable x.int x = s1.length(); Assign the first character of s1 to a char variable x.char x = s1.charAt(0); Create a new string s3 that

8、combines s1 with s2.String s3 = s1 + s2; Create a substring of s1 starting from index 1.String s3 = s1.substring(1); Create a substring of s1 from index 1 to index 4.String s3 = s1.substring(1, 5); Create a new string s3 that converts s1 to lowercase.String s3 = s1.lowercase(); Create a new string s

9、3 that converts s1 to uppercase.String s3 = s1.uppercase(); Create a new string s3 that trims blank spaces on both ends of s1.String s3 = s1.trim(); Replace all occurrence of character e with E in s1 and assign the new string to s3.String s3 = s1.replaceAll(e, E); Split Welcome to Java and HTML into

10、 an array tokens using delimited by a space.String tokens = Welcome to Java and HTML.split( ); Assign the index of the first occurrence of character e in s1 to an int variable x.int x = s1.indexOf(e); Assign the index of the last occurrence of string abc in s1 to an int variable x.int x = s1.lastInd

11、exOf(“abc”);5. No.6. 0.7. Use the overloaded static valueOf method in the String class.8. The text is declared in Line 2 as a data field, but redeclared in Line 5 as a local variable. The local variable is assigned with the string passed to the constructor, but the data field is still null. In Line

12、10, test.text is null, which causes NullPointerException when invoking the toLowerCase() method.9. The constructor is declared incorrectly. It should not have void.10. A lowercase letter is between a and z. You can use the static isLowerCase(char) method in the Character class to test if a character

13、 is in lowercase. An uppercase letter is between A and Z. You can use the static isUpperCase(char) method in the Character class to test if a character is in uppercase.11. An alphanumeric character is between 0 and 9, or A and Z, or a and z. You can use the static isLetterOrDigit(char ch) method in

14、the Character class to test if a character is a digit or a letter.12. The StringBuilder class, introduced in JDK 1.5, is similar to StringBuffer except that the update methods in StringBuffer are synchronized.13. Use the StringBuilders constructor to create a string buffer for a string, and use the

15、toString method in StringBuilder class to return a string from a StringBuilder.14. StringBuilder sb = new StringBuilder(s); sb.reverse();s = sb.toString();15. StringBuilder sb = new StringBuilder(s); sb.delete(4, 10);s = sb.toString();16. Both string and string buffer use arrays to hold characters.

16、The array in a string is fixed once a string is created. The array in a string buffer may change if the buffer capacity is changed. To accommodate the change, a new array is created.17.(1) Java is fun(2) JavaHTML(3) Jais funva(4) JHTMLava(5) v(6) 4(7) Jav(8) Ja(9) avaJ(10) JComputera(11) av(12) va18.The outp

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号