Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Module 01 - Data Structure ADT(1)

The document discusses Abstract Data Types (ADTs) with a focus on the Bag ADT, which is a collection of objects that allows duplicates and provides various operations such as adding, removing, and checking the size. It emphasizes the importance of data and procedural abstraction in programming, allowing users to interact with data structures without needing to understand their internal implementations. Additionally, it includes examples of using the Bag ADT in practical applications like an online shopping cart and a piggy bank.

Uploaded by

ugonnaumunna1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Module 01 - Data Structure ADT(1)

The document discusses Abstract Data Types (ADTs) with a focus on the Bag ADT, which is a collection of objects that allows duplicates and provides various operations such as adding, removing, and checking the size. It emphasizes the importance of data and procedural abstraction in programming, allowing users to interact with data structures without needing to understand their internal implementations. Additionally, it includes examples of using the Bag ADT in practical applications like an online shopping cart and a piggy bank.

Uploaded by

ugonnaumunna1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures and Abstractions with Java ™

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)

– We need not know the operation algorithms


• This allows us to build upon other peoples
– Knowledge (algorithms) and
– Efforts (data structure implementation and debugging)

Thus reducing the cost of software


Standard Abstract Data Type
• An ADT whose method signatures conform to a standard
• Advantage is that an application’s data structure can be
changed by changing
– The declaration of the data structure object
– Not the operation method invocations
• Greatly reduces the cost of software maintenance

Example: Declare the standard method signature to add an element to a data structure as:

boolean add(T element)

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

FIGURE 1-1 A CRC card for a class Bag


Copyright © 2019, 2015, 2012 Pearson Education, Inc. 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

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[]

FIGURE 1-2 UML notation for the class Bag


Copyright © 2019, 2015, 2012 Pearson Education, Inc. 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

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();

/** Sees whether this bag is empty.


@return True if the bag is empty, or false if not. */
public boolean isEmpty();

/** 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();

LISTING 1-1 A Java interface for a class of bags


Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
An Interface (Part 2)
/** 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();

/** 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 find.
@return True if the bag contains anEntry, or false if not. */
public boolean contains(T anEntry);

/** Retrieves all entries that are in this bag.


@return A newly allocated array of all the entries in the bag.
Note: If the bag is empty, the returned array is empty. */
public T[] toArray();
} // end BagInterface

LISTING 1-1 A Java interface for a class of bags


Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Using the ADT Bag
/** A class that maintains a shopping cart for an online store. */
public class OnlineShopper
{
public static void main(String[] args) Program Output
{ Sunflower seeds $12.95
Item[] items = {new Item("Bird feeder", 2050),
Bird bath
new Item("Squirrel guard", 1547),
$44.99
new Item("Bird bath", 4499),
Squirrel guard $15.47
new Item("Sunflower seeds", 1295)};
Bird feeder
BagInterface<Item> shoppingCart = new Bag<>(); $20.50
int totalCost = 0; Total cost: $93.91
// Statements that add selected items to the shopping cart:
for (int index = 0; index < items.length; index++)
{
Item nextItem = items[index]; // Simulate getting item from shopper
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

LISTING 1-2 A program that maintains a bag for online shopping


Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Example: A Piggy Bank
/** A class that implements a piggy bank by using a bag. */
public class PiggyBank
{
private BagInterface<Coin> coins;

public PiggyBank()
{
coins = new ArrayBag<>();
} // end default constructor

public boolean add(Coin aCoin)


{
return coins.add(aCoin);
} // end add

public Coin remove()


{
return coins.remove();
} // end remove

public boolean isEmpty()


{
return coins.isEmpty();
} // end isEmpty
} // end PiggyBank

LISTING 1-3 A class of piggy banks


Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Example: Using A Piggy Bank (Part 1)
/** A class that demonstrates the class PiggyBank. */
public class PiggyBankExample
{
public static void main(String[] args)
{
PiggyBank myBank = new PiggyBank();

addCoin(new Coin(1, 2010), myBank);


addCoin(new Coin(5, 2011), myBank);
addCoin(new Coin(10, 2000), myBank);
addCoin(new Coin(25, 2012), myBank);

System.out.println("Removing all the coins:");


int amountRemoved = 0;

while (!myBank.isEmpty())
{
Coin removedCoin = myBank.remove();
System.out.println("Removed a " + removedCoin.getCoinName() + ".");
amountRemoved = amountRemoved + removedCoin.getValue();
} // end while

System.out.println("All done. Removed " + amountRemoved + " cents.");


} // end main

LISTING 1-4 A demonstration of the class PiggyBank


Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Example: Using A Piggy Bank (Part 2)
private static void addCoin(Coin aCoin, PiggyBank aBank)
{
if (aBank.add(aCoin))
System.out.println("Added a " + aCoin.getCoinName() + ".");
else
System.out.println("Tried to add a " + aCoin.getCoinName() +
", but couldn't");
} // end addCoin
} // end PiggyBankExample

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.

LISTING 1-4 A demonstration of the class PiggyBank


Copyright © 2019, 2015, 2012 Pearson Education, Inc. 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. FIGURE 1-3
A vending machine
• Usable even with new insides.

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();

/** Adds a new entry to this set, avoiding duplicates.


@param newEntry The object to be added as a new entry.
@return True if the addition is successful, or
false if the item already is in the set. */
public boolean add(T newEntry);

/** Removes a specific entry from this set, 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);

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

You might also like