© 2016 Pearson Education, Ltd. All rights reserved.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved 4-1 Chapter 4 Data Abstraction: The Walls.
Data Abstraction: The Walls
Dictionaries Chapter 17 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures and Abstractions with Java, 4e Frank Carrano
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 1 Data Abstraction: The Walls CS Data Structures Mehmet H Gunes Modified from authors’ slides.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Data Abstraction: The Walls
Queues Chapter 8 (continued)
Data Abstraction & Problem Solving with C++
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Data Abstraction: The Walls
Chapter 1 Data Abstraction: The Walls
Lists Chapter 4.
Lecture 2 of Computer Science II
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Exceptions C++ Interlude 3
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Iterators (short version)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Slides by Steve Armstrong LeTourneau University Longview, TX
Adapted from Pearson Education, Inc.
Dynamic Data Structures and Generics
Lists Chapter 8.
Introduction to Data Structure
Lecture 11 Abstract Data Type
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Bag Implementations that Use Arrays
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Lecture 3 – Data collection List ADT
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Uses An Array
A List Implementation that Links Data
© 2016 Pearson Education, Ltd. All rights reserved.
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

© 2016 Pearson Education, Ltd. All rights reserved. Bags Chapter 1 Data Structures and Abstractions with Java, 4e, Global Edition Frank Carrano © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Bags This chapter builds on the concepts of encapsulation and data abstraction, and it develops the notion of an abstract data type. This chapter also begins to generalize the idea of grouping objects. A collection is an object that groups other objects and provides various services to its client. In particular, a typical collection enables a client to add, remove, retrieve, and query the objects it represents © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Bags Various collections exist for different purposes. Their behaviors are specified abstractly and can differ in purpose according to the collection. To provide an example of a collection and of an abstract data type, we will specify and use the ADT bag. In doing so we will provide a Java interface for our bag. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Bags Knowing just this interface, you will be able to use a bag in a Java program. You do not need to know how the entries in the bag are stored or how the bag operations are implemented. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Bags A bag is a finite collection of objects in no particular order. These objects have the same or related data types. A bag can contain duplicate items. What distinguishes a bag from other collections? A bag doesn’t do much more than contain its items. It doesn’t order them in a particular way, nor does it prevent duplicate items. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. While describing the behaviors for the collection that we’ll design in this chapter, let’s keep in mind that we are specifying an abstraction inspired by an actual physical bag. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Since a bag contains a finite number of objects, reporting how many objects it contains could be one of a bag’s behaviors: Get the number of items currently in the bag A related behavior detects whether a bag is empty: See whether the bag is empty © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 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 © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. We should be able to add and remove objects: Add a given object to the bag Remove an unspecified object from the bag Remove an occurrence of a particular object from the bag, if possible Remove all objects from the bag © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. The first remove operation just removes any object it can. This operation is like reaching into a grab bag and pulling something out. On the other hand, the second remove operation looks for a particular item in the bag. If you find it, you take it out. If the bag contains several equal objects that satisfy your search, you remove any one © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. What is in that bag? © 2016 Pearson Education, Ltd.  All rights reserved.

CRC Card FIGURE 1-1 A CRC card for a class Bag © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 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 or do not © 2016 Pearson Education, Ltd.  All rights reserved.

UML Notation FIGURE 1-2 UML notation for the class Bag © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. 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 © 2016 Pearson Education, Ltd.  All rights reserved.

An Interface LISTING 1-1 A Java interface for a class of bags © 2016 Pearson Education, Ltd.  All rights reserved.

An Interface LISTING 1-1 A Java interface for a class of bags © 2016 Pearson Education, Ltd.  All rights reserved.

An Interface LISTING 1-1 A Java interface for a class of bags © 2016 Pearson Education, Ltd.  All rights reserved.

Using the ADT Bag LISTING 1-2 A program that maintains a bag for online shopping © 2016 Pearson Education, Ltd.  All rights reserved.

Using the ADT Bag LISTING 1-2 A program that maintains a bag for online shopping © 2016 Pearson Education, Ltd.  All rights reserved.

Using the ADT Bag LISTING 1-2 A program that maintains a bag for online shopping © 2016 Pearson Education, Ltd.  All rights reserved.

Example: A Piggy Bank LISTING 1-3 A class of piggy banks © 2016 Pearson Education, Ltd.  All rights reserved.

Example: A Piggy Bank LISTING 1-3 A class of piggy banks © 2016 Pearson Education, Ltd.  All rights reserved.

Example: A Piggy Bank LISTING 1-4 A demonstration of the class PiggyBank © 2016 Pearson Education, Ltd.  All rights reserved.

Example: A Piggy Bank LISTING 1-4 A demonstration of the class PiggyBank © 2016 Pearson Education, Ltd.  All rights reserved.

Example: A Piggy Bank LISTING 1-4 A demonstration of the class PiggyBank © 2016 Pearson Education, Ltd.  All rights reserved.

Using ADT Like Using Vending Machine FIGURE 1-3 A vending machine © 2016 Pearson Education, Ltd.  All rights reserved.

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. © 2016 Pearson Education, Ltd.  All rights reserved.

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. © 2016 Pearson Education, Ltd.  All rights reserved.

Java Class Library: The Interface Set Listing 1-5 A Java interface for a class of sets © 2016 Pearson Education, Ltd.  All rights reserved.

Java Class Library: The Interface Set Listing 1-5 A Java interface for a class of sets © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. End Chapter 1 © 2016 Pearson Education, Ltd.  All rights reserved.