Module 01 - Data Structure ADT(1)
Module 01 - Data Structure ADT(1)
5th Edition
Module 1
ADT Data
Structures
Using:
Chapter 1 –
ADT Bags
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Topics
– Data
Data Organization
– Abstraction
▪ Procedural abstraction
▪ Data Abstraction
ADT: BAG
Design – Unusual conditions
Interface for BAG ADT
Using the BAG ADT
Set ADT
Content can be found in Data Structures and Abstraction with Java by Carrano & Henry
Data
• Data is information
▪ Information input to, generated by, or output from
a program
▪ e.g., your name, or address, or telephone number
• Typical sources and destinations of data
▪ the user, disk files, other computers, and the web
• Programs process data
Data Organization in Life
• Standing in a line
• Stack of books
• To-Do list
• Dictionary
• Folders, directories
on your computer
• Road map
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Data Organization
Computers need a way to organize their data
• Represented by ADT – Abstract Data Type
– Data that is stored
– Operations on that data
• Data Structure – implementation of a ADT
• Collection – ADT that contains a group of objects
– Bag
– List
– Stack & Queue
– Dictionary
– Tree
– Graph
Abstraction
• The idea of knowing how to use something without
knowing how it is implemented
– We all use cars, but few of us could design and build
one
– Applies to methods and data structures
• Two types of abstractions in computer science
– Procedural abstraction
– Data abstraction (Abstract Data Types, ADTs)
Procedural Abstraction
• The idea that to use a method
– We need only know
▪ What is does (to decide when to invoke it)
▪ How to invoke it (its signature)
– We need not know how it works (its algorithm)
• This allows us to build upon other peoples
– Knowledge (algorithms) and
– Efforts (method coding and debugging)
Thus reducing the cost of software
Data Abstraction (Abstract Data Types)
• The idea that to use a data structure
– We only need to know
▪ What the operation methods do (to decide when to invoke
them)
▪ How to invoke the operation methods (their signature)
Example: Declare the standard method signature to add an element to a data structure as:
You could then change the data structure from an array based list to a linked list without
having to change the method signature to add an element to the data structure. You
wouldn’t have to worry that the method to add an element to your new data structure was
called “insert” or “addItem”, etc.
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
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
CRC Card
Bag
Responsibilities
Get the number of items currently in the bag
See whether the bag is empty
Add a given object to the bag
Remove an unspecified object from the bag
Remove a particular object from the bag, if possible
Remove all objects from the bag
Count the number of times a certain object occurs in the bag
Test whether the bag contains a particular object
Look at all objects that are in the bag
Collaborations
The class of objects that the bag can contain
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Using UML Notation to Specify a Class
Bag
+getCurrentSize(): integer
+isEmpty(): boolean
+add(newEntry: T): boolean
+remove(): T
+remove(anEntry: T): boolean
+clear(): void
+getFrequencyOf(anEntry: T): integer
+contains(anEntry: T): boolean
+toArray(): T[]
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
An Interface (Part 1)
/** An interface that describes the operations of a bag of objects. */
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();
/** 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);
// Simulate checkout
while (!shoppingCart.isEmpty())
System.out.println(shoppingCart.remove());
public PiggyBank()
{
coins = new ArrayBag<>();
} // end default constructor
while (!myBank.isEmpty())
{
Coin removedCoin = myBank.remove();
System.out.println("Removed a " + removedCoin.getCoinName() + ".");
amountRemoved = amountRemoved + removedCoin.getValue();
} // end while
Program Output
Added a PENNY.
Added a NICKEL.
Added a DIME.
Added a QUARTER.
Removing all the coins:
Removed a QUARTER.
Removed a DIME.
Removed a NICKEL.
Removed a PENNY.
All done. Removed 41 cents.
Copyright © 2019, 2015, 2012 Pearson Education, Inc. 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.
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
User defined: Interface Set
/** An interface that describes the operations of a set of objects. */
public interface SetInterface<T>
{
public int getCurrentSize();
public boolean isEmpty();
public T remove();
public void clear();
public boolean contains(T anEntry);
public T[] toArray();
} // end SetInterface
Listing 1-5 A Java interface for a class of sets
Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Java Class Library