/
WordList.java
58 lines (47 loc) · 1.21 KB
/
WordList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
public class WordList {
private ArrayList<String> MyList;
public WordList() {
MyList = new ArrayList<String>();
//populate with random "words"
Random r = new Random();
for(int i=0; i < 10; i++) {
String s = "";
int numChars = r.nextInt( 5 ) + 1;
for(int j=0; j < numChars; j++ ) {
s+= (char)(r.nextInt(26) + 97);
}
MyList.add( s );
}
}
public String toString() {
return MyList.toString();
}
/**
* THESE ARE THE THREE METHODS YOU NEED TO WRITE FOR THE ASSIGNMENT!
*/
public int numWordsOfLength( int len ) {
int numWords = 0;
for (int i = 0; i < MyList.size(); i++)
if (MyList.get(i).length() == len)
numWords++;
return numWords;
}
public void removeWordsOfLength( int len ) {
int index = 0;
while (index < MyList.size()) {
if (MyList.get(index).length() == len)
MyList.remove(index);
else
index++;
}
}
//END OF ASSIGNMENT
public static void main(String[] args){
WordList wl = new WordList();
System.out.println( wl );
System.out.println("Words of length 3: " + wl.numWordsOfLength(3));
wl.removeWordsOfLength(3);
System.out.println("After removing: " + wl);
}
}