Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package set;
  2.  
  3. import java.util.NoSuchElementException;
  4. import java.util.Iterator;
  5.  
  6. public class MaxSet<E extends Comparable<E>> extends ArraySet<E> {
  7.     private E maxElement = null;
  8.  
  9.     /**
  10.      * Constructs a new empty set.
  11.      */
  12.     public MaxSet() {
  13.         super();
  14.  
  15.     }
  16.  
  17.     /**
  18.      * Returns the currently largest element in this set. pre: the set is not
  19.      * empty post: the set is unchanged
  20.      *
  21.      * @return the currently largest element in this set
  22.      * @throws NoSuchElementException
  23.      *             if this set is empty
  24.      */
  25.     public E getMax() {
  26.         if (!super.isEmpty()) {
  27.             return maxElement;
  28.         } else {
  29.             throw new NoSuchElementException("set empty");
  30.         }
  31.         // System.out.println(maxElement);
  32.     }
  33.  
  34.     /**
  35.      * Adds the specified element to this set, if it is not already present.
  36.      * post: x is added to the set if it is not already present
  37.      *
  38.      * @param x
  39.      *            the element to be added
  40.      * @return true if the specified element was added
  41.      */
  42.     public boolean add(E x) {
  43.         if (maxElement == null) {
  44.             maxElement = x;
  45.         } else {
  46.             if (x.compareTo(maxElement) > 0) {
  47.                 maxElement = x;
  48.             }
  49.         }
  50.         return super.add(x);
  51.     }
  52.  
  53.     /**
  54.      * post: x is removed if it was present
  55.      *
  56.      * @param x
  57.      *            the element to remove - if present
  58.      * @return true if the set contained the specified element
  59.      */
  60.     public boolean remove(Object x) { // varför object
  61.         if (super.isEmpty()) {
  62.             return false;
  63.         }
  64.  
  65.         if (super.remove(x)) {
  66.  
  67.             if (maxElement.equals(x))
  68.                 findMax();
  69.             return true;
  70.         }
  71.  
  72.         return false;
  73.  
  74.     }
  75.  
  76.     private void findMax() {
  77.         // hittar nya maxElement
  78.        
  79.         maxElement = null;
  80.         Iterator<? extends E> itr = super.iterator();
  81.         for (int i = 0; i < super.size(); i++) {
  82.             if (itr.hasNext()) {
  83.                 E temp = itr.next();
  84.                 if (maxElement == null) {
  85.                     maxElement = temp;
  86.                 } else if (temp.compareTo(maxElement) > 0) {
  87.                     maxElement = temp;
  88.                 }
  89.             }
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * Adds all of the elements in the specified set, for which it is possible,
  95.      * to this set. post: all elements, for which it is possible, in the
  96.      * specified set are added to this set.
  97.      *
  98.      * @return true if this set changed as a result of the call
  99.      */
  100.     public boolean addAll(SimpleSet<? extends E> c) {
  101.         return super.addAll(c);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement