COMPUTER 2430 Object Oriented Programming and Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I 1
Container Classes • Bag • Set • Sorted List • . . . 2
Class Bag • Container • Can grow, but never shrink • The order of array elements is not maintained • May or may not allow duplicate elements 3
public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int num. Objects = 0; public void add. Object( Object obj ) public boolean remove. Object( Object obj ) private int find ( Object obj ) @Override public String to. String () private void grow () } 4
public class CS 2430 Sx { public static void main(String[] args) { Bag my. Bag = new Bag(); Object obj = new Object(); // Valid? my. Bag. add. Object(obj); // Valid? my. Bag. add. Object(new Date()); // Valid? my. Bag. add. Object(new Student(“Frank”)); } } 5
public class Bag { private Object[] bag = new Object[INIT_SIZE]; private int num. Objects = 0; public void add. Object( Object obj ) { if (num. Objects == bag. length) grow(); bag[num. Objects ++] = obj; } private void grow () { int size = bag. length + GROWBY; Object[] temp = new Object[size]; for (int i = 0; i < num. Objects; i ++) temp[i] = bag[i]; bag = temp; } } 6
public class Bag {. . . // Allow duplicate? // Yes public void add. Object( Object obj ) { if (num. Objects == bag. length) grow(); bag[num. Objects ++] = obj; } } 7
public class Bag {. . . // What if duplicates are not allowed? public boolean add. Object( Object obj ) { if (find(obj) != -1) return false; if (num. Objects == bag. length) grow(); bag[num. Objects ++] = obj; return true; } private void grow () private int find( Object obj ) } 8
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 < num. Objects; i ++) if (bag[i]. equals(obj)) return i; return -1; } } It works for all classes: Polymorphism! 9
public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int num. Objects = 0; // Maintain order public boolean remove. Object( Object obj ) { int index = find (obj); if (index > -1) { -- num. Objects; for (int i = index; i < num. Objects; i ++) bag[i] = bag[i + 1]; return true; } return false; } } 10
// Maintain order public boolean remove. Object( 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 ) { -- num. Objects; for (int i = index; i < num. Objects; i ++) bag[i] = bag[i + 1]; } 11
public class Bag { private final int INIT_SIZE = 400000; private final int GROWBY = 500; private Object[] bag = new Object[INIT_SIZE]; private int num. Objects = 0; // Do not maintain order public boolean remove. Object( Object obj ) { int index = find (obj); if (index > -1) { bag[index] = bag[-- num. Objects]; return true; } return false; } } 12
// Remove one object from the array public boolean remove. Object( Object obj ) { int index = find (obj); if (index > -1) { delete(index); return true; } return false; } 13
// Remove all objects from the array that equal obj public int remove. Object( Object obj ) { int count = 0; int index = find (obj); while (index > -1) { delete(index); count ++; index = find(obj); } return count; } 14
public class Bag { private final int INIT_SIZE = 10000; private final int GROWBY = 100; private Object[] bag = new Object[INIT_SIZE]; private int num. Objects = 0; public void add. Object( Object obj ) private void grow () public boolean remove. Object( Object obj ) public int find ( Object obj ) @Override public String to. String () } Any Other Meaningful Bag Methods? 15
public class Bag { private final int INIT_SIZE = 10000; private final int GROWBY = 100; private Object[] bag = new Object[INIT_SIZE]; private int num. Objects = 0; . . . public boolean is. Empty() { return num. Objects == 0; } public int get. Count () { return num. Objects; } } 16
public class Bag { private Object[] bag = new Object[INIT_SIZE]; public Object get. Item ( Object obj ) { int index = find(obj); if ( index != -1 ) return bag[index]; return null; } } Why we need this method? 17
Golf. League the. League = new Golf. League(); Golf. League. Member member = new Senior. Member(“Qi”, 80); System. out. println(member. to. String()); // The scores and handicap for Qi has changed. . . member = the. League. get. Item(member); System. out. println(member. to. String()); 18
Sorted. List 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 Sorted. List methods, but the list must be in sorted order. 19
Method add 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! 20
Method add 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 21
Method delete index 0 1 2 num-1 … … length-1 … … Find the item (index) to be deleted Move items up one position Update num 22
Sort Method 23
Search Method 24
Quiz 2 25
Lab 3 Submission 26
Prog 2 27
- Slides: 27