Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You...

61
Chapter 2 Chapter 2 Data Design and Data Design and Implementation Implementation
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    1

Transcript of Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You...

Page 1: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Chapter 2Chapter 2

Data Design and Data Design and ImplementationImplementation

Page 2: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

HomeworkHomework

You should have already read section You should have already read section 2.22.2

Read Section 2.3Read Section 2.3 pages 98-133.pages 98-133. It’s a long sectionIt’s a long section Really read itReally read it This will be the only homework for a This will be the only homework for a

week.week.

Page 3: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

DataData

The representation of information in The representation of information in a manner suitable for communication a manner suitable for communication or analysis by humans or machinesor analysis by humans or machines

Data are the nouns of the Data are the nouns of the programming world: programming world: – The objects that are manipulatedThe objects that are manipulated– The information that is processedThe information that is processed

Page 4: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Data TypeData Type

A category of data characterized by A category of data characterized by the supported elements of the the supported elements of the category and the supported category and the supported operations on those elementsoperations on those elements

Simple data type examples: Simple data type examples: integers, real numbers, charactersintegers, real numbers, characters

Page 5: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

DefinitionsDefinitions

Atomic or primitive type Atomic or primitive type A data A data type whose elements are single, type whose elements are single, non-decomposable data itemsnon-decomposable data items

Composite type Composite type A data type whose A data type whose elements are composed of multiple elements are composed of multiple data itemsdata items

Page 6: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Definitions Definitions (cont’d)(cont’d)

Structured composite typeStructured composite type An An organized collection of components organized collection of components in which the organization determines in which the organization determines the means of accessing individual the means of accessing individual data components or subsets of the data components or subsets of the collectioncollection

Page 7: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Atomic (Simple) and Atomic (Simple) and Composite Data TypesComposite Data Types

Page 8: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

DefinitionsDefinitions

Data abstraction Data abstraction The separation of a The separation of a data type’s logical properties from its data type’s logical properties from its implementationimplementation

Data encapsulation Data encapsulation The separation The separation of the representation of data from of the representation of data from the applications that use the data at the applications that use the data at a logical level; a programming a logical level; a programming language feature that enforces language feature that enforces information hiding information hiding

Page 9: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.
Page 10: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Abstract Data Type (ADT)Abstract Data Type (ADT)

Abstract data type (ADT) Abstract data type (ADT) A data type A data type whose properties (domain and operations) whose properties (domain and operations) are specified independently of any are specified independently of any particular implementationparticular implementation

In effect, all Java built-in types are ADTs.In effect, all Java built-in types are ADTs. In addition to the built-in ADTs, Java In addition to the built-in ADTs, Java

programmers can use the Java programmers can use the Java classclass mechanism to build their own ADTs.mechanism to build their own ADTs.

Page 11: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Data StructureData Structure

A collection of data elements whose A collection of data elements whose logical organization reflects a logical organization reflects a relationship among the elementsrelationship among the elements

It is best to design data structures It is best to design data structures with ADTs.with ADTs.

Page 12: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Basic Operations on Encapsulated Basic Operations on Encapsulated DataData

A constructor is an operation that A constructor is an operation that creates a new instance (object) of the creates a new instance (object) of the data type.data type.

Transformers (sometimes called Transformers (sometimes called mutatorsmutators) are operations that change ) are operations that change the state of one or more of the data the state of one or more of the data values.values.

Page 13: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Basic Operations on Encapsulated Basic Operations on Encapsulated DataData

An observer is an operation that allows An observer is an operation that allows us to observe the state of one or more us to observe the state of one or more of the data values with out changing of the data values with out changing them.them.

An iterator is an operation that allows An iterator is an operation that allows us to process all the components in a us to process all the components in a data structure sequentially.data structure sequentially.

Page 14: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Data Levels of an ADTData Levels of an ADT

Logical (or abstract) levelLogical (or abstract) level:: Abstract Abstract view of the data values (the domain) view of the data values (the domain) and the set of operations to and the set of operations to manipulate them.manipulate them.

Application (or user) levelApplication (or user) level: Here the : Here the application programmer uses the application programmer uses the ADT to solve a problem.ADT to solve a problem.

Page 15: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Data Levels of an ADT Data Levels of an ADT (Cont’d)(Cont’d)

