mihainan

Lab03 - POO

Oct 21st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. package Lab03;
  2.  
  3. /**
  4.  *
  5.  * @author Nan Mihai
  6.  */
  7. public class IntSet {
  8.     int v[];
  9.     int nr;
  10.    
  11.     public IntSet(int n) {
  12.         this.v = new int[n];
  13.         this.nr = 0;
  14.     }
  15.    
  16.     public boolean contains(int x) {
  17.         IntSet set = this;
  18.         for(int i = 0; i < set.nr; i++) {
  19.             if(set.v[i] == x) {
  20.                 return true;
  21.             }
  22.         }
  23.         return false;
  24.     }
  25.    
  26.     public void add(int x) {
  27.         IntSet set = this;
  28.         if(!set.contains(x)) {
  29.             v[nr] = x;
  30.             nr++;
  31.         }
  32.     }
  33.    
  34.     public String toString() {
  35.         IntSet set = this;
  36.         String s = "";
  37.         for(int i = 0; i < set.nr; i++) {
  38.             s += set.v[i] + " ";
  39.         }
  40.         s += "\n";
  41.         return s;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment