Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Use LinkedLists to implement a class Set_YI.java with the following methods:
- //
- // 1. add(E e)
- //
- // 2. addAll(Collection c)
- //
- // 3. void clear()
- //
- // 4. boolean contains(Object o)
- //
- // 5.boolean containsAll(Collection c)
- //
- // 6. boolean equals(Object o)
- //
- // 7. boolean isEmpty()
- //
- // 8. Iterator iterator()
- //
- // 9. boolean remove(Object o)
- //
- // 10. boolean removeAll(Collection c)
- //
- // 11. int size()
- // Will Morrison
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.LinkedList;
- public class Set_YI<E> {
- public LinkedList<E> list;
- public int size() {
- // Returns the number of elements
- return list.size();
- }
- public boolean add(E o) {
- // Ensures that this collection contains the specified element
- if (list.contains(o) == false) {
- list.add(o);
- return true;
- }
- return false;
- }
- public boolean addAll(Collection<E> c) {
- // Adds all of the elements in the specified collection to this set if they’re not already present
- if (list.containsAll(c) == false) {
- list.addAll(c);
- return true;
- }
- return false;
- }
- public void clear() {
- // Removes all of the elements from this set
- list.clear();
- }
- public boolean contains(Object o) {
- // Returns true if this set contains the specified element
- if (list.contains(o)) {
- return true;
- } else {
- return false;
- }
- }
- public boolean containsAll(Collection<E> c) {
- // Returns true if this set contains all of the elements
- if (list.containsAll(c)) {
- return true;
- } else {
- return false;
- }
- }
- public boolean equals(Object o) {
- // Compares the specified object with this set for equality
- if (list.equals(o)) {
- return true;
- } else {
- return false;
- }
- }
- public boolean isEmpty() {
- // Returns true if this set contains no elements
- if (list.isEmpty()) {
- return true;
- } else {
- return false;
- }
- }
- public Iterator<E> iterator() {
- // Returns an iterator over the elements in this set
- return list.iterator();
- }
- public boolean remove(Object o) {
- // Removes the specified element from this set if it is present
- if (list.contains(o) == true) {
- list.remove(o);
- return true;
- } else {
- return false;
- }
- }
- public boolean removeAll(Collection<E> c) {
- // Removes from this set all of its elements that are contained in the specified collection
- if (list.containsAll(c) == true) {
- list.removeAll(c);
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement