Advertisement
Crenox

Word & Words Java Program

Jan 14th, 2015
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. // Name: Sammy Samkough
  2. // Prog: Word & Words
  3. // Spec: Complete methods for a Word and Words Class that consists of an ArrayList of Word objects.
  4.  
  5. public class Word
  6. {
  7. private String word;
  8.  
  9. public Word()
  10. {
  11. word = "";
  12. }
  13.  
  14. public Word(String wrd)
  15. {
  16. word = wrd;
  17. }
  18.  
  19. public void setWord(String wrd)
  20. {
  21. word = wrd;
  22. }
  23.  
  24. public String getWord()
  25. {
  26. return word;
  27. }
  28.  
  29. public int getNumVowels()
  30. {
  31. int count = 0;
  32.  
  33. String vowels = "AEIOUaeiou";
  34.  
  35. for (int i = 0; i < word.length(); i++)
  36. {
  37. if (vowels.indexOf(word.charAt(i)) >= 0)
  38. {
  39. count++;
  40. }
  41. }
  42.  
  43. return count;
  44. }
  45.  
  46. public int getLength()
  47. {
  48. int length = word.length();
  49.  
  50. return length;
  51. }
  52.  
  53. public String toString()
  54. {
  55. return "" + word;
  56. }
  57. }
  58. -------------------------------------------------------------------------------------------------------------------------------
  59. // Name: Sammy Samkough
  60. // Prog: Word & Words
  61. // Spec: Complete methods for a Word and Words Class that consists of an ArrayList of Word objects.
  62.  
  63. public class WordRunner
  64. {
  65. public static void main(String[] args)
  66. {
  67. Word w = new Word("Word"); // vowels: 1 length: 4
  68. System.out.println("The word is: " + w);
  69. System.out.println("Number of vowels: " + w.getNumVowels());
  70. System.out.println("Length of word: " + w.getLength() + "\n");
  71.  
  72. w = new Word("Bananas"); // vowels: 3 length: 7
  73. System.out.println("The word is: " + w);
  74. System.out.println("Number of vowels: " + w.getNumVowels());
  75. System.out.println("Length of word: " + w.getLength() + "\n");
  76.  
  77. w = new Word("Alligator"); // vowels: 4 length: 9
  78. System.out.println("The word is: " + w);
  79. System.out.println("Number of vowels: " + w.getNumVowels());
  80. System.out.println("Length of word: " + w.getLength() + "\n");
  81. }
  82. }
  83. /*
  84. The word is: Word
  85. Number of vowels: 1
  86. Length of word: 4
  87.  
  88. The word is: Bananas
  89. Number of vowels: 3
  90. Length of word: 7
  91.  
  92. The word is: Alligator
  93. Number of vowels: 4
  94. Length of word: 9
  95.  
  96. Press any key to continue . . .
  97. */
  98. -------------------------------------------------------------------------------------------------------------------------------
  99. // Name: Sammy Samkough
  100. // Prog: Word & Words
  101. // Spec: Complete methods for a Word and Words Class that consists of an ArrayList of Word objects.
  102.  
  103. import java.util.Scanner;
  104. import java.util.ArrayList;
  105. import java.util.Collections;
  106.  
  107. public class Words
  108. {
  109. private ArrayList<Word> words;
  110.  
  111. public Words()
  112. {
  113. setWords("");
  114. }
  115.  
  116. public Words(String wordList)
  117. {
  118. words = new ArrayList<Word>();
  119. setWords(wordList);
  120. }
  121.  
  122. public void setWords(String wordList)
  123. {
  124. words = new ArrayList<Word>();
  125. Scanner sc = new Scanner(wordList);
  126.  
  127. while(sc.hasNext())
  128. {
  129. words.add(new Word(sc.next()));
  130. }
  131. }
  132.  
  133. /*public String getWords()
  134. {
  135. String wd = words.get();
  136.  
  137. return wd;
  138. }*/
  139.  
  140. // count how many characters there are in a word
  141. public int countWordsWithXChars(int size)
  142. {
  143. int count = 0;
  144.  
  145. for (Word wd : words)
  146. {
  147. if(wd.getLength() == size)
  148. {
  149. count++;
  150. }
  151. }
  152.  
  153. return count;
  154. }
  155.  
  156. // this method will remove all words with a specified size / length
  157. // this method will also return the sum of the vowels in all words removed
  158. public int removeWordsWithXChars(int size)
  159. {
  160. int count = 0;
  161.  
  162. for (int i = 0; i < words.size(); i++)
  163. {
  164. if (words.get(i).getLength() == size)
  165. {
  166. count += words.get(i).getNumVowels();
  167. words.remove(i);
  168. }
  169. }
  170.  
  171. return count;
  172. }
  173.  
  174. // count how many vowels there are in a word
  175. public int countWordsWithXVowels(int numVowels)
  176. {
  177. int count = 0;
  178.  
  179. for (Word wd : words)
  180. {
  181. if (wd.getNumVowels() == numVowels)
  182. {
  183. count++;
  184. }
  185. }
  186.  
  187. return count;
  188. }
  189.  
  190. public String toString()
  191. {
  192. return "" + words;
  193. }
  194. }
  195. -------------------------------------------------------------------------------------------------------------------------------
  196. // Name: Sammy Samkough
  197. // Prog: Word & Words
  198. // Spec: Complete methods for a Word and Words Class that consists of an ArrayList of Word objects.
  199.  
  200. import java.util.Scanner;
  201. import java.util.ArrayList;
  202. import java.util.Collections;
  203.  
  204. public class WordsRunner
  205. {
  206. public static void main(String args[])
  207. {
  208. Words test = new Words("one two three four five six seven alligator");
  209. System.out.println("The words are: " + test);
  210. System.out.println("word with 2 vowels = " + test.countWordsWithXVowels(2));
  211. System.out.println("word with 3 vowels = " + test.countWordsWithXVowels(3));
  212. System.out.println("word with 4 vowels = " + test.countWordsWithXVowels(4));
  213. System.out.println("word with 2 chars = " + test.countWordsWithXChars(2));
  214. System.out.println("word with 3 chars = " + test.countWordsWithXChars(3));
  215. System.out.println("word with 4 chars = " + test.countWordsWithXChars(4));
  216. System.out.println("word with 5 chars = " + test.countWordsWithXChars(5));
  217.  
  218. int vowelsRemoved = test.removeWordsWithXChars(3);
  219. System.out.println("\nafter removing words with 3 chars: \n" + test);
  220. System.out.println("\nnumber of vowels in the words removed == " + vowelsRemoved);
  221. System.out.print("--------------------------------------------------------------------------------");
  222.  
  223. /*----------------------------------------------------------------------------------------------------------------------------*/
  224.  
  225. test = new Words("fun fly four size times ten plus eight");
  226. System.out.println("The words are: " + test);
  227. System.out.println("word with 2 vowels = " + test.countWordsWithXVowels(2));
  228. System.out.println("word with 3 vowels = " + test.countWordsWithXVowels(3));
  229. System.out.println("word with 4 vowels = " + test.countWordsWithXVowels(4));
  230. System.out.println("word with 2 chars = " + test.countWordsWithXChars(2));
  231. System.out.println("word with 3 chars = " + test.countWordsWithXChars(3));
  232. System.out.println("word with 4 chars = " + test.countWordsWithXChars(4));
  233. System.out.println("word with 5 chars = " + test.countWordsWithXChars(5));
  234.  
  235. vowelsRemoved = test.removeWordsWithXChars(3);
  236. System.out.println("\nafter removing words with 3 chars: \n" + test);
  237. System.out.println("\nnumber of vowels in the words removed == " + vowelsRemoved);
  238. System.out.print("--------------------------------------------------------------------------------");
  239.  
  240. /*----------------------------------------------------------------------------------------------------------------------------*/
  241.  
  242. test = new Words("alligator chicken dog cat pig buffalo");
  243. System.out.println("The words are: " + test);
  244. System.out.println("word with 2 vowels = " + test.countWordsWithXVowels(2));
  245. System.out.println("word with 3 vowels = " + test.countWordsWithXVowels(3));
  246. System.out.println("word with 4 vowels = " + test.countWordsWithXVowels(4));
  247. System.out.println("word with 2 chars = " + test.countWordsWithXChars(2));
  248. System.out.println("word with 3 chars = " + test.countWordsWithXChars(3));
  249. System.out.println("word with 4 chars = " + test.countWordsWithXChars(4));
  250. System.out.println("word with 5 chars = " + test.countWordsWithXChars(5));
  251.  
  252. vowelsRemoved = test.removeWordsWithXChars(3);
  253. System.out.println("\nafter removing words with 3 chars: \n" + test);
  254. System.out.println("\nnumber of vowels in the words removed == " + vowelsRemoved);
  255. System.out.print("--------------------------------------------------------------------------------");
  256. }
  257. }
  258. /*
  259. The words are: [one, two, three, four, five, six, seven, alligator]
  260. word with 2 vowels = 5
  261. word with 3 vowels = 0
  262. word with 4 vowels = 1
  263. word with 2 chars = 0
  264. word with 3 chars = 3
  265. word with 4 chars = 2
  266. word with 5 chars = 2
  267.  
  268. after removing words with 3 chars:
  269. [two, three, four, five, seven, alligator]
  270.  
  271. number of vowels in the words removed == 3
  272. --------------------------------------------------------------------------------
  273. The words are: [fun, fly, four, size, times, ten, plus, eight]
  274. word with 2 vowels = 4
  275. word with 3 vowels = 0
  276. word with 4 vowels = 0
  277. word with 2 chars = 0
  278. word with 3 chars = 3
  279. word with 4 chars = 3
  280. word with 5 chars = 2
  281.  
  282. after removing words with 3 chars:
  283. [fly, four, size, times, plus, eight]
  284.  
  285. number of vowels in the words removed == 2
  286. --------------------------------------------------------------------------------
  287. The words are: [alligator, chicken, dog, cat, pig, buffalo]
  288. word with 2 vowels = 1
  289. word with 3 vowels = 1
  290. word with 4 vowels = 1
  291. word with 2 chars = 0
  292. word with 3 chars = 3
  293. word with 4 chars = 0
  294. word with 5 chars = 0
  295.  
  296. after removing words with 3 chars:
  297. [alligator, chicken, cat, buffalo]
  298.  
  299. number of vowels in the words removed == 2
  300. --------------------------------------------------------------------------------
  301. Press any key to continue . . .
  302. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement