1 / 24

Abstract Data Types

Abstract Data Types. Data Abstraction. Data abstraction Abstract data type (ADT) Data type Data structure Information hiding reduction of complexity security modifiability Programming-by-contract supplier vs client specification vs realization Collection (container) types.

traci
Download Presentation

Abstract Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract Data Types

  2. Data Abstraction • Data abstraction • Abstract data type (ADT) • Data type • Data structure • Information hiding • reduction of complexity • security • modifiability • Programming-by-contract • supplier vs client • specification vs realization • Collection (container) types

  3. Data Abstraction in Java • The class and information hiding • public vs private instance variables, methods • Interface • specification only • defines a type • contract • Class includes specification and implementation • modification • multiple implementations? • Exception • reporting contract violations (errors) • Package • aggregation of interfaces and classes • Libraries • .jar files

  4. Fraction ADT • Not a built-in type • approximations: double • round-off error • When desire precise calculations • Values • mixed fractions • of the form p/q where p & q are integers • Operations • arithmetic: +, -, *, / • comparison • compatibility with other numeric types • conversions • string representation • for I/O

  5. Package • Collection of related interfaces and classes • Libraries • Syntax • package names • Unnamed package • Import • package.* - all public interfaces and classes in package • package.name – only specific class • Class visibility • class modifiers • public or package (no modifier) • Package visibility • implementation defined

  6. Interface • Defines a type • can declare variables • any class that implements is compatible • No implementation • Syntax • constants and method headers only • Compilation unit • can compile & use for verifying implementations & clients

  7. Fraction Interface • Fractions (library) package • Immutable • a “value” class • no way to change a Fraction object, only create new ones • no updater methods • operations return Fraction as result • Minimal set of operations • Accessor methods • Conversion • constructors for conversion • conversion methods • loss of information • explicit

  8. Comparison • Arithmetic • methods • Fraction results • functional notation • e.g. e = f.mul(g).add(h) • intermediate objects • Defines a type • more than one possible implementation

  9. Error Handling • Method of supplier class determines client made an error • doesn’t know client • boolean results • other results? • constraint testing in client • Exceptions

  10. Exceptions • Three kinds • unrecoverable errors • program errors • recoverable exceptions • Contract violations • program error • Declaration • exception is a class • subtype of some Exception class • Throwing • throw statement • catching • propagation

  11. Implementation Classes • Class which supplies implementation of a specification (interface) • implements clause • implementing multiple interfaces • Requirements • all methods in interface • maybe toString • useful for debugging • error handling • exceptions • Constructors • default constructor • chaining

  12. RationalFraction Class • As Rational numbers (p/q, q0) • pair of integers • numerator (num) • denominator (denom) • Multiple representations of same value? • 1/2, 2/4, 3/6, … • 1/2, -1/-2 and -1/2, 1/-2 • 0/1, 0/2, … • Whole numbers • Mixed fractions • Canonical (normal) form • lowest terms • sign • zero • exception

  13. Constructors • constructor chaining • normalization • String to Fraction conversion • cannot chain • Conversions • Arithmetic • normalization • creation of result • what implementation type? • Normalization • exception • sign • exclusive or (^) • GCD

  14. Building Libraries • .jar files • “java archive file” • collection of java .class files that makes up a library • Creating a .jar file • can create a .jar file from a project in DrJava and JCreator • Accessing a custom library • must be included on “classpath” (where Java compiler looks for libraries) • set property for client project in DrJava or JCreator

  15. Testing Implementation Classes • Test suites • based on interface definitions • Testing exceptions • try/catch statement • syntax • effect • use • Exception handling • debugging • “fail-soft”

More Related