Title: Growable Bag Continued
1Growable Bag Continued
2public class Bag private final int GROWBY
3 private int num 0 private Object
list public Bag ( int size ) if
( size lt GROWBY ) list new
ObjectGROWBY else list new
Objectsize // Default constructor
public Bag () list new
ObjectGROWBY ...
3Methods
public void add ( Object obj ) if ( num
list.length ) grow() listnum
obj private void grow() Object temp
new Objectlist.length GROWBY for (int i
0 i lt num i ) tempi listi
list temp public int getCount() return
num
4Method Find
// Private method to be used by implementer //
inside the class private int find ( Object obj
) for (int i 0 i lt num i ) if (
listi.equals( obj ) ) return i
return -1 Write it the dirty way
5Method Remove
// Removes one object from list that equals
obj public void remove ( Object obj ) int
index find( obj ) if ( index gt 0 )
listindex list--num Does not preserve
order! Preserve order move items up!
6Method Remove
// Removes one object from list that equals
obj public boolean remove ( Object obj ) int
index find( obj ) if ( index gt 0 )
listindex list--num return
true return false Different
signatures, different methods. public void remove
( Object obj )
7Method RemoveAll
// Removes all objects from list that equal
obj public void removeAll( Object obj ) int
index find( obj ) while ( index gt 0 )
listindex list--num index
find( obj ) Zero or more objects will be
removed. How many are removed? Is it efficient?
8Method RemoveAll
// Removes all objects from list that equal
obj public int removeAll( Object obj ) int
count 0, index find( obj ) while ( index
gt 0 ) listindex list--num
count index find( obj )
return count The user will know how many have
been removed. Not Efficient!
9Efficient Implementation of RemoveAll
// Removes all objects from list that equal
obj public int removeAll( Object obj ) int
index 0, count 0 while ( index lt num )
if ( listindex.equals(obj) )
listindex list--num count
else index
return count
10Public Search Method
- Return Index?
- No!
- Boolean method returns true or false
- Integer count method number of time in the bag
- Reference to one element in the bag that equals
obj
11Public Search
Boolean method returns true or false What should
the name be? theBag.???( obj ) theBag.contains(
obj )! public boolean contains( Object obj )
return find( obj ) gt 0 Dont know where it
is. Dont know number of times. But its in the
bag.
12Method Count
Integer count method number of time in the
bag public int count ( Object obj ) int
numTimes 0 for (int i 0 i lt num i )
if (listi.equals( obj )) numTimes
return numTimes Is it good?
13Method Count
Integer count method number of time in the
bag public int count ( Object obj ) int
numTimes 0 for (int i 0 i lt num i )
if (listi.equals( obj )) numTimes
return numTimes
14Method Count
public int count ( Object obj ) int numTimes
0 while ( find( obj ) gt0 ) numTimes
return numTimes Will it terminate?
15Method Locate
Reference to one element in the bag that equals
obj public Object locate( Object obj ) int
pos find( obj ) if ( pos gt 0 )
return listpos return null Is it
useful? Is listpos the same as obj? Depending
on method equals.
16Method Find
// Private method to be used by implementer //
inside the class private int find ( Object obj
) for (int i 0 i lt num i ) if (
listi.equals( obj ) ) return i
return -1
17Object Class and Method equals
- Object class has a method equals testing
- Every class is a sub-class of Object and inherits
method equals - Most likely, method equals has been overridden
- Example class Student overrides equals to test
the same ID - obj has only ID set when calling locate
- public Object locate( Object obj )
18Testing
Test-bed main The entire class As
implementer Can access private data/methods Check
results
Junit Individual methods As User Can NOT access
private data/methods assertEquals assertTrue asse
rtFalse
19Debugging
- Break point
- Where you want to debug
- Step over
- To the next statement
- Step into
- To inside the method being called
- Method call
- As one statement by Step over
- Without method call
- Step into the same as Step over
- Continue
- To next break point
20Debugging
- See the value of a variable
- Scope
- Local variables
- Watches
- Run/Evaluate Expression
- Mouse over