Java Software Structures - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Java Software Structures

Description:

Java Software Structures Lewis and Chase. 1. Chapter 2. Java Software Structures ... Java Software Structures Lewis and Chase. 4. Chapter 2. Linear and Non ... – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 39
Provided by: joec95
Category:

less

Transcript and Presenter's Notes

Title: Java Software Structures


1
Java Software Structures
  • Chapter 2 Collections

2
Chapter Objectives
  • ? Define the concepts and terminology related to
    collections
  • ? Explore the basic structure of the Java
    Collections API
  • ? Discuss the abstract design of collections
  • ? Define a bag collection
  • ? Use a bag collection to solve a problem
  • ? Examine an array implementation of a bag

3
Collections
  • A collection is an object that gathers and
    organizes other objects
  • The elements of a collection may only be accessed
    in prescribed ways
  • The user or client of a collection is usually
    another class or object

4
Linear and Non-Linear Collections
  • Collections are either linear or non-linear

5
Organization of Collections
  • The organization of the elements in a collection,
    relative to each other, is usually determined by
    one of two things
  • ? by the order in which they were added to the
    collection, or
  • ? by some inherent relationship among the
    elements themselves

6
Abstraction
  • An abstraction hides or ignores certain details
  • Examples
  • Driving a car
  • Using a word processor
  • Cooking

7
Abstraction
8
Abstraction in Collections
  • ? How does the collection operate conceptually?
  • ? How do we formally define the interface to the
    collection?
  • ? What kinds of problems does the collection help
    us solve?
  • ? What various ways might we implement the
    collection?
  • ? What are the benefits and costs of each
    implementation?

9
Terminology
  • A data type is a group of values and the
    operations defined on those values
  • An abstract data type (ADT) is a data type whose
    values and operations are not inherently defined
    within a programming language
  • A data structure is the collection of programming
    constructs used to implement a collection

10
The Java Collections API
  • The Java Collections API is a set of classes that
    represent a few specific types of collections
  • Why create our own?

11
Example A Bag
  • A bag is a collection that groups elements with
    no particular positional relationship.
  • Conceptually it is similar to a physical bag into
    which elements are placed

12
Conceptual View of a Bag
13
Operations on a Bag
14
ADTs and Java Interfaces
  • A Java interface provides a formal mechanism for
    defining the set of operations for any
    collection
  • Java interfaces facilitate the separation of the
    interface operations from the methods that
    implement them
  • The interface name can be used as the type of a
    reference, which can be assigned any object of
    any class that implements the interface

15
A Bag ADT
  • BagADT.java defines a Java interface for a bag
    collection
  • We name a collection interface using the
    collection name followed by the abbreviation ADT

16
Iterators
  • An iterator is an object that provides the means
    to iterate over a collection
  • Most collections provide one or more ways to
    iterate over their elements
  • In the case of the BagADT.java interface, we
    define a method called iterator that returns an
    Iterator object

17
Iterators
  • The Iterator interface is defined in the Java
    standard class library. The two primary abstract
    methods defined in the Iterator interface are
  • ? hasNext, which returns true if there are more
    elements in the iteration
  • ? next, which returns the next element in the
    iteration

18
Exceptions
  • The manner in which exceptions are used is
    important to the definition of a software system
  • Usually its best to throw exceptions whenever an
    invalid operation is attempted.
  • In the case of a bag, we will throw an exception
    whenever the user attempts to remove an element
    from an empty bag

19
Exceptions
  • User then has choice to prevent or handle the
    exception
  • if (! theBag.isEmpty())
  • element theBag.removeRandom()
  • OR
  • try
  • element theBag.removeRandom()
  • catch (EmptyBagException exception)
  • System.out.println (No elements
    available.)

20
Bingo
21
Bingo
  • Class  BingoBall.javamodels each possible
    selection
  • Class  Bingo.javamodels the game

22
Bingo UML Description
23
Managing Capacity
  • throw an exception if the data structure is
    full?
  • return a status indicator that can be checked by
    the user to see if the add operation was
    successful?
  • automatically expand the capacity of the
    underlying data structure whenever necessary?

