Below is the code for a “Bounded Set”, which is an object which can store other objects of a generic type . This gives the bounded set the ability to store any type of object, and the methods included also allow the contents to be searched, added to and removed from, as well as a “toString” method which overrides the java method of the same name, to print an actual string of the contents, as opposed to a location of the “Bounded Set” within the computer. The Main() is included for testing purposes as well!
/** * BoundedSet is an object which stores objects of a generic type, E, * in an array. * * @author AidanTakami * @version 1.0 * */ class BoundedSet { private int size; private E[] array; /** * Constructor for the object BoundedSet which takes the Set size as an int capacity * also sets size = capacity and creates a new array of type Object and castes it as an array * of E. * * @since 1.0 * @param int capacity which represents the size of the BoundedSet * */ @SuppressWarnings("unchecked") public BoundedSet(int capacity){ size = capacity; array = (E[]) new Object[capacity]; } /** * method to add E element to the BoundedSet and return a boolean * * @since 1.0 * @param E element which is what will be attempted to be added to the BoundedSet, cannot be null. * @return Boolean: true if added, will return false if element = null, BoundedSet is full, or element has already been added */ public boolean add(E element){ //returns false if element = null, if element is already contained within BoundedSet, or if BoundedSet is full if(element == null) return false; else if(contains(element)) return false; else if(getSize() == size) return false; else { array[getSize()] = element; return true; } } /** * method to remove E element from the BoundedSet and return a boolean * * @since 1.0 * @param E element which is the element to be removed from the BoundedSet * @return Boolean: true if added, false if element = null, or cannot be found */ public boolean remove(E element){ //returns false if element = null if(element == null) return false; if(!contains(element)) return false; else { int removal = findIndex(element); array[removal] = null; return true; } } /** * Method to see whether the BoundedSet contains E element * * @since 1.0 * @param E element which is the element which is searched for within the BoundedSet * @return Boolean: true if element is contained, false if not. */ public boolean contains(E element){ //Calls private method findIndex and returns false if -1 is returned, otherwise returns true if(findIndex(element) == -1) return false; else return true; } /** * Accessor for the capacity of the BoundedSet * * @since 1.0 * @return int representing the capacity of the BoundedSet */ public int capacity(){ return size; } /** * Accessor for the current size of the BoundedSet * * @since 1.0 * @return int representing the current size of the BoundedSet */ public int getSize(){ return occupiedSize(); } /** * toString method which converts BoundedSet into something human readable * * @since 1.0 * @return String of the BoundedSet */ @Override public String toString(){ String set = new String(); set += "["; //loops through BoundedSet and adds each element to String set for(int i = 0; i < size; i++){ set += (array[i] + ", "); } set += "]"; return set; } //Finds where E element is located within BoundedSet private int findIndex(E element){ for(int i = 0; i < size; i++){ if(array[i] == element) return i; } return -1; } //Gets the number of E in the BoundedSet private int occupiedSize(){ int occupied = 0; for(int i = 0; i < size; i++){ if(array[i] != null) occupied++; } return occupied; } public static void main(String[] args){ BoundedSet intSet = new BoundedSet(8); BoundedSet stringSet = new BoundedSet(4); //adds ints to intSet. adds both duplicates and passes capacity intSet.add(14); intSet.add(12); intSet.add(12); intSet.add(52); intSet.add(90); intSet.add(78); intSet.add(2); intSet.add(123); intSet.add(44); intSet.add(50); //adds Strings to stringSet stringSet.add("Hello"); stringSet.add("World"); System.out.println("We have 2 BoundedSets. The first contains ints, the second contains Stirngs"); System.out.println("The first: " + intSet); System.out.println("The second: " + stringSet); System.out.println(); System.out.println("Does intSet contain: 54? " + intSet.contains(54)); System.out.println("Does intSet contain: 78? " + intSet.contains(78)); System.out.println(); System.out.println("What is the capacity of stringSet? " + stringSet.capacity()); System.out.println("What is the number of elements currently contained in stringSet? " + stringSet.getSize()); System.out.println(); stringSet.remove("World"); System.out.println("We have now removed 'World' from stringSet, which looks as follow: " + stringSet); System.out.println(); } }