-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordList.java
More file actions
82 lines (74 loc) · 1.91 KB
/
WordList.java
File metadata and controls
82 lines (74 loc) · 1.91 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public class WordList {
String[] words;
int count;
public WordList() {
words = new String[2];
count = 0;
}
public int addWord(String x){
if (findWord(x) != -1){
return count;
}
if (count == words.length){
String[] newWords = new String[words.length*2];
for (int j=0; j<words.length; j++){
newWords[j] = words[j];
}
words = newWords;
}
words[count] = x;
return count++;
}
public void removeWord(String y){
if (findWord(y) == -1){
return;
}
else{
int a = findWord(y);
for (int k = a; k<count-1; k++){
words[k] = words [k+1];
}
count--;
words[count] = null;
return;
}
}
public int findWord(String z){
for(int i = 0; i < count; i++){
if (z.equals(words[i])){
return i;
}
}
return -1;
}
public boolean equals(WordList other){
if(count != other.count){
return false;
}
for(int c = 0; c<count; c++){
if(!words[c].equals(other.words[c])){
return false;
}
}
return true;
}
public String toString() {
String output = "There are " + count + " words in the word list:\n";
for (int i = 0; i < count; i++) {
output += words[i] + "\n";
}
return output;
}
public static void main(String[] args){
WordList w1 = new WordList();
w1.addWord("computer");
w1.addWord("abacus");
w1.addWord("computer");
w1.removeWord("computer");
System.out.println(w1);
WordList w2 = new WordList();
w1.equals(w2);
w2.addWord("abacus");
w1.equals(w2);
}
}