Class Bag - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Class Bag

Description:

public class Bag // Student must comment properly! // this could be a variable ... Comment out the constructor. 12. Usage of the Bag Class. Bag myBag = new Bag ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 20
Provided by: qya
Category:
Tags: bag | class | comment

less

Transcript and Presenter's Notes

Title: Class Bag


1
Class 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()
. . . ...
2
Method getCount()
public int getCount() return num
Should num be public? NO! Access Methods
getCount() Modification Methods add() and
remove() No setCount()!
3
Usage 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!
4
Usage 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?
5
Usage of the Bag Class
Bag myBag new Bag() Object obj new
Object() // Will it work? myBag.add( obj ) No
default constructor!
6
Class 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.
7
Usage 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.
8
Class 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
...
9
Initializing 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
...
10
Initializing 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 ...
11
Initializing Array
public class Bag private final int GROWBY
3 private int num private Object
list public Bag() list new
ObjectGROWBY num 0 ...
12
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
13
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
14
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. Is it efficient?
15
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
16
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
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

19
Schedule
  • Quiz2 Next Monday
  • Test 1 Next Friday
  • Class on Friday
Write a Comment
User Comments (0)
About PowerShow.com