Adapted from Pearson Education, Inc.

Slides:



Advertisements
Similar presentations
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Object Oriented Programming & Software Engineering.
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. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Abstraction: The Walls
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary A Java Interface Iterators Using the ADT Dictionary A Directory of Telephone.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets:
Data Structures and Abstractions with Java, 4e Frank Carrano
Abstract Data Types Chapter 1. Today n Data Types & Abstract Data Types n Designing Systems and ADTs use casesuse cases class-responsibility-collaboration.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
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 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Chapter 1 Data Abstraction: The Walls CS Data Structures Mehmet H Gunes Modified from authors’ slides.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
1 CS162: Introduction to Computer Science II Abstract Data Types.
DATA STRUCTURES 1st exam solution. Question 1(1/3) Suppose we have the ADT Bag which has the following operations: bool isEmpty(): tests whether Bag is.
Data Abstraction: The Walls
Linked Data Structures
Sections 3.4 Formal Specification
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Abstraction: The Walls
Efficiency of in Binary Trees
Chapter 1 Data Abstraction: The Walls
Lists Chapter 4.
Bag Implementations that Use Arrays
A Bag Implementation that Links Data
Abstract Data Types Chapter 1.
Queues, Deques and Priority Queues
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Queues, Deques and Priority Queues
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Adapted from Pearson Education, Inc.
TCSS 143, Autumn 2004 Lecture Notes
Priority Queues.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Dynamic Data Structures and Generics
A List Implementation That Links Data
ArrayLists 22-Feb-19.
Queues CSC212.
Stacks Chapter 5.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Bag Implementations that Use Arrays
Recitation 2 CS0445 Data Structures
Java Generics & Iterators
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A type is a collection of values
Abstract Data Types Chapter 1.
© 2016 Pearson Education, Ltd. All rights reserved.
Data Structures & Programming
CHAPTER 3: Collections—Stacks
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

Adapted from Pearson Education, Inc. Bags Chapter 1 Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. Contents & Objectives Contents Objectives The Bag A Bag’s Behaviors Specifying a Bag An Interface Using the ADT Bag Using an ADT Is Like Using a Vending Machine Describe the concept of abstract data type (ADT) Describe ADT bag Use ADT bag in a Java program Adapted from Pearson Education, Inc.

Bag: Definition & Behavior A finite collection of objects In no particular order May contain duplicate items Determine how many objects in bag Full? Empty? Add, remove objects Count duplicates Test for specific object View all objects Adapted from Pearson Education, Inc.

A CRC card for a class Bag Adapted from Pearson Education, Inc.

Specifying a Bag Describe data Specify methods for bag’s behaviors Name methods Choose parameters Decide return types Write comments UML notation for the class Bag Adapted from Pearson Education, Inc.

Design Decisions What should the method add do when it cannot add a new entry? Nothing? Leave bag unchanged, signal client of condition? 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? Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. Interface What does that mean? public interface BagInterface < T > { /** Gets the current number of entries in this bag. @return the integer number of entries currently in the bag */ public int getCurrentSize (); /** Sees whether this bag is full. @return true if full, or false if not */ public boolean isFull (); /** Sees whether this bag is empty. @return true if empty, or false if not */ public boolean isEmpty (); /** Counts the number of times a given entry appears in this bag. @param anEntry the entry to be counted. @return the number of times anEntry appears in the bag */ public int getFrequencyOf (T anEntry); /** Tests whether this bag contains a given entry. @param anEntry the entry to locate @return true if the bag contains anEntry, or false otherwise */ public boolean contains (T anEntry); Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. Interface /** Adds a new entry to this bag. @param newEntry the object to be added as a new entry @return true if the addition is successful, or false if not */ public boolean add (T newEntry); /** Removes one unspecified entry from this bag, if possible. @return either the removed entry, if the removal was successful, or null */ public T remove (); /** Removes one occurrence of a given entry from this bag, if possible. @param anEntry the entry to be removed @return true if the removal was successful, or false if not */ public boolean remove (T anEntry); /** Removes all entries from this bag. */ public void clear (); /** Creates an array of all entries that are in this bag. @return a newly allocated array of all the entries in the bag */ public T [] toArray (); } // end BagInterface Adapted from Pearson Education, Inc.

Using ADT Bag Implementation done from specifications User (client) needs know what ADT does, not how Type of object in bag specified by program (client) using the ADT Adapted from Pearson Education, Inc.

Using ADT Bag Implementation done from specifications User (client) needs know what ADT does, not how Type of object in bag specified by program (client) using the ADT An ADT is like a Vending Machine: 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 Adapted from Pearson Education, Inc.

Example of Bag for online shopping public class OnlineShopper { public static void main (String [] args) { Item [] items = { new Item ("Bird feeder", 2050), new Item ("Squirrel guard", 1547), new Item ("Bird bath", 4499), new Item ("Sunflower seeds", 1295) }; BagInterface < Item > shoppingCart = new Bag < Item > (); int totalCost = 0; // simulate getting item from shopper and add them to the shopping cart: for (int index = 0 ; index < items.length ; index++) { Item nextItem = items [index]; shoppingCart.add (nextItem); totalCost = totalCost + nextItem.getPrice (); } // end for // simulate checkout while (!shoppingCart.isEmpty ()) System.out.println (shoppingCart.remove ()); System.out.println ("Total cost: " + "\t$" + totalCost / 100 + "." + totalCost % 100); } // end main } // end OnlineShopper Adapted from Pearson Education, Inc.

Using ADT Bag In what ways is a piggybank similar to a bag? In what ways is it different? Example of Bag for class of piggy banks Listing 1-3 Demonstration of class PiggyBank Listing 1-4 Adapted from Pearson Education, Inc.

Class Library The interface Set public boolean add(T newEntry) public boolean remove(Object anEntry) public void clear() public boolean contains(Object anEntry) public boolean isEmpty() public int size() public Object[] toArray() Adapted from Pearson Education, Inc.

End Chapter 1 Adapted from Pearson Education, Inc.