Guest User

Untitled

a guest
Aug 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. /**
  2. * lab goals: - Learn all about "do it yourself" linked lists
  3. * - Learn how to create unit tests using JUnit and the Jamtester tool.
  4. * - Learn how to create a complete battery of tests so as to attain complete "code coverage".
  5. * - Have a lot of fun looking at the bright colors red and green!
  6. *
  7. * lab instructions:
  8. * while ( everyMethodNotYetWrittenAndThoroughlyTested() ) {
  9. * 1) Write a test for a method.
  10. * 2) Run and watch the test fail.
  11. *
  12. * while ( testNotYetGreen() ) {
  13. 3) Write/modify the code for the method.
  14. * 4) re-run the test.
  15. * }
  16. * }
  17. *
  18. * Now that you have at least one test method per method in your class, go back and run
  19. * all tests in the student tool. Do you have complete code coverage? If not, design more
  20. * tests until you do.
  21. *
  22. * class invariant: elements stored in the list are all of the same type.
  23. */
  24. import java.util.*;
  25. import java.io.PrintStream;
  26. import java.lang.Comparable;
  27. import java.lang.Object;
  28. import java.lang.String;
  29.  
  30. public class SinglyLinkedList2 extends SinglyLinkedList
  31. {
  32.  
  33.  
  34. /** returns the number of nodes in the list **/
  35. public int size()
  36. {
  37. return size(getFront());// so it compiles
  38. }
  39.  
  40. private int size(ListNode curNode) // what is this method for?
  41. {
  42. int size = 1;
  43. while(curNode.getNext() != null)
  44. {
  45. curNode = curNode.getNext();
  46. size++;
  47. }
  48. return size;
  49. }
  50.  
  51. /** adds c immediately before the getFront() node larger than it
  52. ** <br>all nodes before c are smaller than c
  53. ** <br>that is c.compareTo(get(i)) > 0
  54. **/
  55. public void inOrderInsert(Comparable c)
  56. {
  57. int ind = 0;
  58. ListNode curr = getFront();
  59. while (c.compareTo(curr.getNext().getValue()) < 0 && curr != null)
  60. {
  61. curr = curr.getNext();
  62. ind++;
  63. }
  64. add(ind +1 , c);
  65. }
  66.  
  67. /** adds value to the end of the list.**/
  68. public void addLast(Object value)
  69. {
  70. assertValidType(value);
  71. int ind = 0;
  72. ListNode curr = getFront();
  73. while(curr.getNext() != null)
  74. {
  75. curr = curr.getNext();
  76. ind++;
  77. }
  78. add( ind +1, value);
  79. }
  80.  
  81. /** returns a reference to the last node in a list**/
  82. public Object getLast()
  83. {
  84. ListNode node = getFront();
  85. while(node.getNext() != null)
  86. {
  87. node = node.getNext();
  88. }
  89. return node.getValue();
  90. // so it compiles
  91. }
  92.  
  93. /** reverse the contents of the list.**/
  94. public void reverse()
  95. {
  96. for(int i = 0; i < size(); i++)
  97. {
  98.  
  99. }
  100. }
  101.  
  102. /** returns true if true iff getvalue().equals(obj) for some node in list<br>
  103. ** false otherwise **/
  104. public boolean contains(Object obj)
  105. {
  106. ListNode node = getFront();
  107. while(node != null)
  108. {
  109. if(node.getValue().equals(obj))
  110. {
  111. return true;
  112. }
  113. node = node.getNext();
  114. }
  115. return false;
  116. }
  117.  
  118. /**returns the a reference to the middle node (or either of two middle nodes)
  119. **<br> returns null if the list is empty **/
  120. public ListNode middleNode()
  121. {
  122. if(getFront() == null)
  123. {
  124. return null;
  125. }
  126. ListNode node = getFront();
  127. for (int i = 0; i < size()/2; i++)
  128. {
  129. node = node.getNext();
  130. }
  131. return node;
  132. // so it compiles
  133. }
  134. }
Add Comment
Please, Sign In to add comment