program realisation 2 today s topics tu e software
play

Program Realisation 2 Todays Topics TU/e Software Engineering - PowerPoint PPT Presentation

Program Realisation 2 Todays Topics TU/e Software Engineering Reference Guide http://www.win.tue.nl/hemerik/2IP20/ science versus engineering Lecture 2 Introduction to Abstract Data Types (ADTs) Kees Hemerik User Tom Verhoe


  1. Program Realisation 2 Today’s Topics • TU/e Software Engineering Reference Guide http://www.win.tue.nl/˜hemerik/2IP20/ science ‘versus’ engineering Lecture 2 • Introduction to Abstract Data Types (ADTs) Kees Hemerik User Tom Verhoe ff specification (read/write), usage , implementation Contract Maker Technische Universiteit Eindhoven Faculteit Wiskunde en Informatica • Simple ADTs in Delphi Object Pascal Software Engineering Technology O Feedback to T.Verhoeff@TUE.NL syntax , semantics , pragmatics O . O � 2006, T. Verhoe ff @ TUE.NL c 1 Program Realization 2: Lecture 2 � 2006, T. Verhoe ff @ TUE.NL c 2 Program Realization 2: Lecture 2 (Data) Types Example of Type Usage A (data) type is a set of values and accompanying operations 1 var v : T; { variable v declared of type T } 2 3 Compare to an algebra in mathematics. In Pascal: 4 begin { memory for v allocated, value undefined } 5 • Boolean , Integer , Real , Char , ReadLn(...v...) 6 7 8 ; ...v... := ... • enumeration (Red, Green, Blue) , subrange ’0’..’9’ , 9 10 ; if ...v... then ... • string , array , record , set , TextFile , file , 11 12 ; WriteLn(...v...) 13 ˆNode , class , procedural • pointer 14 end { memory for v de-allocated } � 2006, T. Verhoe ff @ TUE.NL c 3 Program Realization 2: Lecture 2 � 2006, T. Verhoe ff @ TUE.NL c 4 Program Realization 2: Lecture 2

  2. Example of Type Usage (2) Limitations of ‘Classic’ Pascal Types 1 type 1 type Point = record Axis = (x, y); 2 2 • Memory allocation for storage : x, y: Integer; Point = array 3 3 end ; [ Axis ] of Integer; 4 4 – same size for all values, determined at compile time 5 5 6 var 6 var – tied to scope of variables p : Point; p : Point; 7 7 8 8 • Syntax for operations : 9 begin 9 begin 10 10 – ad hoc, dependent on type p.x := 0 p[x] := 0 11 11 12 ; p.y := 0 12 ; p[y] := 0 – change of definition requires change of all using occurrences 13 13 14 ... 14 ... � 2006, T. Verhoe ff @ TUE.NL c 5 Program Realization 2: Lecture 2 � 2006, T. Verhoe ff @ TUE.NL c 6 Program Realization 2: Lecture 2 Abstract Data Type (ADT): Definition of Concept Simple Abstract Data Type: Interface Syntax • Type name TStringList An Abstract Data Type is a type whose specification and usage abstracts from (i.e. does not depend on) implementation details. • Operations (methods): procedure , function , property The implementation of an ADT can be changed, without a ff ecting – Constructor, destructor (mem. mgmt) Create, Free the using occurrences, provided it adheres to the ADT’s specification. – Queries (state inspection) Count, Strings An ADT can serve as a module of a program. – Commands (state change) LoadFromFile, Sort � 2006, T. Verhoe ff @ TUE.NL c 7 Program Realization 2: Lecture 2 � 2006, T. Verhoe ff @ TUE.NL c 8 Program Realization 2: Lecture 2

  3. Simple Abstract Data Type: Usage Syntax Simple Abstract Data Type: Usage Syntax (2) 10 begin v := TStringList.Create { create empty list } 11 12 13 ; v.LoadFromFile(’strings.in’) { read v from text file } 1 program TStringListExample; 14 { illustrates the use of the ADT TStringList } 2 15 ; if v.Count <> 0 then begin { v is not empty } 3 WriteLn(v.Strings[0]) { write first string in v } 16 4 uses end 17 Classes; { has a few ADT definitions } 5 18 6 19 ; v.Sort { sort v } 7 var 20 v : TStringList; { a dynamic list of strings } 8 21 ; v.SaveToFile(’strings.out’) { write v to text file } 22 23 ; v.Free { destroy v and de-allocate memory } 24 end . � 2006, T. Verhoe ff @ TUE.NL c 9 Program Realization 2: Lecture 2 � 2006, T. Verhoe ff @ TUE.NL c 10 Program Realization 2: Lecture 2 Simple Abstract Data Type: Interface Semantics = Contract Rectangles ADT in Delphi Object Pascal 1 unit Rectangles; { provides ADT for axis-parallel grid rectangles } 2 • Set of (abstract) values 3 4 interface 5 • Contracts for operations 6 type TRectangle = class (TObject) // axis-parallel grid rectangles 7 – preconditions private (* ignore implementation details *) 8 – postconditions or e ff ects public 15 // construction 16 – invariants constructor Create(AXL, AYL, AXH, AYH: Integer); 17 // pre: AXL <= AXH /\ AYL <= AYH 18 // post: XL=AXL /\ YL=AYL /\ XH=AXH /\ YH=AYH 19 � 2006, T. Verhoe ff @ TUE.NL c 11 Program Realization 2: Lecture 2 � 2006, T. Verhoe ff @ TUE.NL c 12 Program Realization 2: Lecture 2

  4. Rectangles ADT in Delphi Object Pascal Rectangles ADT in Delphi Object Pascal function Intersects( const ARectangle: TRectangle): Boolean; 20 34 // basic queries // pre: true 21 35 function XL: Integer; // lower X coordinate // ret: Self intersects ARectangle 22 36 function YL: Integer; // lower Y coordinate 23 37 function XH: Integer; // higher X coordinate //commands 24 38 function YH: Integer; // higher Y coordinate procedure SetXL(AX: Integer); 25 39 // pre: AX <= XH 26 40 // invariants // effect: XL := AX 27 41 // WellFormed: XL <= XH /\ YL <= YH procedure SetYL(AY: Integer); 28 42 // pre: AY <= YH 29 43 // derived queries // effect: YL := AY 30 44 function Contains(AX, AY: Integer): Boolean; procedure SetXH(AX: Integer); 31 45 // pre: true // pre: XL <= AX 32 46 // ret: XL <= AX <= XH /\ YL <= AY <= YH // effect: XH := AX 33 47 � 2006, T. Verhoe ff @ TUE.NL c 12 Program Realization 2: Lecture 2 � 2006, T. Verhoe ff @ TUE.NL c 12 Program Realization 2: Lecture 2 Rectangles ADT in Delphi Object Pascal Using Rectangles ADT in Delphi Object Pascal 1 program RectanglesTest; procedure SetYH(AY: Integer); 48 2 // pre: YL <= AY 49 3 uses // effect: YH := AY 50 Rectangles; 4 procedure Shift(AX, AY: Integer); 51 5 // pre: true 52 6 var // effect: XL, YL, XH, YL := XL+AX, YL+AY, XH+AX, YH+AY 53 r, s: TRectangle; 7 procedure Intersect( const ARectangle: TRectangle); 54 8 // pre: Intersects(ARectangle) 55 9 begin // effect: Self := Self intersection ARectangle 56 r := TRectangle.Create(0, 0, 200, 100) 10 procedure Hull( const ARectangle: TRectangle); 57 11 ; s := TRectangle.Create(100, 50, 300, 150) // pre: true 58 12 ; r.Intersect(s) // effect: Self := smallest rectangle enclosing Self and ARectangle 59 13 ; if r.Contains(50, 50) then writeln(’Huh?’) end ; { class TRectangle } 60 14 end . � 2006, T. Verhoe ff @ TUE.NL c 12 Program Realization 2: Lecture 2 � 2006, T. Verhoe ff @ TUE.NL c 13 Program Realization 2: Lecture 2

  5. What Lies Ahead • How to implement ADTs • Performance issues: memory, speed • How to design complete ADTs from scratch • Relationships between ADTs � 2006, T. Verhoe ff @ TUE.NL c 14 Program Realization 2: Lecture 2

Recommend


More recommend