250 likes | 472 Views
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.
E N D
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
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
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
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
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
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
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
Error Handling • Method of supplier class determines client made an error • doesn’t know client • boolean results • other results? • constraint testing in client • Exceptions
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
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
RationalFraction Class • As Rational numbers (p/q, q0) • 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
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
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
Testing Implementation Classes • Test suites • based on interface definitions • Testing exceptions • try/catch statement • syntax • effect • use • Exception handling • debugging • “fail-soft”