Data Abstraction and Abstract Data Types Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1
• Outline – Abstract Data types (ADT) – ADT Design Specification – ADT Java representation 2
Data Type • A data type is characterized by: – a set of values – a set of operations, which can be applied uniformly to all these values – a data representation, which is common to all these values, and • Java has two data types: – Primitive data types – Reference data types 3
Data Type • Java provides eight primitive types: – boolean – char , byte , short , int , long – float , double • Each primitive type has – a set of values – a set of operations – a data representation • These are “set in stone”— there is nothing the programmer can do to change anything about them 4
Data Types • Reference data type – Array – All classes – built in and user defined – Store address of the object that it refers to – Has set of values – Has set of operations that can be applied – Has its own representation 5
Data Abstraction • The process of hiding the inside detail of a data and providing the essential information • It is a programming design technique that relies on the separation of interface and implementation 6
Data Abstraction • Why abstraction? – Easier to design – hide what doesn’t matter – Security – prevent access to things that shouldn’t be accessed – Modifiability – can be modified without affecting users 7
Abstract Data Type • An Abstract Data Type (ADT) can be defined as: – Is a specification of a data set and the operations on that data. – An ADT is an externally defined data type that holds some kind of data. – An ADT has built-in operations that can be performed on it or by it. – Does not indicate any information about the internal representation of the data storage or implementation of the operations. – Independent of any programming language. 8
Abstract Data Type • A class can be a data type – The possible values of a class are called objects – The data representation is a reference (pointer) to a block of storage • The structure of this block is defined by the fields (both inherited and immediate) of the class – The operations on the objects are called methods • Many classes are defined in Java’s packages • Classes can be defined as needed 9
Abstract Data Type • ADT doesn’t specify how to store the data or how to implement the operations • ADTs are independently of any programming language. • Data structure – An implementation of an ADT within a programming language. 10
Abstract Data Type • A collection is a general term for an ADT that contains a group of objects. – Duplicate Vs non duplicate – Ordered Vs non ordered • A container is a class that implements a collection. 11
Definition: Bag • A finite collection of objects • In no particular order • May contain duplicate objects 12
Behaviors • Determine how many objects in bag – Full? – Empty? • Add, remove objects • Count duplicates • Look for specific object • View all objects 13
Data • Collection of Objects • Number of objects in the bag 14
CRC of Bag 15
Design Specification of a Bag • Describe data • Specify methods for bag ’ s behaviors – Name methods – Choose parameters – Decide return types – Write comments 16
Design Decisions • What should the method add do when it cannot add a new entry? – Nothing? – Leave bag unchanged, signal client of condition? – Throw an Exception 17
Design Decisions • What should happen when an unusual condition occurs? – Assume invalid never happens? – Ignore invalid event? – Guess at client ’ s intention? – Return flag value? – Return boolean value – success/failure? – Throw exception? 18
UML 19
Interface Bag • Can contain only constants (final variables) and abstract method (no implementation) • Bag can organize into interface • Note items in bag are of same type – Generic type <T> 20
public interface BagInterface<T> { public int getCurrentSize(); public boolean isFull(); public boolean isEmpty(); public boolean add(T newEntry); public T remove(); public boolean remove(T anEntry); public void clear(); public int getFrequencyOf(T anEntry); public boolean contains(T anEntry); public T[] toArray(); } // end BagInterface 21
Using ADT Bag • Implementation done from specifications – User needs know what ADT does, not how • Type of object in bag specified by program using the ADT 22
public class OnlineShopper { public static void main(String[] args) { Item[] items = { new Item("Bird feeder", 2050), new Item("Squirrel guard", 1547), new Item("Bird bath", 4499), new Item("Sunflower seeds", 1295)}; BagInterface<Item> shoppingCart = new Bag<Item>(); int totalCost = 0; for (int index = 0; index < items.length; index++) { Item nextItem = items[index]; // simulate getting item from shoppingCart.add(nextItem); totalCost = totalCost + nextItem.getPrice(); } // end for while (!shoppingCart.isEmpty()) { System.out.println(shoppingCart.remove()); System.out.println("Total cost: " + "\t$" + totalCost / 100 + "." + totalCost % 100); } } 23
• Output 24
A Vending Machine 25
Vending Machine Like An ADT • Perform only available tasks • User must understand the tasks • Cannot access inside of mechanism • Usable without knowing inside implementation • New inside implementation unknown to users 26
Class Library • The interface Set public boolean add(T newEntry) public boolean remove(Object anEntry) public void clear() public boolean contains(Object anEntry) public boolean isEmpty() public int size() public Object[] toArray() 27
Summary • An abstract data type, or ADT, is a specification of a data set and the operations on that data. • This specification does not indicate how to store the data or how to implement the operations, and it is independent of any programming language. • A collection is an object that holds a group of other objects. • Writing a Java interface is a way to organize a specification for an ADT. 28
Recommend
More recommend