Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- *
- * @author
- */
- public class WordList extends ArrayList<String> {
- /**
- * method to returns the number of words in this WordList that are exactly len
- * letters long
- *
- * @param len
- * - length of the words to search for
- * @return number of words in this list of length len
- */
- public int numWordsOfLength(int len)
- {
- // to be implemented in part (a)
- int count = 0;
- for(int i = 0; i < this.size(); i++){
- if(this.get(i).length() == len){
- count++;
- }
- }
- return count;
- }
- /**
- * All words that are exactly len letters long are removed from this WordList,
- * with the order of the remaining words unchanged
- *
- * @param len
- * - length of the word to remove
- */
- public void removeWordsOfLength(int len) {
- // to be implemented in part (b)
- for(int i = 0; i < this.size(); i++){
- if(this.get(i).length() == len){
- remove(i);
- i--;
- }
- }
- }
- /**
- * method to calculate the length of the longest word
- *
- * @return length of the longest word
- */
- public int lengthOfLongestWord() {
- // to be implemented in part (c)
- String largestword = "";
- for(int i = 0; i < this.size(); i++){
- if(this.get(i).length() == largestword.length()){
- largestword = this.get(i);
- }
- }
- return largestword.length();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement