blob: e93ef3d81e373002d1c35984d7244ee9b899a511 (
plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
import java.util.Random;
public class RecursionLLTester
{
public static void main(String[] args)
{
RecursionLinkedList rll = new RecursionLinkedList();
int point = 0;
boolean isError = false;
System.out.println("Create an empty RecursionLinkedList named rll.");
System.out.print("Test the method contains() on an empty RecursionLinkedList: ");
if(rll.contains(5) != false)
{
System.out.println("FAIL");
System.out.println("Nothing is added into RecursionLinkedList rll.");
System.out.println("rll.contains(5) should return 0.");
System.out.println("But your rll.contains(5) returns " + rll.contains(5) + ".");
}
else
{
System.out.println("PASS");
point++;
}
System.out.print("Test the method getFrequencyOf() on an empty RecursionLinkedList: ");
if(rll.getFrequencyOf(5) != 0)
{
System.out.println("FAIL");
System.out.println("Nothing is added into RecursionLinkedList rll.");
System.out.println("rll.getFrequencyOf(5) should return 0.");
System.out.println("But your rll.getFrequencyOf(5) returns " + rll.getFrequencyOf(5) + ".");
}
else
{
System.out.println("PASS");
point++;
}
Random rand = new Random();
int num = 20;
int[] array = new int[5];
for(int i = 0; i < 5; i++)
{
array[i] = 0;
}
System.out.println("Add the following integer into rll:");
String s = "]";
for(int i = 0; i < num - 1; i++)
{
int temp = rand.nextInt(5);
System.out.print(temp + " ");
s = "," + temp + s;
array[temp]++;
rll.add(temp);
}
int temp = rand.nextInt(5);
System.out.println(temp);
s = "[" + temp + s;
array[temp]++;
rll.add(temp);
System.out.println("\nTest the method contains() on a non-empty RecursionLinkedList");
System.out.print(" - Test rll.contains(-1): ");
if(rll.contains(-1) != false)
{
System.out.println("FAIL");
System.out.println("No -1 has been added into the RecursionLinkedList rll.");
System.out.println("rll.contains(-1) should return 0.");
System.out.println("But your rll.contains(-1) returns " + rll.contains(-1));
}
else
{
System.out.println("PASS");
point++;
}
for(int i = 0; i < 5; i++)
{
System.out.print(" - Test rll.contains(" + i + "): ");
if(rll.contains(i) != (array[i] != 0))
{
System.out.println("FAIL");
System.out.println("There are " + array[i] + " in RecursiveLinkedList rll.");
System.out.println("rll.contains(" + i + ") should return " + (array[i] != 0));
System.out.println("But your rll.contains(" + i + ") returns " + rll.contains(i));
isError = true;
}
else
{
System.out.println("PASS");
}
}
if(!isError)
{
point++;
isError = false;
}
System.out.print(" - Test rll.contains(5): ");
if(rll.contains(5) != false)
{
System.out.println("FAIL");
System.out.println("No 5 has been added into the RecursionLinkedList rll.");
System.out.println("rll.contains(5) should return 0.");
System.out.println("But your rll.contains(5) returns " + rll.contains(5));
}
else
{
System.out.println("PASS");
point++;
}
System.out.println("Test the method getFrequencyOf() on a non-empty RecursionLinkedList");
System.out.print(" - Test rll.getFrequencyOf(-1): ");
if(rll.getFrequencyOf(-1) != 0)
{
System.out.println("FAIL");
System.out.println("No -1 has been added into the RecursionLinkedList rll.");
System.out.println("rll.getFrequencyOf(-1) should return 0.");
System.out.println("But your rll.getFrequencyOf(-1) returns " + rll.getFrequencyOf(-1));
}
else
{
System.out.println("PASS");
point++;
}
for(int i = 0; i < 5; i++)
{
System.out.print(" - Test rll.getFrequencyOf(" + i + "): ");
if(rll.getFrequencyOf(i) != array[i])
{
System.out.println("FAIL");
System.out.println(i + " has been added to the RecursionLinkedList " + array[i] + " times.");
System.out.println("rll.getFrequencyOf(" + i + ") should return " + array[i] + ".");
System.out.println("But your rll.getFrequencyOf(" + i + ") returns " + rll.getFrequencyOf(i));
isError = true;
}
else
{
System.out.println("PASS");
}
}
if(!isError)
{
point++;
isError = false;
}
System.out.print(" - Test rll.getFrequencyOf(5): ");
if(rll.getFrequencyOf(5) != 0)
{
System.out.println("FAIL");
System.out.println("No 5 has been added into the RecursionLinkedList rll.");
System.out.println("rll.getFrequencyOf(5) should return 0.");
System.out.println("But your rll.getFrequencyOf(5) returns " + rll.getFrequencyOf(5));
}
else
{
System.out.println("PASS");
point++;
}
System.out.print("Test the method toString(): ");
if(!s.equals(rll.toString()))
{
System.out.println("FAIL");
System.out.println("rll.toString() should return the string \"" + s + "\".");
System.out.println("But your rll.toString() returns the string \"" + rll.toString() + "\".");
}
else
{
System.out.println("PASS");
point++;
}
System.out.println("Test the method getIndexOf()");
System.out.println("Currently the rll is " + rll + ".");
String[] str = rll.toString().split(",");
str[0] = str[0].substring(1);
str[str.length - 1] = str[str.length - 1].substring(0, 1);
for(int i = -1; i <= 5; i++)
{
System.out.print("Test the method getIndexOf(" + i + "): ");
if(getIndex(str,i) != rll.getIndexOf(i))
{
System.out.println("FAIL");
System.out.println("The index of " + i + " should be " + getIndex(str,i) + ".");
System.out.println("But your rll.getIndexOf(" + i + ") returns " + rll.getIndexOf(i) + ".");
isError = true;
}
else
{
System.out.println("PASS");
}
}
if(!isError)
{
point++;
isError = false;
}
System.out.println("Your current point is " + point + ".");
}
public static int getIndex(String[] str, int s)
{
int result = -1;
for(int i = 0; i < str.length; i++)
{
if(s == Integer.parseInt(str[i]))
{
return i;
}
}
return result;
}
}
|