<<

ABSTRACT DATA TYPES

An abstract is a of data values and associated operations that are precisely specified independent of any particular .1 The term is often abbreviated ADT.

A data abstraction separates the essential characteristics of data from the physical details of its implementation. ADT is the same concept but more precisely defined and specifically applied to data types. The main characteristic is the ability to describe the data and operations without resorting to computer terminology.

Example In mathematics, a rational number is the quotient of two where the divisor is not zero. a In other words, a fraction b where a and b are both integers and b≠0.

a ac The product of two rational numbers,   . b d b d

a c ad  cb The sum of two rational numbers,   . b d bd

Since the values and operations of a rational number can be defined independently of computer terminology, it is an ADT.

Example Most word processors have a spell checker that can detect misspelled words. It works by taking each word of the word processing document and comparing it to correctly spelled words in the spell checker’s dictionary.

The dictionary is a list of correctly spelled words ( a, aardvark, an, anorexic, etc.) Operations on the dictionary include finding a word in the dictionary and adding a new word to it.

The dictionary is an ADT because it can be described independently of computer terminology.

1 Paul Black, ed. U.S. National Institute of Standards and Technology, 2/10/2005 (Accessed 9/1/2013), Available WWW: http://xlinux.nist.gov/dads/HTML/abstractDataType.html.

Abstract Data Types Page 1 An ADT is created in Java using the class construct. You create variables of the ADT by building objects of the class.

Seasoned programmers implement ADTs following the principle of data encapsulation – protecting the object’s internal state variables and providing public methods through which outsiders interact with the object.

Example Here’s a simple Java implementation of the rational number ADT. The numerator and denominator are tagged private to encapsulate them from direct manipulation by other objects. Other objects can use the public constructor (lines 6‒11) to initialize a new RationalNumber. Other object multiply two rational numbers by calling the public method times.

1 public class RationalNumber 2 { 3 private int numerator; 4 private int denominator; 5 6 public RationalNumber( int n, int d ) 7 // Construct n/d. 8 { 9 numerator = n; 10 denominator = d; 11 } 12 13 public RationalNumber times( RationalNumber r ) 14 // Return this * r 15 { 16 int n = numerator * r.numerator; 17 int d = denominator * r.denominator; 18 return new RationalNumber( n, d ); 19 } 20 }

Abstract Data Types Page 2 Example Below is the outline of a Java class that implements the Dictionary ADT. 1 public class Dictionary 2 { 3 . . . // private implementation data here 4 5 public Dictionary( ) 6 // Construct a new dictionary. 7 { 8 . . . 9 } 10 11 public boolean find( String word ) 12 // Return true if 'word' is found within the dictionary. 13 { 14 . . . 15 } 16 17 public void insert( String word ) 18 // Insert 'word' in the dictionary. 19 { 20 . . . 21 } 22 }

Abstract Data Types Page 3 Exercises

Enter class RationalNumber into jGRASP and save it to its own Java file. Compile it. Open jGRASP’s Interactions window and complete the exercises below within it. Be sure to observe the run-time objects in jGRASP’s Workbench window. 1. Call the RationalNumber constructor to create a new object containing the fraction ½.

2. Call the RationalNumber constructor to create a new object containing the fraction ¾.

3. Call the times method to create a new object containing the product of the other two fractions.

Abstract Data Types Page 4