data abstraction and abstract
play

Data Abstraction and Abstract Data Types Tessema M. Mengistu - PowerPoint PPT Presentation

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


  1. Data Abstraction and Abstract Data Types Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1

  2. • Outline – Abstract Data types (ADT) – ADT Design Specification – ADT Java representation 2

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Definition: Bag • A finite collection of objects • In no particular order • May contain duplicate objects 12

  13. Behaviors • Determine how many objects in bag – Full? – Empty? • Add, remove objects • Count duplicates • Look for specific object • View all objects 13

  14. Data • Collection of Objects • Number of objects in the bag 14

  15. CRC of Bag 15

  16. Design Specification of a Bag • Describe data • Specify methods for bag ’ s behaviors – Name methods – Choose parameters – Decide return types – Write comments 16

  17. 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

  18. 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

  19. UML 19

  20. 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

  21. 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

  22. 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

  23. 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

  24. • Output 24

  25. A Vending Machine 25

  26. 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

  27. 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

  28. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend