Data Structures in Java
Mahender K
An abstract data type (ADT) is a type name and a set of operations on that type where:
Users of the ADT are expected to alter/examine values
of this type only via the operations provided. The creator of the ADT promises to leave the operation specifications unchanged. The creator of the ADT is allowed to change the code of the operations at any time, as long as it continues to satisfy the specifications. The creator of the ADT is also allowed to change the data structure actually used to implement the type.
10/2/2009 Data Structures in Java 2
An ADT is not a data type(DT), because it has no representation. We implement an ADT by supplying a representation for the data and algorithms for the operations. So an ADT implementation is a data type. Another way of looking at it:
An ADT is a design concept. A DT is a programming language concept. An ADT implementation is the combination of the
two.
10/2/2009
Data Structures in Java
Lets try an interesting example: Counters.
A counter is simply an object that maintains a
count that can be initialized and incremented.
The first question to ask:
What, precisely, is a counter?
Or, in object-oriented terms:
What is it that makes an object a counter?
10/2/2009
Data Structures in Java
Informally, a counting object
can be initialized, can be incremented, and
can be read.
We can say these things precisely in Java by writing an interface.
10/2/2009
Data Structures in Java
/** Interface for counting objects. */ public interface Countable { /** Increment the internal counter. */ public void inc(); /** Reset the internal counter to 0. */ public void resetCount(); /** Return the internal count. */ public int readCount(); }
10/2/2009
Data Structures in Java
A Java interface is a kind of specification for a class.
It specifies a list of methods that any
implementation must provide.
10/2/2009
Data Structures in Java
/** Simple counting objects. */ public class SimpleCounter implements Countable { /** Initialization for new counters. */ public SimpleCounter () { resetCount(); } /** Reset the counter to 0. */ public void resetCount () { count = 0; } Constructor /** Increment the counter. */ Method public void inc () { count++; } /** Return the current count. */ public int readCount () { return count; } private int count; }
Private instance variable
10/2/2009
Data Structures in Java
/** A program that uses a SimpleCounters.*/ public class Main { public static void main (String args[]) { int n = 99; SimpleCounter c = new SimpleCounter(); SimpleCounter d = new SimpleCounter(); c.inc(); d.inc(); d.inc(); System.out.println (n + c.readCount() + d.readCount()); } Creation of new }
SimpleCounter objects.
10/2/2009
Data Structures in Java
SimpleCounter c = new SimpleCounter();
SimpleCounter d = new SimpleCounter();
10/2/2009
Data Structures in Java
10
Files we have created:
Countable.java The interface specification.
SimpleCounter.java An implementation of the interface.
Main.java A client program.
10/2/2009
Data Structures in Java
11
Strictly speaking, Java does not require us to make use of an interface for this program.
public class SimpleCounter implements Countable { }
10/2/2009
Data Structures in Java
12
Strictly speaking, Java does not require us to make use of an interface for this program.
public class SimpleCounter { } Leaving out the implements declaration is OK with Java.
10/2/2009
Data Structures in Java
13
Interfaces allow us to separate interface from implementation. Clients assume only the interface. Clients thereby protect themselves from many kinds of changes in the underlying implementations.
10/2/2009
Data Structures in Java
14
Separate interface from implementation.
10/2/2009
Data Structures in Java
15
Separate interface from implementation.
10/2/2009
Data Structures in Java
16
Separate interface from implementation. Clients do not need to know most implementation details.
10/2/2009
Data Structures in Java
17
Abstract data types make it much easier to make use of pre-existing code.
We shall see this in later assignments in the
project
work in a team. maintain code in the long run. write down the results of your careful thinking about the problem.
Data Structures in Java 18
10/2/2009
/** Counting objects that double every time. */ public class DoublingCounter implements Countable { /** Initialization for new counters. */ public DoublingCounter () { resetCount(); } /** Reset the counter to 1. */ public void resetCount () { count = 1; }
/** Increment the counter. */ public void inc () { count *= 2; } /** Return the current count. */ public int readCount () { return count; } private int count; }
10/2/2009 Data Structures in Java 19
How to Think Like a Computer Scientist, Downey, Allen B., URL:
Java Structures : Data Structures in Java for the Principled Programmer, Bailey, Duane Structured Computer Organization, Tanenbaum, A. S.,
http://www.cs.williams.edu/JavaStructures/Welcome.html http://www.cs.williams.edu/JavaStructures/Welcome.html ftp://ftp.cs.odu.edu/pub/zeil/cs412/Lectures/03adts/adts_s http://www.greenteapress.com/thinkapjava/
ADTs & Classes, Zeil, Steven J.,
ummary.pdf
Data Structures and Algorithms in Java by Robert Sedge wick
Data Structures in Java 20
10/2/2009