-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleArraySeq.java
More file actions
510 lines (475 loc) · 15.4 KB
/
DoubleArraySeq.java
File metadata and controls
510 lines (475 loc) · 15.4 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package solution;
/**
* A DoubleArraySeq keeps track of a sequence of double numbers. The sequence
* can have a special current element, which is specified and accessed through
* four methods that are not available in the IntArrayBag class (start,
* getCurrent, advance, and isCurrent.
*
* Note that the addAfter and addBefore methods do not allocate more memory
* until the current capacity of the sequence is reached.
*
* Note also that any attempt to increase the capacity of any sequence beyond
* Integer.MAX_VALUE will cause the sequence to fail with an arithmetic
* overflow.
*
* Limitations:
*
* 1. The capacity of a sequence change change after it is created, but the
* maximum capacity is limited by the amount of free memory on the machine. The
* constructor, addAfter, addBefore, clone, and concatenation will result in an
* OutOfMemoryError when free memory is exhausted. 2. A sequence's capacity
* cannot exceed the largest integer 2,147,483,647 (Integer.Max_VALUE). Any
* attempt to create a larger capacity results in failure due to an arithmetic
* overflow.
*
* @author Jake Wooten
* @version 4th edition 2012
*/
public class DoubleArraySeq implements Cloneable
{
/*
* Invariant of the DoubleArraySeq class: 1. The number of elements in the
* sequences is in the instance variable manyItems. 2. For an empty sequence
* (with no elements), we do not care what is stored in any of data; for a
* non-empty sequence, the elements of the sequence are stored in data[0]
* through data[manyItems - 1], and we don't care what's in the rest of
* data. 3. If there is a current element, then it lies in
* data[currentIndex]; if there is no current element, then currentIndex
* equals manyItems.
*/
/**
* The default capacity of a newly constructed DoubleArraySeq. (10 elements)
*/
public final static int DEFAULT_CAPACITY = 10;
/** The elements of this sequence. */
// Hint: private double[] data;
private double[] data;
/**
* The current length of this sequence (i.e., how many items are in this
* sequence).
*/
// Hint: private int manyItems;
private int manyItems;
/** The index of the current element in this sequence. */
// Hint: private int currentIndex;
private int currentIndex;
/**
* Initializes an empty sequence with an initial capacity of
* DEFAULT_CAPACITY.
*
* @postcondition This sequence is empty and has an initial capacity of
* DEFAULT_CAPACITY.
*
* @throws OutOfMemoryError
* if there is insufficient memory for: new
* double[DEFAULT_CAPACITY].
*/
public DoubleArraySeq() throws OutOfMemoryError
{
// TODO
data = new double[DEFAULT_CAPACITY];
manyItems = 0;
currentIndex = -1;
}
/**
* Initializes an empty sequence with the specified initial capacity.
*
* @postcondition This sequence is empty and has an initial capacity of
* initialCapacity.
*
* @param initialCapacity
* initial size of the array
* @throws OutOfMemoryError
* if there is insufficient memory for: new
* double[initialCapacity].
*/
public DoubleArraySeq(int initialCapacity) throws OutOfMemoryError
{
// TODO
data = new double[initialCapacity];
currentIndex = -1;
}
/**
* Adds a new element to this sequence after the current element. If this
* new element would take this beyond its current capacity, then the
* capacity is increased before adding the new element.
*
* @param element
* the new element that is being added to this sequence.
*
* @postcondition a new copy of the element has been added to this sequence.
* If there was a current element, then this method places
* the new element after the current element. If there was no
* current element, then this method places the new element
* at the end of this sequence. The newly added element
* becomes the new current element of this sequence.
*
* @throws OutOfMemoryError
* if there is insufficient memory to increase the size of this
* sequence.
*/
public void addAfter(double element) throws OutOfMemoryError
{
// TODO
if (manyItems == data.length)
{
ensureCapacity((manyItems * 2) + 1);
}
if (isCurrent())
{
for (int i = manyItems; i > currentIndex; i--)
{
data[i] = data[i - 1];
}
currentIndex++;
}
else
{
currentIndex = manyItems;
}
data[currentIndex] = element;
manyItems++;
}
/**
* Adds a new element to this sequence before the current element. If this
* new element would take this beyond its current capacity, then the
* capacity is increased before adding the new element.
*
* @param element
* the new element that is being added to this sequence.
*
* @postcondition a new copy of the element has been added to this sequence.
* If there was a current element, then this method places
* the new element before the current element. If there was
* no current element, then this method places the new
* element at the front of this sequence. The newly added
* element becomes the new current element of this sequence.
*
* @throws OutOfMemoryError
* if there is insufficient memory to increase the size of this
* sequence.
*/
public void addBefore(double element) throws OutOfMemoryError
{
// TODO
if (manyItems == data.length)
{
ensureCapacity((manyItems * 2) + 1);
}
if (!isCurrent())
{
currentIndex = 0;
}
for (int i = manyItems; i > currentIndex; i--)
{
data[i] = data[i - 1];
}
data[currentIndex] = element;
manyItems++;
}
/**
* Places the contents of another sequence at the end of this sequence.
*
* @precondition addend must not be null.
*
* @param addend
* a sequence whose contents will be placed at the end of this
* sequence.
*
* @postcondition the elements from addend have been placed at the end of
* this sequence. The current element of this sequence
* remains where it was, and addend is unchanged.
*
* @throws NullPointerException
* if addend is null.
* @throws OutOfMemoryError
* if there is insufficient memory to increase the capacity of
* this sequence.
*/
public void addAll(DoubleArraySeq addend)
throws NullPointerException, OutOfMemoryError
{
ensureCapacity(addend.manyItems + manyItems);
System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
manyItems += addend.manyItems;
}
/**
* Move forward so that the current element is now the next element in the
* sequence.
*
* @precondition isCurrent() returns true.
*
* @postcondition If the current element was already the end element of this
* sequence (with nothing after it), then there is no longer
* any current element. Otherwise, the new element is the
* element immediately after the original current element.
*
* @throws IllegalStateException
* if there is no current element.
*/
public void advance() throws IllegalStateException
{
if (!isCurrent())
{
throw new IllegalStateException("There is no current element");
}
if (currentIndex == manyItems - 1)
{
currentIndex = -1;
}
else
{
currentIndex++;
}
}
/**
* Creates a copy of this sequence.
*
* @return a copy of this sequence. Subsequent changes to the copy will not
* affect the original, nor vice versa.
*
* @throws OutOfMemoryError
* if there is insufficient memory for creating the clone
* object.
* @throws RuntimeException
* if this class does not implement Cloneable.
*/
@Override
public DoubleArraySeq clone()
throws OutOfMemoryError, RuntimeException
{
// TODO: change this.
return new DoubleArraySeq();
}
/**
* Creates a new sequence that contains all the elements from s1 followed by
* all of the elements from s2.
*
* @precondition neither s1 nor s2 are null.
*
* @param s1
* the first of two sequences.
* @param s2
* the second of two sequences.
*
* @return a new sequence that has the elements of s1 followed by the
* elements of s2 (with no current element).
*
* @throws NullPointerException
* if s1 or s2 are null.
* @throws OutOfMemoryError
* if there is insufficient memory for the new sequence.
*/
public static DoubleArraySeq concatenation(DoubleArraySeq s1,
DoubleArraySeq s2)
throws NullPointerException, OutOfMemoryError
{
// TODO: change this.
if ((s1 == null) && (s2 == null))
{
throw new NullPointerException("one of them is null");
}
DoubleArraySeq s3 = new DoubleArraySeq();
s3.manyItems = s1.manyItems + s2.manyItems;
s3.currentIndex = s2.currentIndex + s1.currentIndex;
return s3;
}
/**
* Change the current capacity of this sequence to be at least the specified
* value.
*
* @param minimumCapacity
* the new minimum capacity for this sequence.
*
* @postcondition This sequence's capacity has been changed to be at least
* minimumCapacity, but no less than size.
*
* @throws OutOfMemoryError
* if there is insufficient memory for the a new array: new
* double[minimumCapacity].
*/
public void ensureCapacity(int minimumCapacity)
throws OutOfMemoryError
{
double huge[];
if (data.length < minimumCapacity)
{
huge = new double[minimumCapacity];
System.arraycopy(data, 0, huge, 0, manyItems);
data = huge;
}
}
/**
* Determines the current capacity of this sequence.
*
* @return the current capacity of this sequence.
*/
public int getCapacity()
{
// TODO: change this
return data.length;
}
/**
* Returns a copy of the current element in this sequence.
*
* @precondition isCurrent() returns true.
*
* @return the current element of this sequence.
*
* @throws IllegalStateException
* if there is no current element.
*/
public double getCurrent() throws IllegalStateException
{
if (isCurrent())
{
return data[currentIndex];
}
else
{
throw new IllegalStateException("There is no current index.");
}
}
/**
* Determines whether this sequence has specified a current element.
*
* @return true if there is a current element, or false otherwise.
*/
public boolean isCurrent()
{
boolean current;
if (currentIndex >= 0)
{
current = true;
}
else
{
current = false;
}
return current;
}
/**
* Removes the current element from this sequence.
*
* @precondition isCurrent() returns true.
*
* @postcondition The current element has been removed from this sequence,
* and the following element (if there is one) is now the new
* current element. If there was no following element, then
* there is now no current element.
*
* @throws IllegalStateException
* if there is no current element.
*/
public void removeCurrent()
throws IllegalStateException
{
for (int i = currentIndex; i < manyItems - 1; i++)
{
data[i] = data[i + 1];
}
manyItems--;
}
/**
* Determines the number of elements in this sequence.
*
* @return the number of elements in this sequence.
*/
public int size()
{
return manyItems;
}
/**
* Sets the current element at the front of this sequence.
*
* @postcondition If this sequence is not empty, the front element of this
* sequence is now the current element; otherwise, there is
* no current element.
*/
public void start()
{
// TODO
if (data[0] != 0)
{
currentIndex = 0;
}
}
/**
* Reduces the capacity of the sequence to the number of elements currently
* in the sequence.
*
* @postcondition This sequence's capacity has been changed to its current
* size.
*
* @throws OutOfMemoryError
* if there is insufficient memory for altering the capacity.
*/
public void trimToSize()
throws OutOfMemoryError
{
// TODO
double[] trim;
if (data.length != manyItems)
{
trim = new double[manyItems];
System.arraycopy(data, 0, trim, 0, manyItems);
data = trim;
}
}
/**
* Returns a String representation of this sequence. If the sequence is
* empty, the method should return <>. If the sequence has one item, say
* 1.1, and that item is not the current item, the method should return
* <1.1>. If the sequence has more than one item, they should be separated
* by commas, for example: <1.1, 2.2, 3.3>. If there exists a current item,
* then that item should be surrounded by square braces. For example, if the
* second item is the current item, the method should return: <1.1, [2.2],
* 3.3>.
*
* @return a String representation of this sequence.
*/
@Override
public String toString()
{
String str = "";
str += "<";
for (int i = 0; i < manyItems; i++)
{
if (i == currentIndex)
{
str += "[" + data[i] + "]";
}
else
{
str += data[i];
}
if (i != manyItems - 1)
{
str += ", ";
}
}
str += ">";
return str;
}
/**
* Determines if this object is equal to the other object. Two sequences are
* the same if they have the same number of elements, and the elements at
* each position in the sequence are the same. (It does not matter if the
* two sequences have the same capacity or not.)
*
* @param other
* The object to compare.
*
* @return true if this object is equal to the other object, false
* otherwise.
*/
@Override
public boolean equals(Object other)
{
if (other == data)
{
return true;
}
return false;
}
}