Download presentation
Presentation is loading. Please wait.
Published byАлександра Чичагова Modified over 5 years ago
1
© 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.
2
© 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.
3
© 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.
4
© 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.
5
© 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.
6
© 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.
7
© 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.
8
© 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.
9
© 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.
10
© 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.
11
© 2016 Pearson Education, Ltd. All rights reserved.
What is in that bag? © 2016 Pearson Education, Ltd. All rights reserved.
12
CRC Card FIGURE 1-1 A CRC card for a class Bag
© 2016 Pearson Education, Ltd. All rights reserved.
13
© 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.
14
UML Notation FIGURE 1-2 UML notation for the class Bag
© 2016 Pearson Education, Ltd. All rights reserved.
15
© 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.
16
An Interface LISTING 1-1 A Java interface for a class of bags
© 2016 Pearson Education, Ltd. All rights reserved.
17
An Interface LISTING 1-1 A Java interface for a class of bags
© 2016 Pearson Education, Ltd. All rights reserved.
18
An Interface LISTING 1-1 A Java interface for a class of bags
© 2016 Pearson Education, Ltd. All rights reserved.
19
Using the ADT Bag LISTING 1-2 A program that maintains a bag for online shopping © 2016 Pearson Education, Ltd. All rights reserved.
20
Using the ADT Bag LISTING 1-2 A program that maintains a bag for online shopping © 2016 Pearson Education, Ltd. All rights reserved.
21
Using the ADT Bag LISTING 1-2 A program that maintains a bag for online shopping © 2016 Pearson Education, Ltd. All rights reserved.
22
Example: A Piggy Bank LISTING 1-3 A class of piggy banks
© 2016 Pearson Education, Ltd. All rights reserved.
23
Example: A Piggy Bank LISTING 1-3 A class of piggy banks
© 2016 Pearson Education, Ltd. All rights reserved.
24
Example: A Piggy Bank LISTING 1-4 A demonstration of the class PiggyBank © 2016 Pearson Education, Ltd. All rights reserved.
25
Example: A Piggy Bank LISTING 1-4 A demonstration of the class PiggyBank © 2016 Pearson Education, Ltd. All rights reserved.
26
Example: A Piggy Bank LISTING 1-4 A demonstration of the class PiggyBank © 2016 Pearson Education, Ltd. All rights reserved.
27
Using ADT Like Using Vending Machine
FIGURE 1-3 A vending machine © 2016 Pearson Education, Ltd. All rights reserved.
28
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.
29
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.
30
Java Class Library: The Interface Set
Listing 1-5 A Java interface for a class of sets © 2016 Pearson Education, Ltd. All rights reserved.
31
Java Class Library: The Interface Set
Listing 1-5 A Java interface for a class of sets © 2016 Pearson Education, Ltd. All rights reserved.
32
© 2016 Pearson Education, Ltd. All rights reserved.
End Chapter 1 © 2016 Pearson Education, Ltd. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.