Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.57 KB | None | 0 0
  1. package solution;
  2. /**
  3. * A DoubleArraySeq keeps track of a sequence of double numbers. The sequence
  4. * can have a special current element, which is specified and accessed through
  5. * four methods that are not available in the IntArrayBag class (start,
  6. * getCurrent, advance, and isCurrent.
  7. *
  8. * Note that the addAfter and addBefore methods do not allocate more memory
  9. * until the current capacity of the sequence is reached.
  10. *
  11. * Note also that any attempt to increase the capacity of any sequence beyond
  12. * Integer.MAX_VALUE will cause the sequence to fail with an arithmetic
  13. * overflow.
  14. *
  15. * Limitations:
  16. *
  17. * 1. The capacity of a sequence change change after it is created, but the
  18. * maximum capacity is limited by the amount of free memory on the machine. The
  19. * constructor, addAfter, addBefore, clone, and concatenation will result in an
  20. * OutOfMemoryError when free memory is exhausted. 2. A sequence's capacity
  21. * cannot exceed the largest integer 2,147,483,647 (Integer.Max_VALUE). Any
  22. * attempt to create a larger capacity results in failure due to an arithmetic
  23. * overflow.
  24. *
  25. * @author Stephanie Drum
  26. * @version 4February 2019
  27. */
  28.  
  29. public class DoubleArraySeq implements Cloneable
  30. {
  31. /*
  32. * Invariant of the DoubleArraySeq class: 1. The number of elements in the
  33. * sequences is in the instance variable manyItems. 2. For an empty sequence
  34. * (with no elements), we do not care what is stored in any of data; for a
  35. * non-empty sequence, the elements of the sequence are stored in data[0]
  36. * through data[manyItems - 1], and we don't care what's in the rest of
  37. * data. 3. If there is a current element, then it lies in
  38. * data[currentIndex]; if there is no current element, then currentIndex
  39. * equals manyItems.
  40. */
  41.  
  42. /**
  43. * The default capacity of a newly constructed DoubleArraySeq. (10 elements)
  44. */
  45. public final static int DEFAULT_CAPACITY = 10;
  46.  
  47. /** The elements of this sequence. */
  48. // Hint: private double[] data;
  49. private double[] data;
  50.  
  51. /**
  52. * The current length of this sequence (i.e., how many items are in this
  53. * sequence).
  54. */
  55. // Hint: private int manyItems;
  56. private int multipleItems;
  57.  
  58. /** The index of the current element in this sequence. */
  59. // Hint: private int currentIndex;
  60. private int curIndex;
  61.  
  62. /**
  63. * Initializes an empty sequence with an initial capacity of
  64. * DEFAULT_CAPACITY.
  65. *
  66. * @postcondition This sequence is empty and has an initial capacity of
  67. * DEFAULT_CAPACITY.
  68. *
  69. * @throws OutOfMemoryError
  70. * if there is insufficient memory for: new
  71. * double[DEFAULT_CAPACITY].
  72. */
  73. public DoubleArraySeq() throws OutOfMemoryError
  74. {
  75. // TODO
  76.  
  77. multipleItems = 0;
  78. curIndex = 0;
  79. data = new double[DEFAULT_CAPACITY];
  80.  
  81. }
  82.  
  83. /**
  84. * Initializes an empty sequence with the specified initial capacity.
  85. *
  86. * @postcondition This sequence is empty and has an initial capacity of
  87. * initialCapacity.
  88. *
  89. * @param initialCapacity
  90. * initial size of the array
  91. * @throws OutOfMemoryError
  92. * if there is insufficient memory for: new
  93. * double[initialCapacity].
  94. */
  95. public DoubleArraySeq(int initialCapacity) throws OutOfMemoryError
  96. {
  97. // TODO
  98.  
  99. if (initialCapacity >= 0)
  100. {
  101. data = new double[initialCapacity];
  102. //Reset the items to 0, int variable
  103. multipleItems = 0;
  104. //Reset the index to 0, int variable
  105. curIndex = 0;
  106. }
  107. else
  108. {
  109. data = new double[DEFAULT_CAPACITY];
  110.  
  111. throw new OutOfMemoryError("Memory is out, a default "
  112. + "memory space has been cretaed");
  113.  
  114. }
  115. }
  116.  
  117. /**
  118. * Adds a new element to this sequence after the current element. If this
  119. * new element would take this beyond its current capacity, then the
  120. * capacity is increased before adding the new element.
  121. *
  122. * @param element
  123. * the new element that is being added to this sequence.
  124. *
  125. * @postcondition a new copy of the element has been added to this sequence.
  126. * If there was a current element, then this method places
  127. * the new element after the current element. If there was no
  128. * current element, then this method places the new element
  129. * at the end of this sequence. The newly added element
  130. * becomes the new current element of this sequence.
  131. *
  132. * @throws OutOfMemoryError
  133. * if there is insufficient memory to increase the size of this
  134. * sequence.
  135. */
  136. public void addAfter(double element) throws OutOfMemoryError
  137. {
  138. // TODO
  139. //adds to the data to equal the number of spots for data storage
  140.  
  141.  
  142. //if the number if items is equal to the length of the data
  143. //multiply the items by 2
  144. if (multipleItems == data.length)
  145. {
  146. ensureCapacity(multipleItems * 2);
  147. }
  148. for (int j = multipleItems - 1; j > curIndex; j--)
  149. {
  150. data[j + 1] = data[j];
  151. }
  152. //if the current number of items is greater than 0
  153. //increase the index
  154. if (multipleItems > 0)
  155. {
  156. curIndex++;
  157. }
  158. //makes the index ad number of items equal
  159. if (curIndex > multipleItems)
  160. {
  161.  
  162. data[multipleItems] = element;
  163. curIndex = multipleItems;
  164. multipleItems++;
  165. }
  166. else if (multipleItems > curIndex)
  167. {
  168. data[curIndex] = element;
  169. multipleItems++;
  170. }
  171.  
  172. }
  173.  
  174. /**
  175. * Adds a new element to this sequence before the current element. If this
  176. * new element would take this beyond its current capacity, then the
  177. * capacity is increased before adding the new element.
  178. *
  179. * @param element
  180. * the new element that is being added to this sequence.
  181. *
  182. * @postcondition a new copy of the element has been added to this sequence.
  183. * If there was a current element, then this method places
  184. * the new element before the current element. If there was
  185. * no current element, then this method places the new
  186. * element at the front of this sequence. The newly added
  187. * element becomes the new current element of this sequence.
  188. *
  189. * @throws OutOfMemoryError
  190. * if there is insufficient memory to increase the size of this
  191. * sequence.
  192. */
  193. public void addBefore(double element) throws OutOfMemoryError
  194. {
  195. // TODO
  196. if (multipleItems == data.length)
  197. {
  198. ensureCapacity(multipleItems * 2);
  199. }
  200. if (!isCurrent())
  201. {
  202. start();
  203. }
  204.  
  205. for (int i = multipleItems - 1; i >= curIndex; i--)
  206. {
  207. data[i + 1] = data[i];
  208. }
  209.  
  210. data[curIndex] = element;
  211. multipleItems++;
  212. }
  213.  
  214. /**
  215. * Places the contents of another sequence at the end of this sequence.
  216. *
  217. * @precondition addend must not be null.
  218. *
  219. * @param addend
  220. * a sequence whose contents will be placed at the end of this
  221. * sequence.
  222. *
  223. * @postcondition the elements from addend have been placed at the end of
  224. * this sequence. The current element of this sequence
  225. * remains where it was, and addend is unchanged.
  226. *
  227. * @throws NullPointerException
  228. * if addend is null.
  229. * @throws OutOfMemoryError
  230. * if there is insufficient memory to increase the capacity of
  231. * this sequence.
  232. */
  233. public void addAll(DoubleArraySeq addend)
  234. throws NullPointerException, OutOfMemoryError
  235. {
  236.  
  237.  
  238. // TODO
  239. //if it is not current then it starts
  240. if (!isCurrent())
  241. {
  242. start();
  243. }
  244. double[] arrayCopy = new double[multipleItems + addend.multipleItems];
  245.  
  246. System.arraycopy(data, 0, arrayCopy, 0, multipleItems);
  247. System.arraycopy(addend.data, 0, arrayCopy,
  248. multipleItems, addend.multipleItems);
  249.  
  250. data = arrayCopy;
  251. multipleItems = addend.multipleItems + multipleItems;
  252. this.ensureCapacity(this.multipleItems + 1 + addend.multipleItems);
  253. }
  254.  
  255. /**
  256. * Move forward so that the current element is now the next element in the
  257. * sequence.
  258. *
  259. * @precondition isCurrent() returns true.
  260. *
  261. * @postcondition If the current element was already the end element of this
  262. * sequence (with nothing after it), then there is no longer
  263. * any current element. Otherwise, the new element is the
  264. * element immediately after the original current element.
  265. *
  266. * @throws IllegalStateException
  267. * if there is no current element.
  268. */
  269. public void advance() throws IllegalStateException
  270. {
  271. // TODO
  272. if (isCurrent())
  273. {
  274. curIndex += 1;
  275. }
  276. else
  277. {
  278. throw new IllegalStateException(
  279. "Not in Index, there is no current element in this index");
  280. }
  281.  
  282. }
  283.  
  284. /**
  285. * Creates a copy of this sequence.
  286. *
  287. * @return a copy of this sequence. Subsequent changes to the copy will not
  288. * affect the original, nor vice versa.
  289. *
  290. * @throws OutOfMemoryError
  291. * if there is insufficient memory for creating the clone
  292. * object.
  293. * @throws RuntimeException
  294. * if this class does not implement Cloneable.
  295. */
  296. @Override
  297. public DoubleArraySeq clone()
  298. throws OutOfMemoryError, RuntimeException
  299. {
  300. DoubleArraySeq doubleArraySeq = new DoubleArraySeq();
  301.  
  302. // TODO: change this.
  303. doubleArraySeq.data = data.clone();
  304. doubleArraySeq.multipleItems = multipleItems;
  305. doubleArraySeq.curIndex = curIndex;
  306.  
  307. return new DoubleArraySeq();
  308. }
  309.  
  310. /**
  311. * Creates a new sequence that contains all the elements from s1 followed by
  312. * all of the elements from s2.
  313. *
  314. * @precondition neither s1 nor s2 are null.
  315. *
  316. * @param s1
  317. * the first of two sequences.
  318. * @param s2
  319. * the second of two sequences.
  320. *
  321. * @return a new sequence that has the elements of s1 followed by the
  322. * elements of s2 (with no current element).
  323. *
  324. * @throws NullPointerException
  325. * if s1 or s2 are null.
  326. * @throws OutOfMemoryError
  327. * if there is insufficient memory for the new sequence.
  328. */
  329. public static DoubleArraySeq concatenation(DoubleArraySeq s1,
  330. DoubleArraySeq s2)
  331. throws NullPointerException, OutOfMemoryError
  332. {
  333. if (s1 == null || s2 == null)
  334. {
  335. throw new NullPointerException("You have a Null!");
  336. }
  337. // TODO: change this.
  338.  
  339. try
  340. {
  341. DoubleArraySeq sequenceR = new DoubleArraySeq(s1.multipleItems
  342. + s2.multipleItems);
  343. System.arraycopy(s1.data, 0, sequenceR.data, 0, s1.multipleItems);
  344. System.out.println(sequenceR.toString());
  345. System.arraycopy(s2.data, 0, sequenceR.data,
  346. s1.multipleItems, s2.multipleItems);
  347. sequenceR.multipleItems = s1.multipleItems + s2.multipleItems;
  348. sequenceR.curIndex = sequenceR.multipleItems;
  349. return sequenceR;
  350. }
  351. catch (OutOfMemoryError e)
  352. {
  353. throw new OutOfMemoryError("You are out of memory!");
  354. }
  355.  
  356. }
  357.  
  358. /**
  359. * Change the current capacity of this sequence to be at least the specified
  360. * value.
  361. *
  362. * @param minimumCapacity
  363. * the new minimum capacity for this sequence.
  364. *
  365. * @postcondition This sequence's capacity has been changed to be at least
  366. * minimumCapacity, but no less than size.
  367. *
  368. * @throws OutOfMemoryError
  369. * if there is insufficient memory for the a new array: new
  370. * double[minimumCapacity].
  371. */
  372. public void ensureCapacity(int minimumCapacity)
  373. throws OutOfMemoryError
  374. {
  375. if (data.length < minimumCapacity)
  376. {
  377. double[] clonedDAS = new double[minimumCapacity];
  378. System.arraycopy(data, 0, clonedDAS, 0, data.length);
  379. data = clonedDAS;
  380. }
  381. }
  382.  
  383. /**
  384. * Determines the current capacity of this sequence.
  385. *
  386. * @return the current capacity of this sequence.
  387. */
  388. public int getCapacity()
  389. {
  390. // TODO: change this
  391. //return the data's length instead
  392.  
  393.  
  394. return data.length;
  395. }
  396.  
  397. /**
  398. * Returns a copy of the current element in this sequence.
  399. *
  400. * @precondition isCurrent() returns true.
  401. *
  402. * @return the current element of this sequence.
  403. *
  404. * @throws IllegalStateException
  405. * if there is no current element.
  406. */
  407. public double getCurrent() throws IllegalStateException
  408. {
  409. // TODO: change this
  410. if (isCurrent() == true)
  411. {
  412. return data[curIndex];
  413. }
  414. else if (isCurrent() == false)
  415. {
  416. throw new IllegalStateException("There is no current element.");
  417. }
  418. return data[curIndex];
  419.  
  420. }
  421.  
  422. /**
  423. * Determines whether this sequence has specified a current element.
  424. *
  425. * @return true if there is a current element, or false otherwise.
  426. */
  427. public boolean isCurrent()
  428. {
  429. // TODO
  430. if (curIndex >= 0 && curIndex < multipleItems)
  431. {
  432. return true;
  433. }
  434. else
  435. {
  436. return false;
  437. }
  438. }
  439.  
  440. /**
  441. * Removes the current element from this sequence.
  442. *
  443. * @precondition isCurrent() returns true.
  444. *
  445. * @postcondition The current element has been removed from this sequence,
  446. * and the following element (if there is one) is now the new
  447. * current element. If there was no following element, then
  448. * there is now no current element.
  449. *
  450. * @throws IllegalStateException
  451. * if there is no current element.
  452. */
  453. public void removeCurrent()
  454. throws IllegalStateException
  455. {
  456. // TODO
  457.  
  458. if (curIndex != multipleItems)
  459. {
  460. for (int i = curIndex; i < multipleItems - 1; i++)
  461. {
  462. data[i] = data[i + 1];
  463. }
  464. multipleItems--;
  465. }
  466. else
  467. {
  468. throw new IllegalStateException("There is no "
  469. + "current element to find");
  470. }
  471.  
  472. }
  473.  
  474. /**
  475. * Determines the number of elements in this sequence.
  476. *
  477. * @return the number of elements in this sequence.
  478. */
  479. public int size()
  480. {
  481. // TODO: change this.
  482. return multipleItems;
  483. }
  484.  
  485. /**
  486. * Sets the current element at the front of this sequence.
  487. *
  488. * @postcondition If this sequence is not empty, the front element of this
  489. * sequence is now the current element; otherwise, there is
  490. * no current element.
  491. */
  492. public void start()
  493. {
  494. // TODO
  495. //starts if the index is 0
  496. curIndex = 0;
  497. }
  498.  
  499. /**
  500. * Reduces the capacity of the sequence to the number of elements currently
  501. * in the sequence.
  502. *
  503. * @postcondition This sequence's capacity has been changed to its current
  504. * size.
  505. *
  506. * @throws OutOfMemoryError
  507. * if there is insufficient memory for altering the capacity.
  508. */
  509. public void trimToSize()
  510. throws OutOfMemoryError
  511. {
  512. // TODO
  513. try
  514. {
  515. double[] smallArray = new double[multipleItems];
  516. for (int i = 0; i < multipleItems; i++)
  517. {
  518. smallArray[i] = data[i];
  519. }
  520. data = smallArray;
  521. }
  522. catch (OutOfMemoryError e)
  523. {
  524. throw new OutOfMemoryError("You are out of Memory!");
  525. }
  526. }
  527.  
  528. /**
  529. * Returns a String representation of this sequence. If the sequence is
  530. * empty, the method should return <>. If the sequence has one item, say
  531. * 1.1, and that item is not the current item, the method should return
  532. * <1.1>. If the sequence has more than one item, they should be separated
  533. * by commas, for example: <1.1, 2.2, 3.3>. If there exists a current item,
  534. * then that item should be surrounded by square braces. For example, if the
  535. * second item is the current item, the method should return: <1.1, [2.2],
  536. * 3.3>.
  537. *
  538. * @return a String representation of this sequence.
  539. */
  540. @Override
  541. public String toString()
  542. {
  543. // TODO: change this.
  544.  
  545. String stringA = "<";
  546. for (int i = 0; i < multipleItems; i++)
  547. {
  548. if (i == curIndex)
  549. {
  550. stringA += "[";
  551. }
  552. stringA += data[i];
  553. if (i == curIndex)
  554. {
  555. stringA += "]";
  556. }
  557. if (!(multipleItems - 1 == i))
  558. {
  559. stringA += ", ";
  560. }
  561. }
  562. stringA += ">";
  563. return stringA;
  564. }
  565.  
  566.  
  567. /**
  568. * Determines if this object is equal to the other object. Two sequences are
  569. * the same if they have the same number of elements, and the elements at
  570. * each position in the sequence are the same. (It does not matter if the
  571. * two sequences have the same capacity or not.)
  572. *
  573. * @param other
  574. * The object to compare.
  575. *
  576. * @return true if this object is equal to the other object, false
  577. * otherwise.
  578. */
  579. @Override
  580. public boolean equals(Object other)
  581. {
  582. // TODO: change this.
  583. return this.toString().equals(other.toString());
  584. }
  585. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement