DOC PREVIEW
Penn CIT 594 - Abstract Data Types

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 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 19 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 19 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 19 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 19 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 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Abstract Data TypesData typesSlide 3Classes in JavaADTs are better than DTsExample of an ADTData representation in an ADTExample of setters and gettersAside: Naming setters and gettersWhat’s the point?And another thing...ContractsImportance of the contractPromise no more than necessaryImplementing an ADTWriting the contractInterfacesSummaryThe EndJan 13, 2019Abstract Data Types2Data types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 values3Abstract 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?4Classes in JavaA class defines a data typeThe possible values of a class are called objectsThe operations on the objects are called methodsThe data representation is all the fields that are contained within the objectIf there is no external access to the data representation, you have an abstract data typeSun’s Java classes are (almost all) abstract data types5ADTs are better than DTsIt is the responsibility of a class to protect its own data, so that objects are always in a valid stateInvalid objects cause program bugsBy keeping the responsibility in one place, it is easier to debug when invalid objects are presentThe less the user has to know about the implementation, the easier it is to use the classThe less the user does know about the implementation, the less likely s\he is to write code that depends on it“Less is more”6Example of an ADTStack s = new Stack();s.push("Hello");String a = s.peek();Color b = s.pop();if (s.empty()) {...}int i = s.search("Hello");The operations are the methods shown on the leftThe values are all the possible stacksWe can construct any value with the supplied operationsWe can discover the value by using the given operationsWe don’t care about the representation (so long as it works well!)“Less is more”7Data 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 getters8Example 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; }}9Aside: Naming 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, you can replace get with is (for example, isRunning) This is more than just a convention—if and when you start using JavaBeans, it becomes a requirement10What’s the point?We can claim that a class is an abstract data type if we make all data privateHowever, if we then provide unlimited access by providing simple getters and setters for everything, we are only fooling ourselvesRules:Only supply getters and setters if there is a clear use for themDo not supply getters and setters that depend on the particular implementation you are usingNever, never write a setter that could be used to create an invalid object!Your setters should do all possible error checking before they change the object11And another thing...Setters and getters allow you to keep control of your implementationFor example, you decide to define a Point in a plane by its x-y coordinates:class Point { public int x; public int y; }Later on, as you gradually add methods to this class, you decide that it’s more efficient to represent a point by its angle and distance from the origin, θ and ρSorry, you can’t do that—you’ll break too much code that accesses x and y directlyIf you had used setters and getters, you could redefine them to compute x and y from θ and ρ12ContractsEvery ADT should have a contract (or specification) that:Specifies the set of valid values of the ADTSpecifies, for each operation of the ADT:Its nameIts parameter typesIts result type, if anyIts observable behaviorDoes not specify:The data representationThe algorithms used to implement the operations13Importance of the contractA contract is an agreement between two parties; in this caseThe implementer of the ADT, who is concerned with making the operations correct and efficient, and also with preserving the flexibility to make changes laterThe applications programmer, who just wants to use the ADT to get a job doneIt doesn’t matter if you are both of these parties; the contract is still essential for good codeThis separation of concerns is essential in any large project14Promise no more than necessaryFor a general API, the implementer should provide as much generality as feasibleThis is the way Sun’s classes are (mostly) writtenBut for a specific program, the class author should provide only what is essential at the momentIn Extreme Programming terms, “You ain’t gonna need it!”In fact, XP practice is to remove functionality that isn’t currently needed!Your documentation should not expose anything that the application programmer does not need to knowIf you design for generality, it’s easy to add functionality later—but removing it may have serious consequences15Implementing an ADTTo implement an ADT, you need to choose:a data representation thatmust be able to represent all possible values of the ADTshould be privatea set of methods that support normal use of the ADTThe user must be able to create, possibly modify, and examine the values of the ADTan algorithm for each of the possible operations thatmust be consistent with the chosen representationall auxiliary (helper) operations that are not in the contract should be private16Writing the contractIn most cases, the Javadoc for a class is the contractThis means:The Javadoc documentation should describe what the class is for and how it should be usedThe Javadoc


View Full Document

Penn CIT 594 - Abstract Data Types

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 Types
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 Types 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 Types 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?