Top Banner
Data Design and Implementation
35

Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Jan 03, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Data Design and Implementation

Page 2: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Definitions of Java TYPESAtomic or primitive type

A data type whose elements are single, non-decomposable data items - e.g, an int or a float variable

Composite type :

A data type whose elements are composed of multiple data items e.g. A CLASS

2

Page 3: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Structured composite type Structured composite type:

An organized collection of components . The organization determines how to access the components of the collection

E.g. An Array or A Tree

3

Page 4: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Atomic (Simple) and Composite Data Types

4

Page 5: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Java’s Built-In Types

5

Page 6: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

The Java Class Construct

is used to create composite, composite, unstructured data types.unstructured data types.

It is composed of :It is composed of :

11. . named data fields ( named data fields ( instance variables) instance variables)

2. 2. methods.methods.

6

Page 7: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

An array differs from a class : An array is a homogenous structure, whereas classes are heterogeneous structures. E.g. an array of integers has only integers in it.

A class has variables of different types and methods

7

Page 8: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Accessing components

A component of an array is accessed by its position in the array e.g.

board[2] [3]

8

Page 9: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Arrays

Because array components are accessed by position,

an array is a structured composite type.

9

Page 10: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Component of square:

A component of a class is accessed by an identifier (the object created of it).

Square sq = new Square(10,20, 40, Color.Red);Square sq = new Square(10,20, 40, Color.Red);

int x = sq.getX();

We access the parts of the class with an We access the parts of the class with an objectobject

10

Page 11: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

11

Because class components are accessed by creating an object,

an class is a unstructured composite type.

Page 12: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

DefinitionsData abstraction: The separation of a

data type’s logical properties from its implementation

A doctor sees (abstracts) the person as patient.  E.g. name, height, weight of a person

An employer sees (abstracts) a person as Employee. E.g.name, age,  health, degree of study, of a person.  

12

Page 13: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Data encapsulation

We can abstract the important characteristics of Person

and store all the characteristics of a person in Person class

The Person class can be used by both the Doctor or Employer classes or ……..others

13

Page 14: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Abstract Data Type

An abstract data type is an interface

a) it defines a collection of

data e.g. an array of clients

b)and the particular operations that are allowed on that array

( through methods )

14

Page 15: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

15

An ADT :

therefore has a name E.G. A STACK, A QUEUE, An ARRAYLIST

A domain of values(data) - the elements in the stack, items stored in the array in the ArrayList

a set of operations that can be performed (methods) on the data

ABSTRACT DATA TYPES – ADT’ s

Page 16: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Collections- Details A collection is an object that

serves as a repository for other objects.

MOST ADT are Collections - since they store the data they are operating on

The Java Collections Framework contains classes that represent collections of objects.

16

Page 17: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Collections & ADT’s In general, a collection refers to a class

whose role is to:

provide methods to add, remove provide methods to add, remove and and

manage the elements that are in the collection.

17

Page 18: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Collections - ArrayList

The ArrayList class in the Collections Framework is implemented with an array.

There is also a SortedArrayList, which maintains the array in sorted order

18

Page 19: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Collections & ADT’s Collections can be implemented in a variety of

ways.

The ArrayList uses an ArrayArray

and contains methodsmethods that perform operations on array.

e.g. Adding or deleting items etc.

19

Page 20: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

ADT’s

ADT’s can be implemented with both

e.g. arrays or linked lists.

The choice of an array or a linked list depends upon the implementation

20

Page 21: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

The ArrayList Class IS Part of the java.util package.

An ArrayList list can grow and shrink depending on need.

When the array is full, its size is doubled

21

Page 22: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

ARRAYLIST

ArrayList class: you store ArrayList class: you store variables of type variables of type ObjectObject. .

Or now type T - a generic type built on class Object

22

Page 23: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

ArrayList

An ArrayList has a size() method - how many objects it is currently holding

It has other methods :

add, remove, find , isEmpty etc. See:

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html 23

Page 24: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

ArrayList ArrayList OperationsOperations

24

Page 25: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Use Arraylist – unsorted - when: The amount of space required may change

from one execution of the program to the next.

The position of an element in the array list has no relevance to the application.

There is no order to the items stored

25

Page 26: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

26

Collection Interface & Map Interface

Page 27: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

27

LIST & SET INTERFACEs

Page 28: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

28

Page 29: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

public interface List<E> extends Collection<E> { E get(int index); E set(int index, E element); boolean add(E element); void add(int index, E element); E remove(int index);

boolean addAll(int index, Collection<? extends E> c); int indexOf(Object o); int lastIndexOf(Object o);

ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); }

29

Page 30: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Collection Interface-List inherits these methods public interface Collection<E> extends

Iterable<E> { int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); boolean remove(Object element); Iterator<E> iterator(); boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); boolean removeAll(Collection<?> c); boolean retainAll(Collection<?> c); void clear(); public <T> T[] toArray(T[] a);

30

Page 31: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Designing ADTs this term

Determine the general purpose of your program.

List the specific types of operations the application application program performs.program performs.

31

Page 32: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

DESIGNING AN ADT

Decide first which ADT to use: an ArrayList , A Stack?

Depends on the problem

Identify a set of public methods to be provided by the ADT class to perform the desired operations

32

Page 33: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Designing ADTs

Identify potential error situations and classify into

1. Those that are handled by throwing an exception

2. Those that are ignored.

33

Page 34: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

Designing ADTs6. Define the needed exception

classes.

7. Decide on a protection level for the identified data.

8. Implement the ADT. ( Using the ARRAYLIST OR STACK

9. Create a test driver and test your ADT.

34

Page 35: Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.

3535

Analysis of List Implementations In both array and linked implementations,

many operations are similar in efficiency

Most are O(1),

except when shifting or searching occurs, in which except when shifting or searching occurs, in which case they are order O(n)case they are order O(n)