Implementation levelImplementation level: A specific : A specific representation of the structure to representation of the structure to hold the data items, and the coding hold the data items, and the coding of the operations in a programming of the operations in a programming language.language.

Page 16: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Communication between the Communication between the Application Level and Implementation Application Level and Implementation

LevelLevel

Page 17: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Java’s Built-In TypesJava’s Built-In Types

Page 18: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

The Java The Java ClassClass Construct Construct

Can be a mechanism for creating Can be a mechanism for creating composite data types.composite data types.

Is composed of named data fields Is composed of named data fields (class and instance variables) and (class and instance variables) and methods.methods.

Is unstructured because the meaning Is unstructured because the meaning is not dependent on the ordering of is not dependent on the ordering of the members.the members.

Page 19: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

RecordsRecords

RecordRecord When a class is used strictly to When a class is used strictly to hold data and does not hide the data.hold data and does not hide the data.

A record is a composite data type A record is a composite data type made up of a finite collection of not made up of a finite collection of not necessarily homogeneous elements necessarily homogeneous elements called fields.called fields.

Accessing is done directly through a Accessing is done directly through a set of named field selectors.set of named field selectors.

Page 20: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Creates an instance of a circle record (object)

Accesses a fieldvalue

Defines a Circle Record (object)

Page 21: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Assignment Statement: Assignment Statement: Primitive Types versus Primitive Types versus

ObjectsObjects

Page 22: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Ramifications of Using Ramifications of Using ReferencesReferences

Aliases—we have two names for the Aliases—we have two names for the same object.same object.

Page 23: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Ramifications of Using Ramifications of Using ReferencesReferences

GarbageGarbage Memory space that has been Memory space that has been allocated to a program but can no longer be allocated to a program but can no longer be accessed by a program.accessed by a program.

Garbage collectionGarbage collection The process of finding all The process of finding all unreachable objects and de-allocating their unreachable objects and de-allocating their storage spacestorage space

Page 24: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Ramifications of Using Ramifications of Using ReferencesReferences

Page 25: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Ramifications of Using Ramifications of Using ReferencesReferences

Parameter PassingParameter Passing All Java arguments are passed by value.All Java arguments are passed by value. If the variable is of a primitive type, the If the variable is of a primitive type, the

actual value (actual value (intint, , doubledouble, and so on) is , and so on) is passed to the method.passed to the method.

If it is a reference type, then the reference If it is a reference type, then the reference that it contains is passed to the method.that it contains is passed to the method.

Page 26: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

What is static?What is static?

public class Circle {public class Circle {public public staticstatic float pi = 3.14; float pi = 3.14;public float xValue;public float xValue;public float yValue;public float yValue;public float radius;public float radius;

}}……Circle c1 = new Circle();Circle c1 = new Circle();Circle c2 = new Circle();Circle c2 = new Circle();

Page 27: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

What is staticWhat is static

It’s the opposite of dynamic; it means It’s the opposite of dynamic; it means “don’t create a new dynamic instance.“don’t create a new dynamic instance.

xValue =

yValue =

radius =

pi

c1

xValue =

yValue =

radius =

pi

c2

3.14

Page 28: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Issue???Issue???

public public staticstatic class Circle { class Circle {public public staticstatic float pi = 3.14; float pi = 3.14;public float xValue = 2;public float xValue = 2;public float yValue = 3;public float yValue = 3;public float radius = 6;public float radius = 6;

}}……Circle c1 = new Circle(); Circle c1 = new Circle(); Is this allowed??? Is this allowed???Circle c2 = new Circle();Circle c2 = new Circle();

Page 29: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

IssuesIssues

Perhaps this is what happens?Perhaps this is what happens?

c1 c2

xValue =

yValue =

radius =

pi =

2

3

6

3.14

Page 30: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

This is what happensThis is what happens

xValue =

yValue =

radius =

pi

c1

xValue =

yValue =

radius =

pi

c2

xValue =

yValue =

radius =

pi =

static instance

2

3

6

3.14

Page 31: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

InterfacesInterfaces

1.1. All variables declared in an All variables declared in an interface must be interface must be finalfinal,, static static variables.variables.

2.2. All methods declared in an interface All methods declared in an interface must be abstract.must be abstract.

3.3. Abstract methodAbstract method A method A method declared in a class or an interface declared in a class or an interface without a method body.without a method body.

Page 32: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

An Example of an InterfaceAn Example of an Interface

