a collection class is a data type that is capable of holding a group of items. in java, collection...

44
A A Collection class Collection class is is a data type that is a data type that is capable of holding a capable of holding a group of items. group of items. In Java, Collection In Java, Collection classes can be classes can be implemented as a implemented as a class, along with class, along with methods to add, methods to add, remove, and examine remove, and examine items. items. Collection Classes Data Structures Data Structures and Other Objects and Other Objects Using Java Using Java

Upload: nathaniel-caldwell

Post on 04-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

A A Collection class Collection class is a data is a data type that is capable of type that is capable of holding a group of items.holding a group of items.

In Java, Collection classes In Java, Collection classes can be implemented as a can be implemented as a class, along with methods to class, along with methods to add, remove, and examine add, remove, and examine items.items.

Collection ClassesCollection Classes

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing JavaUsing Java

Page 2: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

BagsBags

For the first example, For the first example, think about a bag.think about a bag.

Page 3: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

BagsBags

For the first example, For the first example, think about a bag.think about a bag.

Inside the bag are Inside the bag are some numbers.some numbers.

Page 4: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Initial State of a BagInitial State of a Bag

When you first begin When you first begin to use a bag, the bag to use a bag, the bag will be empty.will be empty.

We count on this to be We count on this to be the the initial stateinitial state of any of any bag that we use.bag that we use.

THIS BAGIS

EMPTY.

Page 5: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Adding Numbers into a BagAdding Numbers into a Bag

Numbers may be Numbers may be added into a bag.added into a bag.

I AMPUTTING THE

NUMBER 4INTO THE

BAG.

Page 6: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Adding Numbers into a BagAdding Numbers into a Bag

Numbers may be Numbers may be added into a bag.added into a bag.

THE 4 ISIN THEBAG.

Page 7: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Adding Numbers into a BagAdding Numbers into a Bag

Numbers may be Numbers may be added into a bag.added into a bag.

The bag can hold The bag can hold many numbers.many numbers.

NOW I'MPUTTINGANOTHER

NUMBER IN THE BAG --

AN 8.

Page 8: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

added Numbers into a Bagadded Numbers into a Bag

Numbers may be Numbers may be added into a bag.added into a bag.

The bag can hold The bag can hold many numbers.many numbers.

THE 8 ISALSO IN

THE BAG.

Page 9: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

added Numbers into a Bagadded Numbers into a Bag

Numbers may be Numbers may be added into a bag.added into a bag.

The bag can hold The bag can hold many numbers.many numbers.

We can even place We can even place the same number the same number more than once.more than once. NOW I'M

PUTTING ASECOND 4

IN THEBAG.

Page 10: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Adding Numbers into a BagAdding Numbers into a Bag

Numbers may be Numbers may be added into a bag.added into a bag.

The bag can hold The bag can hold many numbers.many numbers.

We can even place We can even place the same number the same number more than once.more than once.

NOW THEBAG HASTWO 4'S

AND AN 8..

Page 11: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Examining a BagExamining a Bag

We may ask about We may ask about the contents of the the contents of the bag. bag.

HAVEYOU GOTANY 4's

?

YES,I HAVETWO OFTHEM.

Page 12: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Removing a Number from a BagRemoving a Number from a Bag

We may remove a We may remove a number from a bag. number from a bag.

THIS4 IS

OUTTAHERE!

Page 13: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Removing a Number from a BagRemoving a Number from a Bag

We may remove a We may remove a number from a bag.number from a bag.

But we remove only But we remove only one number at a one number at a time. time.

ONE 4 ISGONE, BUTTHE OTHER4 REMAINS.

Page 14: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

How Many NumbersHow Many Numbers

Another operation is Another operation is to determine how to determine how many numbers are in a many numbers are in a bag.bag.

IN MY OPINION, THERE ARE TOO MANY NUMBERS.

Page 15: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Summary of the Bag OperationsSummary of the Bag Operations

