CSIT114 – Sample Final Exam Page 1 of 7 CSIT114-Introduction to Java Namita Singla Sample Final Exam May 10, 2006 Name: Solution Attached you will find the source code for class TextLine.java. Use it to answer some of the questions that follow. In this fragment of TextLine.java (with line numbers) 1) 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. public void removeAllOccurrences(char c) { String newText = ""; for (int i = 0; i < countChars(); i++) { char nextChar = text.charAt(i); if (nextChar != c) { newText = newText + nextChar; } } text = newText; } Find a place on line 74 where a message is sent to an object. Identify the name and type of the object receiving the message, the file where you would you expect to find the code for the method that will respond to the message, and write the declaration for that method, showing its signature. Solution: text.charAt(i) is the place where message is sent to an object. The name of the object is text and type is String. We will find the code for the method charAt in String.java. The signature of the method is: public char charAt(int index) CSIT114 – Sample Final Exam Page 2 of 7 2) Write code at the //method body comment that will make this method do what its javadoc says it should do: /** * Print out a range of characters from text, one to a line. * * You can assume that both start and end are less than the * total number of characters in text. * * Example: If the text is "Hello" the message * printChars(2,4) should print * * l * l * o * * @param start * the start of the range. * @param end * the end of the range. * */ public void printChars(int start, int end) { //method body for (int i = start; i <= end; i++) { System.out.println(text.charAt(i)); } } CSIT114 – Sample Final Exam Page 3 of 7 3) Describe how the author of the TextLine.java class used two important Java coding conventions. Solution: 1. Each word in the name of class starts with capital letter. 2. public methods are well commented. 3. Name of variables and methods starts with lower letter. 4) Add to the TextLine class a method called reverse() to reverse the text. For e.g. if text is “Hello” (before calling this method) then it becomes “olleH” (after calling this method). This method takes no arguments and return type is void. Hint: Refer removeAllOccurrences method. public void reverse() { String newText = ""; for (int i = countChars() -1 ; i >=0 ; { char nextChar = text.charAt(i); newText = newText + nextChar; i++) } text = newText; } 5) Scope of Variables a. (5) What is the scope of the variable “newText” declared on line 72 of TextLine.java? CSIT114 – Sample Final Exam Page 4 of 7 The scope of variable “newText” is inside the removeAllOccurrences method. Or we can say that the scope is from line 72 to 79. b. (5) Line 73 of TextLine.java contains a declaration for variable “i”. What is the scope of this variable? The scope of the variable “i” is inside the for loop. Or we can say that the scope is from line 73 to 77. 6) Consider the following code fragment: int sum = 0; int i = 0; while (i < 5) { sum += i; i++; } System.out.print(sum); Replace the while loop in the fragment above with a for loop that prints the same value of sum variable. for(int i = 0; i < 5; i++) { sum += i; } CSIT114 – Sample Final Exam Page 5 of 7 7) Consider the following loop: Initialization Test Update for (int count = 0; count < 7; count++ ) { System.out.println(2*count+1); Body } Circle and label the 4 parts of a loop (initialization, loop test, loop update, loop body). What is printed to the console during execution of the above code? 1 3 5 7 9 11 13 1. /* 2. Author – Namita Singla TextLine.java This class provides a string 3. that can be edited by a variety of methods that are given below. 4. Date: March 29, 2005 5. */ 6. 7. public class TextLine { 8. 9. // Instance variable 10. private String text; 11. 12. 13. /** 14. * Constructor Sets text to t 15. * @param t 16. */ 17. public TextLine(String t) { 18. text = t; 19. } 20. 21. /** 22. * Static method Returns true if c is vowel 23. */ 24. public boolean isVowel(char c) { 25. return "aeiouAEIOU".indexOf(c) >= 0; 26. } 27. 28. /** 29. * Return number of characters in TextLine CSIT114 – Sample Final Exam 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. * * @return number of characters in TextLine */ public int countChars() { return text.length(); } /** * Sets TextLine to s * * @param t * New text */ public void setTextLine(String t) { text = t; } /** * Get text * * @return text */ public String getTextLine() { return text; } /** * Adds s to end of TextLine * * @param s */ public void appendToTextLine(String s) { text += s; } /** * Remove all occurrences of char c from text * * @param c * Char to remove from text */ public void removeAllOccurrences(char c) { String newText = ""; for (int i = 0; i < countChars(); i++) { char nextChar = text.charAt(i); if (nextChar != c) { newText = newText + nextChar; } } text = newText; } /** * Returns true if c is '.' or '!' or '?' * @return true if c is . or ! or ? */ public boolean isSentenceEnder(char c) { Page 6 of 7 CSIT114 – Sample Final Exam 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. Page 7 of 7 return ".!?".indexOf(c) >= 0; } /** * Clear TextLine */ public void clear() { text = ""; } /** * Print out a range of characters from text, one to a line. * * You can assume that both start and end are less than the * total number of characters in text. * * Example: If the text is "Hello" the message * printChars(2,4) should print * * l * l * o * * @param start * the start of the range. * @param end * the end of the range. * */ public void printChars(int start, int end) { //method body } }