Guest User

Untitled

a guest
Jan 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. Assignment #7: Trimming Words
  2. Due: Wednesday, October 17th by 9:00 pm.
  3. Put a block comment at the beginning of the Java file that contains:
  4. /*
  5. Your first and last name
  6. Your section leader’s name
  7. Your section #, day, and time
  8. Assignment 7 - Problem 1, CSc 127A, Fall 2012
  9. Short description of what your program does
  10. */
  11. Include your partner’s name if you work with a partner on either or both programs. See the course syllabus for details on working with a partner. The partner’s name and section number are required to avoid an Academic Integrity violation.
  12. Grading Notes:
  13. Programs should compile without errors or warnings; those that do not compile will not receive a passing grade.
  14. For full credit, programs should produce the output shown below.
  15. Program: TrimmingWords
  16. Name your program: TrimmingWords.java.
  17. The program will ask the user for the name of a text file. The program will read the contents of the text file and print each of the words in the file, one word to a line. Saying the program should print words can be a tricky thing to do. The difficulty comes in removing characters that are not normally part of a word. Toward this goal, we are going to use the following rules about words:
  18. •First: A word consists of what the next() method of the Scanner class will return. The next() method returns a String that consists of all the characters in the input up to, but not including, the next “whitespace character”, where blank spaces, tabs, and newline characters are the whitespace characters.
  19. •Second: Look at the first character of the word. If this first character is a single-quotation mark, double-quotation mark, a dollar sign, or a period, remove it.
  20. •Third: Look at the last character of the word. If this last character is a single-quotation mark, double-quotation mark, semi-colon, colon, period, comma, hyphen, exclamation point, or question mark remove it.
  21. The second rule means you will remove at most one character from the front of the word.
  22. The third rule means you will remove at most one character from the end of the word.
  23. At the end of the output, print the number of times each character was removed. Finally, print the count of the number of words found.
  24. It is possible that there will be no word left after removing the indicated first and/or last characters. In this case, do not print the word, and do not count it as a word.
  25. You will find both while and do..while loops useful in this program.
  26. Required switch:
  27. You are required to use at least one switch statement in your solution. This one switch can be in either the code for checking the first character of the word or the code for checking the last character of the word. If you wish, it is okay to use switch statements in both places.
  28. What not to use:
  29. If you are familiar with for loops and/or arrays, do not use them on this program. They are not necessary, and the purpose is to gain practice using what has been covered in class to this point.
  30. Examples:
  31. There are (currently) nine test cases available on D2L. Two are shown below as examples. You are encouraged to look at all nine test cases. You are strongly encouraged to run all nine test cases and verify that your output agrees with the provided answers.
  32. As an example, consider the following excerpt from the Gettysburg Address, available as words04.txt on D2L:
  33. Four-score and seven years ago, our fathers brought forth on this
  34. continent a new nation, conceived in liberty and dedicated to the
  35. proposition that all men are created equal.
  36. The next() method may return String’s that have a comma or period as the last character. The third rule above will cause the comma or period to be removed before the word is printed. The output for the example would be:
  37. Enter the file name: words04.txt
  38. Do you want the words printed? (y/n) y
  39. Four-score
  40. and
  41. seven
  42. years
  43. ago
  44. our
  45. fathers
  46. brought
  47. forth
  48. on
  49. this
  50. continent
  51. a
  52. new
  53. nation
  54. conceived
  55. in
  56. liberty
  57. and
  58. dedicated
  59. to
  60. the
  61. proposition
  62. that
  63. all
  64. men
  65. are
  66. created
  67. equal
  68.  
  69. Double Quotes: 0
  70. Single Quotes: 0
  71. Semi-colons: 0
  72. Colons: 0
  73. Periods: 1
  74. Commas: 2
  75. Hyphens: 0
  76. Exclamation Points: 0
  77. Question Marks: 0
  78. Dollar Signs: 0
  79. Words found: 29
  80. Another example, this time using the following paragraph from Tom Sawyer, available as words02.txt on D2L:
  81. "Bother! Well, go 'long with you. I'd made sure you'd played hookey and
  82. been a-swimming. But I forgive ye, Tom. I reckon you're a kind of a singed
  83. cat, as the saying is -- better'n you look. This time."
  84. The answer:
  85. Enter the file name: words02.txt
  86. Do you want the words printed? (y/n) y
  87. Bother
  88. Well
  89. go
  90. long
  91. with
  92. you
  93. I'd
  94. made
  95. sure
  96. you'd
  97. played
  98. hookey
  99. and
  100. been
  101. a-swimming
  102. But
  103. I
  104. forgive
  105. ye
  106. Tom
  107. I
  108. reckon
  109. you're
  110. a
  111. kind
  112. of
  113. a
  114. singed
  115. cat
  116. as
  117. the
  118. saying
  119. is
  120. -
  121. better'n
  122. you
  123. look
  124. This
  125. time.
  126.  
  127. Double Quotes: 2
  128. Single Quotes: 1
  129. Semi-colons: 0
  130. Colons: 0
  131. Periods: 4
  132. Commas: 3
  133. Hyphens: 1
  134. Exclamation Points: 1
  135. Question Marks: 0
  136. Dollar Signs: 0
  137. Words found: 39
  138. Input files:
  139. Your program will ask the user two questions:
  140. •The first question will ask for the name of the text file.
  141. •The second question will ask if the individual words found in the file should be printed. See the example above.
  142. You can assume that the text file name entered is correct; we will not run tests that use an invalid file name.
  143. For the words being printed or not, your program should check that the first letter of the user’s response is either ‘y’, ‘Y’, ‘n’ or ‘N’. If the user’s response does not start with one of these four letters, repeat the question and read the user's response. Continue until the user does enter a word that begins with one of the four letters.
  144. Note that the user can type a single character, or can type a multi-letter word. If they type a word, it is the first letter of the word that determines whether words are printed or not. For example, the user can type any of
  145. y, Yes, yea, yell, Yellow
  146. and your program should then print the words.
Add Comment
Please, Sign In to add comment