CS110 – Sample Exam 2 Page 1 of 8 CS110-Introduction to Computing with Java (Sections 1-2) Namita Singla Sample Exam 2 March 30, 2005 Name: _________________________________________ 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. CS110 – Sample Exam 2 Page 2 of 8 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 } CS110 – Sample Exam 2 Page 3 of 8 3) Draw UML diagram for TextLine.java. You only need to show following methods in the methods compartment. a. removeAllOccurrences b. appendToTextLine c. printChars d. isSentenceEnder 4) Describe how the author of the TextLine.java class used two important Java coding conventions. CS110 – Sample Exam 2 Page 4 of 8 5) 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. 6) Scope of Variables a. (5) What is the scope of the variable “newText” declared on line 72 of TextLine.java? b. (5) Line 73 of TextLine.java contains a declaration for variable “i”. What is the scope of this variable? CS110 – Sample Exam 2 Page 5 of 8 7) 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. 8) Consider the following loop: for (int count = 0; count < 7; count++) { System.out.println(2*count+1); } 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? CS110 – Sample Exam 2 Page 6 of 8 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 30. * 31. * @return number of characters in TextLine 32. */ 33. public int countChars() { 34. return text.length(); 35. } 36. 37. /** 38. * Sets TextLine to s 39. * 40. * @param t 41. * New text 42. */ 43. public void setTextLine(String t) { 44. text = t; 45. } 46. 47. /** 48. * Get text 49. * 50. * @return text 51. */ 52. public String getTextLine() { 53. return text; 54. } 55. CS110 – Sample Exam 2 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. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. Page 7 of 8 /** * 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) { 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 CS110 – Sample Exam 2 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. * the start of the range. * @param end * the end of the range. * */ public void printChars(int start, int end) { //method body } } Page 8 of 8