COMPUTER 2430 Object Oriented Programming and Data Structures I
Container Classes Bag Set Sorted List . . .
Class Bag Container Can grow, but never shrink The order of array elements is not maintained May or may not allow duplicate elements
public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) public boolean removeObject( Object obj ) private int find ( Object obj ) @Override public String toString () private void grow () }
public class CS2430Sx { public static void main(String[] args) Bag myBag = new Bag(); Object obj = new Object(); // Valid? myBag.addObject(obj); myBag.addObject(new Date()); myBag.addObject(new Student(“Frank”)); }
public class Bag { private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; } private void grow () int size = bag.length + GROWBY; Object[] temp = new Object[size]; for (int i = 0; i < numObjects; i ++) temp[i] = bag[i]; bag = temp;
public class Bag { . . . // Allow duplicate? // Yes public void addObject( Object obj ) if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; }
public class Bag { . . . // What if duplicates are not allowed? public boolean addObject( Object obj ) if (find(obj) != -1) return false; if (numObjects == bag.length) grow(); bag[numObjects ++] = obj; return true; } private void grow () private int find( Object obj )
It works for all classes: Polymorphism! public class Bag { private Object[] bag = new Object[INIT_SIZE]; /** Finds and returns the index of element in array bag that equals obj. @param obj the object to be found @return the index of the element if found, -1 otherwise */ private int find( Object obj ) for (int i = 0; i < numObjects; i ++) if (bag[i].equals(obj)) return i; return -1; } It works for all classes: Polymorphism!
public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; // Maintain order public boolean removeObject( Object obj ) int index = find (obj); if (index > -1) -- numObjects; for (int i = index; i < numObjects; i ++) bag[i] = bag[i + 1]; return true; } return false;
// Maintain order public boolean removeObject( Object obj ) { int index = find (obj); if (index > -1) delete(index); return true; } return false; // Quiz 2 // Index is in the range and the operation will be successful! public void delete ( int index ) -- numObjects; for (int i = index; i < numObjects; i ++) bag[i] = bag[i + 1];
public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; // Do not maintain order public boolean removeObject( Object obj ) int index = find (obj); if (index > -1) bag[index] = bag[-- numObjects]; return true; } return false;
// Remove one object from the array public boolean removeObject( Object obj ) { int index = find (obj); if (index > -1) delete(index); return true; } return false;
// Remove all objects from the array that equal obj public int removeObject( Object obj ) { int count = 0; int index = find (obj); while (index > -1) delete(index); count ++; index = find(obj); } return count;
Any Other Meaningful Bag Methods? public class Bag { private final int INIT_SIZE = 10000; private final int GROWBY = 100; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; public void addObject( Object obj ) private void grow () public boolean removeObject( Object obj ) public int find ( Object obj ) @Override public String toString () } Any Other Meaningful Bag Methods?
public class Bag { private final int INIT_SIZE = 10000; private final int GROWBY = 100; private Object[] bag = new Object[INIT_SIZE]; private int numObjects = 0; . . . public boolean isEmpty() return numObjects == 0; } public int getCount () return numObjects;
public class Bag { private Object[] bag = new Object[INIT_SIZE]; public Object getItem ( Object obj ) int index = find(obj); if ( index != -1 ) return bag[index]; return null; } Why we need this method?
GolfLeague theLeague = new GolfLeague(); GolfLeagueMember member = new SeniorMember(“Qi”, 80); System.out.println(member.toString()); // The scores and handicap for Qi has changed . . . member = theLeague.getItem(member);
SortedList Class The data declaration can be the same as that for the Bag. private Object[] list = new Object[INIT_SIZE]; private int num = 0; private final int GROWBY = 3; All the Bag methods we wrote would also work as SortedList methods, but the list must be in sorted order.
Method add Assume in ascending order. Where to insert a new object? index … 0 1 i i+1 num-1 length-1 Assume in ascending order. Where to insert a new object? list[0] < obj list[1] < obj . . . list[i] < obj list[i+1] > obj Need a comparison method!
Method add Find the position to be added Move items down one position index 0 1 2 num-1 length-1 … New item … Find the position to be added Move items down one position Add the new item Update num
Method delete Find the item (index) to be deleted 0 1 2 num-1 length-1 … … Find the item (index) to be deleted Move items up one position Update num
Sort Method
Search Method
Quiz 2
Lab 3 Submission
Prog 2