24
The ArrayBag Class
  • class names indicate both the underlying data
    structure and the collection
  • we define a class called ArrayBag that represents
    an array-based implementation of a bag
    collection
  • ArrayBag implements BagADT.java
  • ArrayIterator.java

25
The size and isEmpty Methods
  • The operations size and isEmpty are found in
    almost all collections
  • The count instance variable represents the number
    of elements in the bag, and can therefore be used
    to provide efficient solutions to these
    operations
  • All operations that change the state of the bag
    collection must carefully maintain the integrity
    of the count value.

26
The Add Method
  • The purpose of the add method is take the Object
    passed in as a parameter and incorporate that
    Object into the bag collection
  • The instance variable count represents the next
    empty space in the array, so we can simply store
    the new element at that location
  • The add operation for a fixed capacity structure
    must take into account the situation in which the
    array is already full

27
The addAll Method
  • The purpose of the addAll method is to
    incorporate all of the objects from the bag
    accepted as a parameter into this bag collection
  • We can use our iterator method to step through
    the contents of one bag and our add method to add
    those elements to the current bag

28
The removeRandom Method
  • The removeRandom operation chooses an element
    from the collection at random
  • Removes that element from the collection
  • Returns that element to the calling method
  • If the bag collection is empty, this method
    throws an EmptyBagException

29
The remove Method
  • The remove operation removes one occurrence of
    the specified element from the bag and returns
    it
  • The remove method will throw an EmptyBagException
    if the bag is empty and a NoSuchElementException
    if the target element is not in the bag

30
The union Method
  • The union operation returns a new bag that is the
    union of this bag and the bag passed as a
    parameter
  • We use our constructor to create a new bag
  • We then step through our array and use the add
    method to add each element of our current bag to
    the new bag
  • We then use an iterator for the bag passed as a
    parameter, step through each element of that bag,
    and add each of them to the new bag

31
The contains Method
  • The contains operation returns true if this bag
    contains the specified target element
  • This operation uses a linear search of the
    contents of the array to locate a particular
    element

32
The equals Method
  • The equals operation will return true if the
    current bag contains exactly the same elements as
    the bag passed as a parameter
  • If the two bags are of different sizes then there
    is no reason to continue the comparison
  • If they are the same size, we use an iterator to
    step through the elements of one bag and use the
    contains method to confirm that each of those
    elements is also in the current bag

33
The iterator Method
  • The implementation of the iterator operation
    simply returns a new object instantiated from the
    ArrayIterator class
  • ArrayIterator.java implements the Iterator
    interface defined in the Java class library, and
    therefore provides definitions for the hasNext
    and next methods

34
The toString Method
  • The toString operation returns one big string
    that represents the state of the collection
  • It does so by concatenating the string
    representations of all elements contained in the
    bag
  • The particular results of this operation depend
    on how the toString operation for the element
    class is defined

35
UML Description of the Bingo Example
36
Summary of Key Concepts
  • A collection is an object that gathers and
    organizes other objects and may be either linear
    or nonlinear.
  • Elements in a collection are typically organized
    by the order of their addition to the collection
    or by some inherent relationship among the
    elements.
  • A collection is an abstraction where the details
    of the implementation are hidden.
  • A data type is a group of values operations
    defined on those value. An abstract data type is
    a data type whose values and operations are not
    inherently defined within a programming
    language.
  • A bag is a nonlinear collection in which there is
    essentially no organization to the elements in
    the colletion.

37
Summary of Key Concepts
  • A Java interface defines a set of abstract
    methods which is useful in separating the concept
    of an abstract data type from its
    implementation.
  • By using the interface name as the return type,
    it doesnt commit the method to the use of any
    particular class that implements a bag.
  • An iterator is an object that provides a means to
    iterate over a collection.

38
Summary of Key Concepts
  • Our goal is to design an efficient implementation
    of the operations for an abstract data type while
    maintaining the seperation between the collection
    and the underlying data structure used to
    implement it.
  • Each decision on whether to handle exceptional
    conditions or throw an exception should be made
    considering whether the ADT or the user of the
    ADT should control the particular behavior.
  • In the Java Collections API and throughout this
    text, class names indicate both the underlying
    data structure and the collection.
Write a Comment
User Comments (0)
About PowerShow.com