Advertisement
Arbitrator

Untitled

Jan 8th, 2020
11,431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. //Use LinkedLists to implement a class Set_YI.java with the following methods:
  2. //
  3. // 1. add(E e)
  4. //
  5. // 2. addAll(Collection c)
  6. //
  7. // 3. void clear()
  8. //
  9. // 4. boolean contains(Object o)
  10. //
  11. // 5.boolean containsAll(Collection c)
  12. //
  13. // 6. boolean equals(Object o)
  14. //
  15. // 7. boolean isEmpty()
  16. //
  17. // 8. Iterator iterator()
  18. //
  19. // 9. boolean remove(Object o)
  20. //
  21. // 10. boolean removeAll(Collection c)
  22. //
  23. // 11. int size()
  24.  
  25. // Will Morrison
  26.  
  27. import java.util.Collection;
  28. import java.util.Iterator;
  29. import java.util.LinkedList;
  30.  
  31. public class Set_YI<E> {
  32.  
  33. public LinkedList<E> list;
  34.  
  35. public int size() {
  36. // Returns the number of elements
  37. return list.size();
  38.  
  39. }
  40.  
  41. public boolean add(E o) {
  42. // Ensures that this collection contains the specified element
  43. if (list.contains(o) == false) {
  44. list.add(o);
  45. return true;
  46. }
  47. return false;
  48. }
  49.  
  50.  
  51. public boolean addAll(Collection<E> c) {
  52. // Adds all of the elements in the specified collection to this set if they’re not already present
  53. if (list.containsAll(c) == false) {
  54. list.addAll(c);
  55. return true;
  56. }
  57. return false;
  58. }
  59.  
  60.  
  61. public void clear() {
  62. // Removes all of the elements from this set
  63. list.clear();
  64. }
  65.  
  66.  
  67. public boolean contains(Object o) {
  68. // Returns true if this set contains the specified element
  69. if (list.contains(o)) {
  70. return true;
  71. } else {
  72. return false;
  73. }
  74. }
  75.  
  76. public boolean containsAll(Collection<E> c) {
  77. // Returns true if this set contains all of the elements
  78. if (list.containsAll(c)) {
  79. return true;
  80. } else {
  81. return false;
  82. }
  83.  
  84. }
  85.  
  86.  
  87. public boolean equals(Object o) {
  88. // Compares the specified object with this set for equality
  89. if (list.equals(o)) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94.  
  95. }
  96.  
  97. public boolean isEmpty() {
  98. // Returns true if this set contains no elements
  99. if (list.isEmpty()) {
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. }
  105.  
  106.  
  107. public Iterator<E> iterator() {
  108. // Returns an iterator over the elements in this set
  109. return list.iterator();
  110.  
  111. }
  112.  
  113. public boolean remove(Object o) {
  114. // Removes the specified element from this set if it is present
  115. if (list.contains(o) == true) {
  116. list.remove(o);
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }
  122.  
  123.  
  124. public boolean removeAll(Collection<E> c) {
  125. // Removes from this set all of its elements that are contained in the specified collection
  126. if (list.containsAll(c) == true) {
  127. list.removeAll(c);
  128. return true;
  129. } else {
  130. return false;
  131. }
  132.  
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement