daily pastebin goal
77%
SHARE
TWEET

Untitled

a guest Jan 21st, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class IntSet {
  2.    //OVERVIEW: IntSets are unbounded, mutable sets of integers.
  3.    private Vector els;
  4.    // the rep
  5.   // constructors
  6.    public IntSet(){
  7.       //EFFECTS: Initializes this to be empty.
  8.       els = new Vector( );
  9. }
  10.    // methods
  11.    public void insert (int x) {
  12.       //MODIFIES: this
  13.       //EFFECTS: Adds x to the elements of this.
  14.       Integer y = new Integer(x);
  15.       if (getIndex(y) < 0) els.add(y);
  16.     }
  17.  
  18.    public void remove (int x) {
  19.       //MODIFIES: this
  20.       //EFFECTS: Removes x from this.
  21.       int i = getIndex(new Integer(x));
  22.       if (i < 0) return;
  23.       els.set(i, els.lastElement( ));
  24.       els.remove(els.size( ) - 1);
  25.    }
  26.  
  27.    public boolean isIn (int x) {
  28.       //EFFECTS: Returns true if x is in this else returns false.
  29.       return getIndex(new Integer(x)) >= 0;
  30.    }
  31.  
  32.    private int getIndex (Integer x) {
  33.       //EFFECTS: If x is in this returns index where x appears else returns -1.
  34.       for (inti=0;i< els.size( ); i++)
  35.          if (x.equals(els.get(i))) return i;
  36.       return -1;
  37.    }
  38.  
  39.    public int size( ){
  40.       //EFFECTS: Returns the cardinality of this.
  41.       return els.size( );
  42.    }
  43.  
  44.    public int choose ( ) throws EmptyException {
  45.       //EFFECTS: If this is empty throws EmptyException else
  46.      //returns an arbitrary element of this.
  47.           if (els.size( ) == 0) throw new EmptyException("IntSet.choose");
  48.           return els.lastElement( );
  49.   }
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top