SlideShare a Scribd company logo
1 of 17
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 Using a Vending
Machine

Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
Definition: Bag
• A finite collection of objects
• In no particular order
• May contain duplicate items

Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
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 types
 Write comments

Copyright ©2012 by Pearson Education, Inc. All rights reserved
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 client of

condition?

Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
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
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
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
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 mechanism
Usable without knowing inside
implementation
• New inside implementation unknown to
users
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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
End
Chapter 1

Copyright ©2012 by Pearson Education, Inc. All rights reserved

More Related Content

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Chapter01 bags

  • 1. Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved
  • 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. 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. 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. 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. Figure 1-1 A CRC card for a class Bag Copyright ©2012 by Pearson Education, Inc. All rights reserved
  • 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. Figure 1-2 UML notation for the class Bag Copyright ©2012 by Pearson Education, Inc. All rights reserved
  • 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. 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. 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. 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. 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. Figure 1-3 A Vending Machine Copyright ©2012 by Pearson Education, Inc. All rights reserved
  • 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. 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. End Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved