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

data abstraction and abstract
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Data Abstraction and Abstract Data Types

1

Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131

slide-2
SLIDE 2
  • Outline

–Abstract Data types (ADT) –ADT Design Specification –ADT Java representation

2

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

  • n it or by it.

– Does not indicate any information about the internal representation of the data storage or implementation

  • f the operations.

– Independent of any programming language.

8

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 12

Definition: Bag

  • A finite collection of objects
  • In no particular order
  • May contain duplicate objects

12

slide-13
SLIDE 13

Behaviors

  • Determine how many objects in bag

– Full? – Empty?

  • Add, remove objects
  • Count duplicates
  • Look for specific object
  • View all objects

13

slide-14
SLIDE 14

Data

  • Collection of Objects
  • Number of objects in the bag

14

slide-15
SLIDE 15

CRC of Bag

15

slide-16
SLIDE 16

Design Specification of a Bag

  • Describe data
  • Specify methods for bag’s behaviors

– Name methods – Choose parameters – Decide return types – Write comments

16

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 19

UML

19

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 24
  • Output

24

slide-25
SLIDE 25

A Vending Machine

25

slide-26
SLIDE 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

slide-27
SLIDE 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

slide-28
SLIDE 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