Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type...

28
Data Abstraction and Abstract Data Types 1 Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale [email protected] Room - 3131

Transcript of Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type...

Page 1: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

Data Abstraction and Abstract Data Types

1

Tessema M. MengistuDepartment of Computer Science

Southern Illinois University [email protected]

Room - 3131

Page 2: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

• Outline

–Abstract Data types (ADT)

–ADT Design Specification

–ADT Java representation

2

Page 3: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 4: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 5: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 6: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 7: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 8: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

Abstract Data Type

• An Abstract Data Type (ADT) can be defined as:

– Is a specification of a data set and the operations onthat data.

– An ADT is an externally defined data type that holdssome kind of data.

– An ADT has built-in operations that can be performedon it or by it.

– Does not indicate any information about the internalrepresentation of the data storage or implementationof the operations.

– Independent of any programming language.

8

Page 9: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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) toa block of storage

• The structure of this block is defined by the fields (bothinherited 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

Page 10: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 11: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

Abstract Data Type

• A collection is a general term for an ADT thatcontains a group of objects.

– Duplicate Vs non duplicate

– Ordered Vs non ordered

• A container is a class that implements acollection.

11

Page 12: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

Definition: Bag• A finite collection of objects

• In no particular order

• May contain duplicate objects

12

Page 13: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

Behaviors

• Determine how many objects in bag

– Full?

– Empty?

• Add, remove objects

• Count duplicates

• Look for specific object

• View all objects

13

Page 14: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

Data

• Collection of Objects

• Number of objects in the bag

14

Page 15: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

CRC of Bag

15

Page 16: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

Design Specification of a Bag

• Describe data

• Specify methods for bag’s behaviors

– Name methods

– Choose parameters

– Decide return types

– Write comments

16

Page 17: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 18: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

Design Decisions

• What should happen when an unusualcondition occurs?

– Assume invalid never happens?

– Ignore invalid event?

– Guess at client’s intention?

– Return flag value?

– Return boolean value – success/failure?

– Throw exception?

18

Page 19: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

UML

19

Page 20: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 21: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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 BagInterface21

Page 22: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 23: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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 fromshoppingCart.add(nextItem);totalCost = totalCost + nextItem.getPrice();

} // end forwhile (!shoppingCart.isEmpty()) {

System.out.println(shoppingCart.remove());System.out.println("Total cost: " + "\t$" + totalCost / 100 + "." + totalCost % 100);

} }

23

Page 24: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

• Output

24

Page 25: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

A Vending Machine25

Page 26: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 27: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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

Page 28: Data Abstraction and Abstract Data Typestmengistu/Courses/Fall2015/CS220... · Abstract Data Type •An Abstract Data Type (ADT) can be defined as: –Is a specification of a data

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