DOC PREVIEW
Penn CIT 594 - Abstract Data Types II

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

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

Unformatted text preview:

Abstract Data Types IISufficient operationsNecessary operationsConvenience operationsNecessary and sufficient operationsExample: StringsTypes of operationsRequirementsOperations in JavaFactory methodsExample: StringImmutable objectsExample: StringBufferMutable objectsSafe use of StringsUnsafe use of StringBuffersSummaryThe EndAbstract Data Types II2Sufficient operationsOperations on an ADT are sufficient if they meet all the requirementsThey must be able to create all the values and perform all the operations required by the applicationRemember that the application cannot directly access the internal valuesThey should be able to create all the values and perform all the operations required by any application in a given class of applications3Necessary operationsAn operation on an ADT is necessary if omitting it would fail to meet the requirementsIf the application can implement an operation easily and efficiently by combining other operations, that operation is unnecessaryIt’s OK to have unnecessary operations if they add significantly to the convenience of using the ADT4Convenience operationsAn operation is a convenience operation if it could be accomplished by some overly complex combination of other operationsConvenience operations should be justifiedWill it be used often?Does it really simplify the user’s task?Would the user expect this operation to be provided?Is it significantly more efficient?5Necessary and sufficient operationsA class should define a necessary and sufficient set of operationsConvenience operations should be justifiedSimilarly, a class should have a necessary and sufficient data representationIn general, a class should not contain data that can be easily computed from other data in the class6Example: StringsNecessary and sufficient operators:A constructor: public String(char[] chs)Ways to access data:public int length()public charAt(int index)Would you be happy with just these?If you invented the String class, could you justify operations such as equals and string concatenation?Convenience operators aren’t all bad!7Types of operationsA constructor is creates a value of the ADT from input valuesAn accessor uses a value of the ADT to compute a value of some other typeA transformer uses a value of the ADT to computer another value of the ADTA mutative transformer changes the value of the ADT it is givenAn applicative transformer takes one ADT and, without changing it, returns a new ADT8RequirementsThe constructors and transformers must together be able to create all legal values of the ADTThe accessors must be able to extract any data needed by the application9Operations in JavaConstructors can be implemented with Java constructorsA constructor’s job is to construct an object of a class in a valid stateThat should be a constructor’s only jobAccessors and transformers can be implemented with Java methodsMutative transformers are typically (but not always) implemented as void methodsSometimes they both modify an object and return it10Factory methodsThe problem with a constructor is that it will always construct an object of a given typeThis isn’t always what you wantAnother approach is to embed the call or calls to a constructor inside a “factory” methodExample:public Animal create(String voice) { if (voice.equals("woof")) return new Dog(); if (voice.equals("meow")) return new Cat(); if (voice.equals("moo")) return new Cow(); throw new IllegalArgumentException(voice);}11Example: StringConstructors:"This is syntactic sugar for a constructor"public String(char[] chs)Accessors:public int length()public char charAt()Transformers (applicative only):public String substring(int i, int j)public String concat(String that) (also +)Etc.12Immutable objectsA String is immutable: it cannot be changedThe String class has no mutative transformersOperations such as string concatenation create new StringsAdvantages:Efficient (for most uses)Easy to use and simple to understand (changing a String in one object doesn’t change it in other objects)Disadvantage:Every call to a transformer creates a new String13Example: StringBufferConstructors:public StringBuffer(String s)Accessors:public int length()public char charAt()Transformers (applicative):noneTransformers (mutative):public StringBuffer append(Object obj)Etc.14Mutable objectsA StringBuffer is mutable: it can be changedThe StringBuffer class has both applicative and mutative transformersAdvantage:Efficient (for doing a lot of string manipulation)Disadvantage:Can be confusing (example coming up shortly)Operations on Strings are done by converting to StringBuffers, doing the work, and converting back15Safe use of Strings public class Person { private String name; Person(String name) { this.name = name;} } String jan = "Jan"; Person doctor = new Person(jan); String dan = "D" + jan.substring(1, 2); Person secretary = new Person(dan);16Unsafe use of StringBuffers public class Person { private StringBuffer name; Person(StringBuffer name) { this.name = name;} } StringBuffer buffer = new StringBuffer("Jan"); Person doctor = new Person(buffer); buffer.setCharAt(0, 'D'); Person secretary = new Person(buffer);17SummaryA class should define a necessary and sufficient set of operationsConvenience operations should be justifiedOperations can be classified as:ConstructorsAccessorsTransformers (applicative or mutative)Immutable objects are often preferable to mutable objects18The


View Full Document

Penn CIT 594 - Abstract Data Types II

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