Title: Class Bag
1Class Bag
public class Bag private final int GROWBY
3 private int num 0 private Object
list //Class Object! public Bag ( int size
) if ( size lt GROWBY ) list
new ObjectGROWBY else list
new Objectsize public void add (
Object obj ) . . . private void grow()
. . . ...
2Method getCount()
public int getCount() return num
Should num be public? NO! Access Methods
getCount() Modification Methods add() and
remove() No setCount()!
3Usage of the Bag Class
Bag myBag Object obj // Whats the
count? System.out.println(Count
myBag.getCount()) // No object! myBag new
Bag(-1) // Whats the count? System.out.println(
Count myBag.getCount()) myBag.add( obj
) // Whats the count? System.out.println(Count
myBag.getCount()) // No object!
4Usage of the Bag Class
Bag myBag Object obj myBag new Bag(10) //
Whats the count? System.out.println(Count
myBag.getCount()) Obj new Object() myBag.add(
obj ) // Whats the count? System.out.println(C
ount myBag.getCount()) myBag.add( new
Date() ) System.out.println(Count
myBag.getCount()) // Can we do this? // Whats
the count?
5Usage of the Bag Class
Bag myBag new Bag() Object obj new
Object() // Will it work? myBag.add( obj ) No
default constructor!
6Class Bag
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 / ... Comment out the
constructor.
7Usage of the Bag Class
Bag myBag new Bag() Object obj new
Object() // Whats the count? System.out.println
(Count " myBag.getCount()) // Will it
work? myBag.add( obj ) NullPointerException! Arr
ay list is not initialized! The default
constructor sets it to null.
8Class Bag
public class Bag private final int GROWBY
3 private int num 0 private Object
list // Assuming list points to an array
object (not null) public void add ( Object obj
) if ( num list.length )
grow() listnum obj
...
9Initializing Array
public class Bag private final int GROWBY
3 private int num 0 private Object
list ... public void add ( Object obj )
if ( list null ) list new
ObjectGROWBY if ( num list.length )
grow() listnum obj
...
10Initializing Array
public class Bag private final int GROWBY
3 private int num 0 private Object
list new ObjectGROWBY ... public void
add ( Object obj ) if ( num
list.length ) grow()
listnum obj ...
11Initializing Array
public class Bag private final int GROWBY
3 private int num private Object
list public Bag() list new
ObjectGROWBY num 0 ...
12Method 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
13Method 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
14Method 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. Is it efficient?
15Efficient 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
16Testing
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
17- public void testAverage()
-
- System.out.println("average")
- ScoresList instance new ScoresList()
- float expResult 0.0F
- float result instance.average()
- assertEquals(expResult, result, 0.0)
- // TODO review the generated test code and
- // remove the default call to fail.
- fail("The test case is a prototype.")
18- public void testAverage()
-
- System.out.println("average")
- ScoresList instance new ScoresList()
- float expResult 0.0F
- float result instance.average()
- assertEquals(expResult, result, 0.0)
- instance.appendScore( 55.5F )
- expResult 55.5F
- result instance.average()
- assertEquals(expResult, result, 0.001)
- // result between 55.499 and 55.501 are good
19Schedule
- Quiz2 Next Monday
- Test 1 Next Friday
- Class on Friday