Growable Bag Continued - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Growable Bag Continued

Description:

Method RemoveAll // Removes all objects from list that equal obj ... Method Locate. Reference to one element in the bag that equals obj ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 21
Provided by: qya
Category:

less

Transcript and Presenter's Notes

Title: Growable Bag Continued


1
Growable Bag Continued
2
public 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 ...
3
Methods
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
4
Method 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
5
Method 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!
6
Method 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 )
7
Method 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?
8
Method 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!
9
Efficient 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
10
Public 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

11
Public 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.
12
Method 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?
13
Method 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
14
Method Count
public int count ( Object obj ) int numTimes
0 while ( find( obj ) gt0 ) numTimes
return numTimes Will it terminate?
15
Method 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.
16
Method 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
17
Object 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 )

18
Testing
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
19
Debugging
  • 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

20
Debugging
  • See the value of a variable
  • Scope
  • Local variables
  • Watches
  • Run/Evaluate Expression
  • Mouse over
Write a Comment
User Comments (0)
About PowerShow.com