Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adapted from Pearson Education, Inc.

Similar presentations


Presentation on theme: "Adapted from Pearson Education, Inc."— Presentation transcript:

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

2 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.

3 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.

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

5 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.

6 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.

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

8 Adapted from Pearson Education, Inc.
Interface /** Adds a new entry to this newEntry the object to be added as a new true if the addition is successful, or false if not */ public boolean add (T newEntry); /** Removes one unspecified entry from this bag, if 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 anEntry the entry to be 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 a newly allocated array of all the entries in the bag */ public T [] toArray (); } // end BagInterface Adapted from Pearson Education, Inc.

9 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.

10 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.

11 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 / "." + totalCost % 100); } // end main } // end OnlineShopper Adapted from Pearson Education, Inc.

12 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.

13 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.

14 End Chapter 1 Adapted from Pearson Education, Inc.


Download ppt "Adapted from Pearson Education, Inc."

Similar presentations


Ads by Google