Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4. *
  5. * @author
  6. */
  7. public class WordList extends ArrayList<String> {
  8.  
  9. /**
  10. * method to returns the number of words in this WordList that are exactly len
  11. * letters long
  12. *
  13. * @param len
  14. * - length of the words to search for
  15. * @return number of words in this list of length len
  16. */
  17. public int numWordsOfLength(int len)
  18. {
  19. // to be implemented in part (a)
  20. int count = 0;
  21. for(int i = 0; i < this.size(); i++){
  22. if(this.get(i).length() == len){
  23. count++;
  24. }
  25. }
  26. return count;
  27. }
  28.  
  29. /**
  30. * All words that are exactly len letters long are removed from this WordList,
  31. * with the order of the remaining words unchanged
  32. *
  33. * @param len
  34. * - length of the word to remove
  35. */
  36. public void removeWordsOfLength(int len) {
  37. // to be implemented in part (b)
  38.  
  39. for(int i = 0; i < this.size(); i++){
  40. if(this.get(i).length() == len){
  41. remove(i);
  42. i--;
  43. }
  44. }
  45.  
  46. }
  47.  
  48. /**
  49. * method to calculate the length of the longest word
  50. *
  51. * @return length of the longest word
  52. */
  53. public int lengthOfLongestWord() {
  54. // to be implemented in part (c)
  55. String largestword = "";
  56. for(int i = 0; i < this.size(); i++){
  57. if(this.get(i).length() == largestword.length()){
  58. largestword = this.get(i);
  59. }
  60. }
  61. return largestword.length();
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement