Bags
Chapter 1

Copyright ©2012 by Pearson Education, Inc. All rights reserved
Contents
• The Bag
 A Bag’s Behaviors

• Specifying a Bag
 An Interface

• Using the ADT Bag
• Using an ADT Is Like Usin...
Objectives
• Describe the concept of abstract data type
(ADT)
• Describe ADT bag
• Use ADT bag in Java program

Copyright ...
Definition: Bag
• A finite collection of objects
• In no particular order
• May contain duplicate items

Copyright ©2012 b...
Behaviors
• Determine how many objects in bag
 Full?
 Empty?

•
•
•
•

Add, remove objects
Count duplicates
Test for spe...
Figure 1-1 A CRC card for a class Bag
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Specifying a Bag
• Describe data
• Specify methods for bag’s behaviors
 Name methods
 Choose parameters
 Decide return ...
Figure 1-2 UML notation for the class Bag
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Design Decisions
• What should the method add do when it
cannot add a new entry?
 Nothing?
 Leave bag unchanged, signal ...
Design Decisions
• What should happen when an unusual
condition occurs?
 Assume invalid never happens?
 Ignore invalid e...
Interface
• Write Java headers
• Organize into interface
• Note items in bag are of same type
 Generic type <T>
• View Li...
Using ADT Bag
• Implementation done from specifications
 User needs know what ADT does, not how

• Type of object in bag ...
Using ADT Bag
• Example of Bag for class of piggy banks
Listing 1-3
• Demonstration of class PiggyBank
Listing 1-4

Copyri...
Figure 1-3 A Vending Machine
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Vending Machine Like An ADT
•
•
•
•

Perform only available tasks
User must understand the tasks
Cannot access inside of m...
Class Library
• The interface Set
public
public
public
public
public
public
public

boolean add(T newEntry)
boolean remove...
End
Chapter 1

Copyright ©2012 by Pearson Education, Inc. All rights reserved
Upcoming SlideShare
Loading in...5
×

Chapter01 bags

277

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
277
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter01 bags

  1. 1. Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved
  2. 2. Contents • The Bag  A Bag’s Behaviors • Specifying a Bag  An Interface • Using the ADT Bag • Using an ADT Is Like Using a Vending Machine Copyright ©2012 by Pearson Education, Inc. All rights reserved
  3. 3. Objectives • Describe the concept of abstract data type (ADT) • Describe ADT bag • Use ADT bag in Java program Copyright ©2012 by Pearson Education, Inc. All rights reserved
  4. 4. Definition: Bag • A finite collection of objects • In no particular order • May contain duplicate items Copyright ©2012 by Pearson Education, Inc. All rights reserved
  5. 5. Behaviors • Determine how many objects in bag  Full?  Empty? • • • • Add, remove objects Count duplicates Test for specific object View all objects Copyright ©2012 by Pearson Education, Inc. All rights reserved
  6. 6. Figure 1-1 A CRC card for a class Bag Copyright ©2012 by Pearson Education, Inc. All rights reserved
  7. 7. Specifying a Bag • Describe data • Specify methods for bag’s behaviors  Name methods  Choose parameters  Decide return types  Write comments Copyright ©2012 by Pearson Education, Inc. All rights reserved
  8. 8. Figure 1-2 UML notation for the class Bag Copyright ©2012 by Pearson Education, Inc. All rights reserved
  9. 9. Design Decisions • What should the method add do when it cannot add a new entry?  Nothing?  Leave bag unchanged, signal client of condition? Copyright ©2012 by Pearson Education, Inc. All rights reserved
  10. 10. Design Decisions • What should happen when an unusual condition occurs?  Assume invalid never happens?  Ignore invalid event?  Guess at client’s intention?  Return flag value?  Return boolean value – success/failure?  Throw exception? Copyright ©2012 by Pearson Education, Inc. All rights reserved
  11. 11. Interface • Write Java headers • Organize into interface • Note items in bag are of same type  Generic type <T> • View Listing 1-1 Note: Code listing files Note: Code listing files must be in same must be in same folder folder as PowerPoint files as PowerPoint files for links to work for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved
  12. 12. Using ADT Bag • Implementation done from specifications  User needs know what ADT does, not how • Type of object in bag specified by program using the ADT • Example of Bag for online shopping Listing 1-2 Copyright ©2012 by Pearson Education, Inc. All rights reserved
  13. 13. Using ADT Bag • Example of Bag for class of piggy banks Listing 1-3 • Demonstration of class PiggyBank Listing 1-4 Copyright ©2012 by Pearson Education, Inc. All rights reserved
  14. 14. Figure 1-3 A Vending Machine Copyright ©2012 by Pearson Education, Inc. All rights reserved
  15. 15. Vending Machine Like An ADT • • • • Perform only available tasks User must understand the tasks Cannot access inside of mechanism Usable without knowing inside implementation • New inside implementation unknown to users Copyright ©2012 by Pearson Education, Inc. All rights reserved
  16. 16. Class Library • The interface Set public public public public public public public boolean add(T newEntry) boolean remove(Object anEntry) void clear() boolean contains(Object anEntry) boolean isEmpty() int size() Object[] toArray() Copyright ©2012 by Pearson Education, Inc. All rights reserved
  17. 17. End Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×