Page 33: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Interfaces can be used in the following Interfaces can be used in the following ways:ways:

As a contractAs a contract—Capture an abstract —Capture an abstract view in an interface.view in an interface.

To share constantsTo share constants—Define —Define constants in an interface and have constants in an interface and have each class implement the interface.each class implement the interface.

Page 34: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

To replace multiple inheritanceTo replace multiple inheritance—A —A class can extend one super-class, but class can extend one super-class, but it can implement many interfaces.it can implement many interfaces.

To provide a generic type To provide a generic type mechanismmechanism—In Chapter 3 you learn —In Chapter 3 you learn how to use the Java interface how to use the Java interface construct to provide generic construct to provide generic structures.structures.

Page 35: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

ArraysArraysAn array differs from a class in the An array differs from a class in the

following three ways:following three ways:1.1. An array is a homogenous structure, whereas An array is a homogenous structure, whereas

classes are heterogeneous structures.classes are heterogeneous structures.2.2. A component of an array is accessed by its A component of an array is accessed by its

position in the structure, whereas a position in the structure, whereas a component of a class is accessed by an component of a class is accessed by an identifier (the name).identifier (the name).

3.3. Because array components are accessed by Because array components are accessed by position, an array is a structured composite position, an array is a structured composite type.type.

Page 36: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Ways of Combining our Built-In Ways of Combining our Built-In Types and Classes into Versatile Types and Classes into Versatile

HierarchiesHierarchies1.1. Aggregate objectsAggregate objects—The instance —The instance

variables of our objects can variables of our objects can themselves be references to themselves be references to objects.objects.

2.2. Arrays of objectsArrays of objects—Simply define —Simply define an array whose components are an array whose components are objects.objects.

Page 37: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

3.3. Two-dimensional arraysTwo-dimensional arrays—To —To represent items in a table with rows represent items in a table with rows and columns.and columns.

4.4. Multilevel HierarchiesMultilevel Hierarchies—Create —Create whatever sort of structure best whatever sort of structure best matches our data. matches our data.

Examples…Examples…

Ways of Combining our Built-In Types Ways of Combining our Built-In Types and Classes into Versatile Hierarchies and Classes into Versatile Hierarchies

(Cont’d)(Cont’d)

Page 38: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Example of an Aggregate Example of an Aggregate ObjectObject

Suppose we define:Suppose we define:

