Top Banner
Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano
23

Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Jan 02, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Bags

Chapter 1

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Data Structures and Abstractions with Java, 4e Frank Carrano

Page 2: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

The ADT Bag

• Definition− A finite collection of objects in no particular

order− Can contain duplicate items

• Possible behaviors− Get number of items− Check for empty− Add, remove objects

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 3: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

CRC Card

FIGURE 1-1 A CRC card for a class Bag

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 4: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Specifying a Bag

• Describe its data and specify in detail the methods

• Options that we can take when add cannot complete its task:− Do nothing− Leave bag unchanged, but signal client

• Note which methods change the object and which do not

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 5: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

UML Notation

FIGURE 1-2 UML notation for the class Bag

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 6: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Design Decision

What to do for unusual conditions?

•Assume it won’t happen

•Ignore invalid situations

•Guess at the client’s intention

•Return value that signals a problem

•Return a boolean

•Throw an exception

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 7: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

An Interface

LISTING 1-1 A Java interface for a class of bags

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 8: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

An Interface

LISTING 1-1 A Java interface for a class of bags

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 9: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

An Interface

LISTING 1-1 A Java interface for a class of bags

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 10: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Using the ADT Bag

LISTING 1-2 A program that maintains a bag for online shopping

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 11: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Using the ADT Bag

LISTING 1-2 A program that maintains a bag for online shopping

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 12: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Using the ADT Bag

LISTING 1-2 A program that maintains a bag for online shopping

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 13: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Example: A Piggy Bank

LISTING 1-3 A class of piggy banks

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 14: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Example: A Piggy Bank

LISTING 1-3 A class of piggy banks

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 15: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Example: A Piggy Bank

LISTING 1-4 A demonstration of the class PiggyBank

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 16: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Example: A Piggy Bank

LISTING 1-4 A demonstration of the class PiggyBank

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 17: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Example: A Piggy Bank

LISTING 1-4 A demonstration of the class PiggyBank

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 18: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Using ADT Like Using Vending Machine

FIGURE 1-3 A vending machine

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 19: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Observations about Vending Machines

• Can perform only tasks machine’s interface presents.

• You must understand these tasks

• Cannot access the inside of the machine

• You can use the machine even though you do not know what happens inside.

• Usable even with new insides.

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 20: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Observations about ADT Bag

• Can perform only tasks specific to ADT

• Must adhere to the specifications of the operations of ADT

• Cannot access data inside ADT without ADT operations

• Use the ADT, even if don’t know how data is stored

• Usable even with new implementation.© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 21: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Java Class Library: The Interface Set

Listing 1-5 A Java interface for a class of sets

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 22: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

Java Class Library: The Interface Set

Listing 1-5 A Java interface for a class of sets

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Page 23: Bags Chapter 1 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.

End

Chapter 1

© 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.