MSc IT - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

MSc IT

Description:

In programming it is necessary to store and process collections of data ... e.g String, Integer. the classes that we will examine later work in this way ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 29
Provided by: ingr96
Category:
Tags: gstring | msc

less

Transcript and Presenter's Notes

Title: MSc IT


1
MSc IT Computing Object Oriented Programming
  • Java
  • Lecture 5

2
Topics Covered
  • Data Structures
  • Stack
  • Queue
  • Vector
  • Enumeration
  • Hashtable

3
Data Structures
4
Data Storage
  • In programming it is necessary to store and
    process collections of data
  • If only a small amount of data is involved then
    unique variable names maybe used to reference
    each individual item
  • rarely the case
  • Usually it is necessary to create a data store to
    hold the data
  • The subject of data structures studies different
    ways that data may be stored and how this
    influences the algorithms that are required to
    process it

5
Requisite Operations
  • The methods or operations of a data structure
    include some or all of the following
  • adding an element to the store
  • removing an element
  • retrieving an element
  • searching for an element
  • processing each element in turn
  • These methods define the behaviour of the data
    structure
  • There are many different implementations to
    achieve same behaviour
  • The next sections identify a few common data
    structure
  • and study the use of some general purpose data
    structures (container classes) provided with JDK

6
Review of Arrays
7
Review of Arrays
  • An array eightnums of eight elements

first numberhas offset 0eightnums0
eighth numberhas offset 7 eightnums 7
  • To declare an array specify type, name and size
  • Appendix A

8
Common Data Structures
  • Each of these common data structures organises
    stored data in a specific way and has a unique
    behaviour
  • Stack - Last-In, First-Out behaviour (LIFO)
  • Queue - First-In, First-Out behaviour (FIFO)
  • Set - An unordered collection without duplicates
  • Bag - A collection with duplicates
  • List - Data is stored and processed in a linear
    or sequential manner
  • Ordered List - Data is in a sorted order
  • Tree - Hierarchical structure
  • Graph - A network of objects
  • Mapping - An set of associated pairs

9
OO Classes Data structures
  • An object oriented class may also be considered a
    data structure
  • it stores data (its attributes)
  • has a behaviour (defined by its methods)
  • To create the data structures above we define a
    class which specifies the required behaviour
  • Then the challenge is to implement it efficiently

10
Stack
  • The stack is a data structure with Last-In,
    First-Out behaviour (LIFO)
  • The methods of a stack restrict access and
    processing to the top element
  • place an element on the top of the stack (push)
  • remove the top element (pop)
  • There are no methods to process or access other
    elements
  • Exercise
  • think of some physical examples of stacks

11
Stack Class Specification
  • Below we define the behaviour of a stack by
    specifying the methods of a Stack object
  • Appendix B
  • Note that the parameters and function results are
    Objects
  • as every Java class inherits from Object a class
    that deals with Objects can store any class
  • e.g String, Integer
  • the classes that we will examine later work in
    this way
  • The Stack class definition includes some extra
    methods to make it useful to a client object
  • The isEmpty()method may be used to avoid
    accessing the top of an empty stack

12
Queue
  • A queue is a list which restricts access to the
    ends
  • The queue is a data structure with First-In,
    First-Out behaviour (FIFO)
  • The methods are
  • Place an element on the end of the queue (join)
  • Remove the top element (leave)
  • Return the length of the queue (length)
  • A queue is used in many applications, especially
    where jobs must be scheduled for a particular
    process
  • Variations of simple queues are priority queues
    and buffers

13
Queue Class Specification
  • The class definition includes some extra methods
    to make it useful to a client object
  • The isEmpty() method is particularly useful to
    prevent a run time error generated by an attempt
    to access the first element of an empty queue
  • Appendix C

14
Vector
  • The Vector class from java.util provides a
    dynamic array-like structure
  • grows (and shrinks) as required
  • If we then add the Enumeration interface (next
    section) our program can iterate through the
    elements in the container
  • At any time a Vector contains a number of
    elements less than or equal to its capacity
  • capacity is the space currently available, which
    will be increased as required
  • Vector default capacity is 10 and it will double
    in size each time it runs out of space
  • an adequate behavior in most cases

15
More Vectors
  • A Vector stores references to Objects
  • it is a general purpose container
  • When retrieving elements they are cast to the
    type required
  • no way of storing the type of object stored
  • The following program demonstrates some of the
    methods of the Vector class
  • Appendix D
  • What method would be used to find the position of
    the String "second" ?
  • Appendix G for documentation

16
Enumeration
17
Enumeration
  • An Enumeration is a class that may be used to
    step through the elements of a data structure
  • Two methods provided are
  • hasMoreElements()
  • returns true as long as there are elements left
  • nextElement()
  • which returns the next one as an Object
  • The program demonstrates use of an Enumeration
  • The Vector method elements returns an Enumeration
    of all the elements as its result
  • Appendix E
  • Appendix H for documentation

18
Hashtable
19
Hashtable
  • A hash table is a data structure that stores
    pairs of objects
  • one object is considered the key
  • other object is the data associated with that key

20
Mapping
  • A hash function is a mapping from keys to index
  • The properties of this function are that
  • for the a random set of keys, the function should
    produce a random set of keys
  • the result of the function is an index within the
    range 0.. table size
  • the hash function computes the same value for the
    same key each time it is applied

21
Storing Values
  • The process to store an associated pair of
    objects in a hashtable is as follows
  • Use the hash function to compute the index from
    the key
  • Check that the table entry is free for that index
  • Store the key and data in the table
  • note the table is like an array

22
Collision Handling
  • If at step 2 the cell is not free, a second hash
    function may be applied
  • This process is repeated until an empty cell is
    found
  • If no empty cell is found after several
    applications, the table is considered to be full
  • Usually, we design a hashtable to be only half
    occupied at most to minimise rehashing
  • This means that the chance of hitting an empty
    cell at random is high

23
Retrieving Data
  • To retrieve an associated pair of objects from a
    hashtable
  • use the hash function to compute the index from
    the target key
  • if the cell is occupied, check that the key
    matches the target
  • otherwise rehash and repeat the check
  • if the key in the cell matches the target then
    the pair is found and maybe retrieved.
  • if the result of hashing or rehashing finds an
    empty cell then the key is not in the table

24
Advantage
  • The advantage of a hashtable over other data
    structures is that insertion and retrieval of
    data has complexity 0(1)
  • That is, no matter how large the hash table,
    storing and searching for data takes
    (approximately) the same number of operations
  • it is very efficient

25
Hashtable Constraints
  • The constraints are
  • keys must be unique, duplicate entries are not
    allowed
  • the hash function must be designed to generate
    'random indices
  • the table loading must not exceed 0.7 full
  • otherwise efficiency drops...
  • the order of the data is not important
  • Hashtables are often used for look up
    applications
  • e.g. Symbol table in a compiler

26
Using Hashtable
  • The HashDem program demonstrates use of
    java.util.Hashtable
  • Appendix F
  • Appendix I for documentation
  • How would you override the default size and
    loading in a Hashtable?

27
Laboratory Work
  • Look at the Vector demonstration files on my
    web-site
  • Make sure that you understand how they work
  • Read Java Gently, p 244 254
  • Attempt problem 6.6

28
Summary
  • We have examined the data structures stack and
    queue
  • The JDK classes Vector, Hashtable and Enumeration
    have been introduced
Write a Comment
User Comments (0)
About PowerShow.com