Download presentation
Presentation is loading. Please wait.
Published bySri Darmali Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
2
Sets A set is a collection of elements. Not sorted. No duplicates!
Can grow. Examples A = {1, 2, 3, 4} B = {2, 5} C = {1, 3, 5, 7, 9, 3} Not a Set!
3
Set Class in Java public class Set { private final int INIT_SIZE = 5;
private final int GROWBY = 5; private Object[] list = new Object[INIT_SIZE]; private int num = 0; // Do we need the default constructor? public Set() } // Constructor with a given size public Set(int size) list = new Object[size];
4
Copy Constructor public class Set {
private Object[] list = new Object[INIT_SIZE]; private int num = 0; public Set() public Set(int size) // Copy constructor public Set(Set s) this.list = new Object[s.length]; for (int i = 0; i < s.num; i ++) this.list[i] = s.list[i]; this.num = s.num; }
5
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean add( Object obj ) if (find(obj) != -1) return false; if (num == list.length) grow(); list[num ++] = obj; // list[num] = obj; num ++; return true; }
6
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean remove( Object obj ) if (find(obj) == -1) return false; list[find(obj)] = list[num - 1]; num --; return true; }
7
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean remove( Object obj ) int index = find(obj); if (index == -1) return false; list[index] = list[num - 1]; num --; return true; }
8
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean remove( Object obj ) int index = find(obj); if (index == -1) return false; num --; list[index] = list[num]; return true; }
9
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; private int find( Object obj ) private void grow() public boolean remove( Object obj ) int index = find(obj); if (index == -1) return false; list[index] = list[-- num]; return true; }
10
public class Set { private final int INIT_SIZE = 5; private final int GROWBY = 5; private Object[] list = new Object[INIT_SIZE]; private int num = 0; // constructors private int find( Object obj ) private void grow() public boolean add( Object obj ) public boolean remove( Object obj ) public boolean isEmpty() public int getCount() public Object getItem( Object obj ) }
11
New Methods for Set Class
contains intersection union difference isSubsetOf . . .
12
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage examples: if (setA.contains(obj)) */ public boolean contains( Object obj ) return find(obj) != -1; } private int find( Object obj )
13
Set Intersection A = {1, 2, 3, 4} B = {2, 5}
A B = {x: x A and x B} = {2} = B A
14
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.intersection(setB) Does not change either this or the parameter s! */ public Set intersection(Set s) Set temp = new Set(num); for (int i = 0; i < num; i ++) if (s.conatins(list[i])) temp.add(list[i]) return temp; }
15
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.intersection(setB) Does not change either this or the parameter s! */ public Set intersection(Set s) Set temp = new Set(s.num); for (int i = 0; i < s.num; i ++) if (this.conatins(s.list[i])) temp.add(s.list[i]) return temp; }
16
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.intersection(setB) Does not change either this or the parameter s! */ public Set intersection(Set s) Set temp = new Set(); // will it work? for (int i = 0; i < num; i ++) if (s.conatins(list[i])) temp.add(list[i]) return temp; }
17
Set Union A = {1, 2, 3, 4} B = {2, 5} A B = {x: x A or x B}
= {1, 2, 3, 4, 5} = B A
18
Does it work? Other Better Way?
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.union(setB) Does not change either this or the parameter s! */ public Set union(Set s) Set temp = new Set(this); for (int i = 0; i < s.num; i ++) if (!this.contains(s.list[i])) temp.add(s.list[i]) return temp; } Does it work? Other Better Way?
19
Better and Do it this Way!
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.union(setB) Does not change either this or the parameter s! */ public Set union(Set s) Set temp = new Set(this); for (int i = 0; i < s.num; i ++) temp.add(s.list[i]) return temp; } Does it work? Better and Do it this Way!
20
Set Difference A = {1, 2, 3, 4} B = {2, 5}
A – B = {x: x A but x B} = {1, 3, 4} B- A B – A = {5}
21
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: setC = setA.difference(setB) Does not change either this or the parameter s! */ public Set difference(Set s) Set temp = new Set(this); for (int i = 0; i < s.num; i ++) temp.remove(s.list[i]) return temp; }
22
Subset For two sets A and B and any element x, if x A then x B
Then A is a subset of B A B or B A A = {1, 2, 3, 4} B = {2, 5} C = {1, 3} B A? C A?
23
public class Set { private Object[] list = new Object[INIT_SIZE]; private int num = 0; /** Usage example: if (setA.isSubsetOf(setB)) Does not change either this or the parameter s! */ public boolean isSubsetOf(Set s) for (int i = 0; i < num; i ++) if (! s.contains(list[i]) return false; return true; }
24
Prog 2 Friday: Class in Lab 206
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.