DOC PREVIEW
Penn CIT 594 - Abstract Data Type

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Abstract Data TypesData types IData types IIPrimitive types in JavaPrimitive types as data typesClasses in JavaMethods and operatorsMethods are operatorsInsertion into a listCognitive loadEfficiencySlide 12Data StructuresData representation in an ADTExample of setters and gettersNaming setters and gettersWhat’s the point?ContractsImportance of the contractPromise no more than necessaryImplementing an ADTExample contract (Javadoc)Implementation, page 1Implementation, page 2ResponsibilitiesAside: an interesting bugSummaryThe EndJan 13, 2019Abstract Data Types2Data types IWe type data--classify it into various categories--such as int, boolean, String, AppletA data type represents a set of possible values, such as {..., -2, -1, 0, 1, 2, ...}, or {true, false}By typing our variables, we allow the computer to find some of our errorsSome operations only make sense when applied to certain kinds of data--multiplication, searchingTyping simplifies internal representationA String requires more and different storage than a boolean3Data types IIA data type is characterized by:a set of valuesa data representation, which is common to all these values, and a set of operations, which can be applied uniformly to all these values4Primitive types in JavaJava provides eight primitive types:booleanchar, byte, short, int, longfloat, doubleEach primitive type hasa set of valuesa data representationa set of operationsThese are “set in stone”—there is nothing the programmer can do to change anything about them5Primitive types as data typesType Values Representation Operations boolean true, false Single byte &&, ||, ! char, byte, short, int, long Integers of varying sizes Two’s complement +, -, *, /, others float, double Floating point numbers of varying sizes and precisions Two’s complement with exponent and mantissa +, -, *, /, others6Classes in JavaA class is 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You can (and must) define your own, as well7Methods and operatorsAn operator typicallyIs written with non-alphabetic characters: +, *, ++, +=, &&, etc.Is written as prefix, infix, or postfix: -x, x+y, x++Has only one or two arguments, or operandsA method (or function) typicallyIs written with letters, and its arguments are enclosed in parentheses: toString(), Math.abs(n)Has any (predetermined) number of arguments8Methods are operatorsThe differences between methods and operations are only syntactic differences, not fundamental onesMany languages (not including Java) let you define new operators, that is, new syntaxWhen you define a new class and its methods, you are, fundamentally, defining a new data type and its operatorsSuppose a language defines the operator @ to mean “times 3 plus 1”; for example @7 is 22Would you consider this a good operation to have in the language?What does this suggest about defining classes and their methods?9Insertion into a listThere are many ways you could insert a new node into a list:Is it a good idea to supply all of these?If not, why not?• As the new first element• As the new last element• Before a given node• After a given node• Before a given value• After a given value• Before the nth element• After the nth element• Before the nth from the end• After the nth from the end• In the correct location to keep the list in sorted order10Cognitive loadHuman minds are limited—you can’t remember everythingYou probably don’t even remember all the Java operators for integersWhat’s the difference between >> and >>> ?What about between << and <<< ?We want our operators (and methods) to be useful and worth remembering11EfficiencyA list is just a sequence of values—it could be implemented by a linked list or by an arrayInserting as a new first element is efficient for a linked list representation, inefficient for an arrayAccessing the nth element is efficient for an array representation, inefficient for a linked listInserting in the nth position is efficient for neitherDo we want to make it easy for the user to be inefficient?Do we want the user to have to know the implementation?12Abstract Data TypesAn Abstract Data Type (ADT) is:a set of valuesa set of operations, which can be applied uniformly to all these valuesTo abstract is to leave out information, keeping (hopefully) the more important partsWhat part of a Data Type does an ADT leave out?13Data StructuresMany kinds of data consist of multiple parts, organized (structured) in some wayA data structure is simply some way of organizing a value that consists of multiple partsHence, an array is a data structure, but an integer is notWhen we talk about data structures, we are talking about the implementation of a data typeIf I talk about the possible values of, say, complex numbers, and the operations I can perform with them, I am talking about them as an ADTIf I talk about the way the parts (“real” and “imaginary”) of a complex number are stored in memory, I am talking about a data structureAn ADT may be implemented in several different waysA complex number might be stored as two separate doubles, or as an array of two doubles, or even in some bizarre way14Data representation in an ADTAn ADT must obviously have some kind of representation for its dataThe user need not know the representationThe user should not be allowed to tamper with the representationSolution: Make all data privateBut what if it’s really more convenient for the user to have direct access to the data?Solution: Use setters and getters15Example of setters and gettersclass Pair {private int first, last;public getFirst() { return first; }public setFirst(int first) { this.first = first; }public getLast() { return last; }public setLast(int last) { this.last = last; }}16Naming setters and gettersSetters and getters should be named by:Capitalizing the first letter of the variable (first becomes First), andPrefixing the name with get or set (setFirst)For boolean variables, replace get with is (for example,


View Full Document

Penn CIT 594 - Abstract Data Type

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download Abstract Data Type
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Abstract Data Type and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Abstract Data Type 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?