A bag can be put in its A bag can be put in its initial stateinitial state, which , which is an empty bag.is an empty bag.

Numbers can be Numbers can be addedadded into the bag. into the bag. You may check how many You may check how many occurrencesoccurrences of of

a certain number are in the bag.a certain number are in the bag. Numbers can be Numbers can be removedremoved from the bag. from the bag. You can check You can check how manyhow many numbers are in numbers are in

the bag.the bag.

Page 16: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Bag ClassThe Bag Class

Java classes (introduced in Java classes (introduced in Chapter 2) can be used to Chapter 2) can be used to implement a Collection class implement a Collection class such as a Bag.such as a Bag.

The class definition includes:The class definition includes:

public class Bag

The heading of the definition

Page 17: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Bag ClassThe Bag Class

Java classes (introduced in Java classes (introduced in Chapter 2) can be used to Chapter 2) can be used to implement a Collection class implement a Collection class such as a Bag.such as a Bag.

The class definition includes:The class definition includes:

class Bag{ public Bag( )...

The heading of the definition A constructor

Page 18: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Bag ClassThe Bag Class

Java classes (introduced in Java classes (introduced in Chapter 2) can be used to Chapter 2) can be used to implement a Collection class implement a Collection class such as a Bag.such as a Bag.

The class definition includes:The class definition includes:

public class Bag{ public Bag( )... public void add(... public void remove(... ...and so on

The heading of the definition A constructor Public methods

Page 19: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Bag ClassThe Bag Class

Java classes (introduced in Java classes (introduced in Chapter 2) can be used to Chapter 2) can be used to implement a Collection class implement a Collection class such as a Bag.such as a Bag.

The class definition includes:The class definition includes: The heading of the definitionA constructor Public methods Private instance variables We’ll look at private

instance variables later.

We’ll look at privateinstance variables

later.

public class Bag{ public Bag( )... public void add(... public void remove(... ...and so on

Page 20: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Bag’s ConstructorThe Bag’s Constructor

Places a bag in the initial state (an empty Places a bag in the initial state (an empty bag)bag)

// Postcondition: The Bag has been initialized// and it is now empty.public Bag( ){ . . .

}

Page 21: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Add MethodThe Add Method

Adds a new number to the bagAdds a new number to the bag

public void add(int newEntry){

. . .}

Page 22: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Size MethodThe Size Method

Counts how many integers are in the bag.Counts how many integers are in the bag.

// Postcondition: The return value is the number// of integers in the Bag.public int size( ){

. . .}

Page 23: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The countOccurrences MethodThe countOccurrences Method

Counts how many copies of a number occurCounts how many copies of a number occur

// Postcondition: The return value is the number// of copies of target in the Bag.public int countOccurrences(int target){

. . .}

Page 24: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Remove MethodThe Remove Method

Removes one copy of a numberRemoves one copy of a number

// Postcondition: If target was in the Bag, then// one copy of target has been removed from the// Bag, and the return value is true; otherwise the// Bag is unchanged and the return value is false.public boolean remove(int target){

. . .}

Page 25: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Using the Bag in a ProgramUsing the Bag in a Program

Here is typical code from a Here is typical code from a program that uses the new program that uses the new Bag class:Bag class:

Bag ages = new Bag( );

// Record the ages of three children:ages.add(4);ages.add(8);ages.add(4);

Page 26: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Documentation for the Bag ClassDocumentation for the Bag Class

The documentation gives The documentation gives specificationsspecifications for the bag for the bag methods.methods.

Specifications are written as Specifications are written as precondition/postcondition precondition/postcondition contracts.contracts.

Everything needed to Everything needed to useuse the the Bag class is included in this Bag class is included in this documentation.documentation.

Bag’s documentationcan be automatically

created with the Javadoc tooldescribed inAppendix H.

Page 27: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

A QuizA Quiz

Suppose that a Mysterious Suppose that a Mysterious Benefactor provides you Benefactor provides you with the Bag class, but you with the Bag class, but you are only permitted to read are only permitted to read the documentation. You the documentation. You cannot read the class cannot read the class implementation or .java implementation or .java file. Can you write a file. Can you write a program that uses the Bag program that uses the Bag data type ?data type ?

Yes I can.Yes I can. No. Not unless I see the No. Not unless I see the

class implementation for class implementation for the Bag.the Bag.

Page 28: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

A QuizA Quiz

Suppose that a Mysterious Suppose that a Mysterious Benefactor provides you Benefactor provides you with the Bag class, but you with the Bag class, but you are only permitted to read are only permitted to read the documentation. You the documentation. You cannot read the class cannot read the class implementation or .java implementation or .java file. Can you write a file. Can you write a program that uses the Bag program that uses the Bag data type ?data type ?

Yes I can.Yes I can.

You know the name of the You know the name of the new data type, and you new data type, and you also know the headings also know the headings and specifications of each and specifications of each of the operations. This is of the operations. This is enough for you to create enough for you to create and use Bags. and use Bags.

Page 29: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Implementation DetailsImplementation Details

The entries of a bag The entries of a bag will be stored in the will be stored in the front part of an array, front part of an array, as shown in this as shown in this example. example.

[ 0 ][ 0 ] [1][1] [ 2 ][ 2 ] [ 3 ][ 3 ] [ 4 ][ 4 ] [ 5 ][ 5 ] . . .. . .

An array of integersAn array of integers

4 8 4

We don't care what's inWe don't care what's inthis part of the array.this part of the array.

Page 30: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Implementation DetailsImplementation Details

The entries may The entries may appear in any order. appear in any order. This represents the This represents the same bag as the same bag as the previous one. . . previous one. . .

An array of integersAn array of integers

4 4 8

We don't care what's inWe don't care what's inthis part of the array.this part of the array.

[ 0 ][ 0 ] [1][1] [ 2 ][ 2 ] [ 3 ][ 3 ] [ 4 ][ 4 ] [ 5 ][ 5 ] . . .. . .

Page 31: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Implementation DetailsImplementation Details

. . . and this also . . . and this also represents the same represents the same bag.bag.

An array of integersAn array of integersWe don't care what's inWe don't care what's inthis part of the array.this part of the array.

[ 0 ][ 0 ] [1][1] [ 2 ][ 2 ] [ 3 ][ 3 ] [ 4 ][ 4 ] [ 5 ][ 5 ] . . .. . .

4 4 8

Page 32: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Implementation DetailsImplementation Details

We also need to keep track of how We also need to keep track of how many numbers are in the bag.many numbers are in the bag.

An array of integersAn array of integers

8 4 4

We don't care what's inWe don't care what's inthis part of the array.this part of the array.

An integer to keepAn integer to keeptrack of the bag's sizetrack of the bag's size

3

[ 0 ][ 0 ] [1][1] [ 2 ][ 2 ] [ 3 ][ 3 ] [ 4 ][ 4 ] [ 5 ][ 5 ] . . .. . .

Page 33: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

An ExerciseAn Exercise

Use these ideas to write a Use these ideas to write a list of private instance list of private instance variables could implement variables could implement the Bag class. You should the Bag class. You should have two instance have two instance variables. variables.

You have 60 secondsto write the declaration.

Page 34: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

An ExerciseAn Exercise

public class Bag{ private int[ ] data; private int manyItems; ...}

One solution:One solution:

Page 35: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

An Example of Calling AddAn Example of Calling Add

public void add(int newEntry)

Before calling add, weBefore calling add, wemight have this bag b:might have this bag b:

2

[ 0 ][ 0 ] [ 1 ][ 1 ] [2][2] . . .. . .

8 4b.datab.data

b.manyItemsb.manyItems

Page 36: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

An Example of Calling AddAn Example of Calling Add

void add(int newEntry)

b.datab.data

b.manyItemsb.manyItems

We activateWe activateb.add(17)b.add(17)

What values will be inb.data and b.manyItemsafter the methodfinishes ?

[ 0 ][ 0 ] [ 1 ][ 1 ] [2][2] . . .. . .

8 4

public void add(int newEntry)

2

Page 37: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

An Example of Calling AddAn Example of Calling Add

void add(int newEntry)

After activating b.add(17),After activating b.add(17),we will have this bag b:we will have this bag b:

33

[ 0 ][ 0 ] [1][1] [ 2 ][ 2 ] . . .. . .

8 4 1717

public void add(int newEntry)

b.datab.data

b.manyItemsb.manyItems

[ 0 ][ 0 ] [ 1 ][ 1 ] [2][2] . . .. . .

8 4

2

Page 38: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Pseudocode for addPseudocode for add

Make sure there is room for a new entry in Make sure there is room for a new entry in the array.the array.

Place Place newEntrynewEntry in the appropriate location in the appropriate location of the of the datadata array. array.

Add one to the instance variable Add one to the instance variable manyItemsmanyItems.. What is the “appropriate

location” of the data array ?

Page 39: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Pseudocode for addPseudocode for add

data[manyItems] = newEntry;manyItems++;

Make sure there is room for a new entry in Make sure there is room for a new entry in the array.the array.

Place Place newEntrynewEntry in the appropriate location in the appropriate location of the of the datadata array. array.

Add one to the instance variable Add one to the instance variable manyItemsmanyItems..

Page 40: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Pseudocode for addPseudocode for add

data[ manyItems++] = newEntry;

Make sure there is room for a new entry in Make sure there is room for a new entry in the array.the array.

Place Place newEntrynewEntry in the appropriate location in the appropriate location of the of the datadata array. array.

Add one to the instance variable Add one to the instance variable manyItemsmanyItems..

Page 41: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

The Other Bag OperationsThe Other Bag Operations

Read Section 3.2 for the implementations of the Read Section 3.2 for the implementations of the other bag methods.other bag methods.

Remember: If you are just Remember: If you are just usingusing the Bag class, the Bag class, then you don’t need to know how the operations then you don’t need to know how the operations are implemented.are implemented.

Later we will Later we will reimplementreimplement the bag using more the bag using more efficient algorithms. efficient algorithms.

We’ll also have a few other operations to We’ll also have a few other operations to manipulate bags.manipulate bags.

Page 42: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

Other Kinds of BagsOther Kinds of Bags

In this example, we have implemented a In this example, we have implemented a bag containing bag containing integersintegers..

But we could have had a bag of But we could have had a bag of float float numbersnumbers, a bag of , a bag of characterscharacters, a bag of , a bag of StringsStrings . . . . . .Suppose you wanted one of these other bags. How much would you need to change in the implementation ?

Page 43: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

A Collection class is a class that can hold a group A Collection class is a class that can hold a group of items.of items.

Collection classes can be implemented with a Java Collection classes can be implemented with a Java class.class.

The author of the class should provide The author of the class should provide documentation that another programmer can read documentation that another programmer can read to use the class.to use the class.

Other details are given in Section 3.2, which you Other details are given in Section 3.2, which you should read.should read.

Summary Summary

Page 44: A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with

THE ENDTHE END

Presentation copyright 1999, Addison Wesley LongmanPresentation copyright 1999, Addison Wesley LongmanFor use with For use with Data Structures and Other Objects Using JavaData Structures and Other Objects Using Javaby Michael Main.by Michael Main.

Some artwork in the presentation is used with permission from Presentation Task ForceSome artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image ClubCorel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image ClubGraphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).

Students and instructors who use Students and instructors who use Data Structures and Other ObjectsData Structures and Other Objects Using Java Using Java arearewelcome to use this presentation however they see fit, so long as this copyright notice welcome to use this presentation however they see fit, so long as this copyright notice remains intact.remains intact.