public class Pointpublic class Point{{public int xValuepublic int xValuepublic int yValuepublic int yValue

Then, we could define a new circle class as:Then, we could define a new circle class as: Public class NewCirclePublic class NewCircle{{public Point location;public Point location;public float radius;public float radius;public boolean solid;public boolean solid;}}

Page 39: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Example of an Array of Example of an Array of ObjectsObjects

Page 40: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

The The allCirclesallCircles Array Array

Page 41: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Example of a Two-Example of a Two-Dimensional ArrayDimensional Array

Page 42: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Sources for ClassesSources for Classes

The Java Class LibraryThe Java Class Library—A class library that —A class library that includes hundreds of useful classes.includes hundreds of useful classes.

Build your ownBuild your own—You create the needed —You create the needed class, possibly using pre-existing classes in class, possibly using pre-existing classes in the process.the process.

Off the shelfOff the shelf—Software components, such —Software components, such as classes or packages of classes, which as classes or packages of classes, which are obtained from third party sources.are obtained from third party sources.

Page 43: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Some Important Library Some Important Library PackagesPackages

Page 44: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Some Useful General Some Useful General Library ClassesLibrary Classes

The System ClassThe System Class—To obtain current —To obtain current system propertiessystem properties

The Random ClassThe Random Class—to generate a —to generate a list of random numberslist of random numbers

The DecimalFormat ClassThe DecimalFormat Class—to format —to format numbers for outputnumbers for output

Page 45: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Some Useful General Some Useful General Library Classes Library Classes

WrappersWrappers—to wrap a primitive valued —to wrap a primitive valued variable in an object’s structurevariable in an object’s structure

The Throwable and Exception ClassesThe Throwable and Exception Classes—An exception is associated with an —An exception is associated with an unusual, often unpredictable event, unusual, often unpredictable event, detectable by software or hardware, detectable by software or hardware, that requires special processing.that requires special processing.

Page 46: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

ExceptionsExceptions

One system unit raises or One system unit raises or throws throws and exceptionand exception, and another unit , and another unit catches the exceptioncatches the exception and and processes (handles) it.processes (handles) it.– Throw an exceptionThrow an exception—Interrupt the —Interrupt the

normal processing of a program to raise normal processing of a program to raise an exception that must be handled or an exception that must be handled or rethrown by surrounding block or code.rethrown by surrounding block or code.

Page 47: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Exceptions ContinuedExceptions Continued

– Catch an exceptionCatch an exception—Code that is —Code that is executed to handle a thrown exception executed to handle a thrown exception is said to catch the exception.is said to catch the exception.

Page 48: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Catching and Handling an Catching and Handling an ExceptionException

Page 49: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

The The ArrayListArrayList Class Class

Part of the Part of the java.utiljava.util package. package. Functionality is similar to that of an Functionality is similar to that of an

array.array. However, an array list can grow and However, an array list can grow and

shrink; its size is not fixed for its shrink; its size is not fixed for its lifetime.lifetime.

Page 50: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Array ListsArray Lists

Hold variables of type Hold variables of type ObjectObject.. Capacities grow and shrink, depending on Capacities grow and shrink, depending on

need.need. An array list has a size indicating how An array list has a size indicating how

many objects it is currently holding, andmany objects it is currently holding, and Has a capacity indicating how many Has a capacity indicating how many

elements the underlying implementation elements the underlying implementation could hold without having to be increased. could hold without having to be increased.

Page 51: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.
Page 52: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Use an array list whenUse an array list when

1.1. Space is an issue.Space is an issue.2.2. Execution time is not an issue.Execution time is not an issue.3.3. The amount of space required The amount of space required

changes drastically from one changes drastically from one execution of the program to the execution of the program to the next.next.

4.4. The actively used size of an array The actively used size of an array list changes a great deal during list changes a great deal during execution.execution.

Page 53: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Use an array list when Use an array list when (Cont’d)(Cont’d)

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

6.6. Most of the insertions and deletions Most of the insertions and deletions to the array list take place at the to the array list take place at the size index.size index.

Page 54: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Access ModifiersAccess Modifiers

Page 55: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Shallow CopyShallow Copy

An operation that copies a source class An operation that copies a source class instance to a destination class instance to a destination class instance, simply copying all instance, simply copying all references so that the destination references so that the destination instance contains duplicate instance contains duplicate references to values that are also references to values that are also referred to by the source.referred to by the source.

Page 56: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Deep CopyDeep Copy

An operation that copies one class An operation that copies one class instance to another, using observer instance to another, using observer methods as necessary to eliminate methods as necessary to eliminate nested references and copy only the nested references and copy only the primitive types that they refer to. primitive types that they refer to. The result is that the two instances The result is that the two instances do not contain any duplicate do not contain any duplicate references. references.

Page 57: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Show picturesShow pictures

Then end!Then end! There should be no problems in lab There should be no problems in lab

Page 58: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Options when dealing with Options when dealing with error situations within our error situations within our

ADT methodADT method

Detect and handle the error Detect and handle the error within the method itself.within the method itself.

Throw an exception related to Throw an exception related to the error.the error.

Ignore the error situation.Ignore the error situation.

Page 59: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Designing ADTsDesigning ADTs

1.1. Determine the general purpose.Determine the general purpose.

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

3.3. Identify a set of public methods to Identify a set of public methods to be provided by the ADT class that be provided by the ADT class that allow the application program to allow the application program to perform the desired operations.perform the desired operations.

Page 60: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Designing ADTsDesigning ADTs

4.4. Identify other public methods which Identify other public methods which help make the ADT generally help make the ADT generally usable.usable.

5.5. Identify potential error situations Identify potential error situations and classify intoand classify into

1.1. Those that are handled by throwing an Those that are handled by throwing an exceptionexception

2.2. Those that are handled by contractThose that are handled by contract

3.3. Those that are ignored.Those that are ignored.

Page 61: Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.

Designing ADTsDesigning ADTs

6.6. Define the needed exception classes.Define the needed exception classes.7.7. Decide how to structure the data.Decide how to structure the data.8.8. Decide on a protection level for the Decide on a protection level for the

identified data.identified data.9.9. Identify private structures and Identify private structures and

methods.methods.10.10. Implement the ADT.Implement the ADT.11.11. Create a test driver and test your Create a test driver and test your

ADT.ADT.