1 / 23

ITEC 320

ITEC 320. Lecture 10 Packages (2). Review. Packages What parts do they have? Syntax?. Store in a .ads file. Specification. Tells the compiler what the package does, but not how to do it. package PairPkg is type Pair is record x, y: Integer; end record

marvin
Download Presentation

ITEC 320

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. ITEC 320 Lecture 10 Packages (2)

  2. Review • Packages • What parts do they have? • Syntax?

  3. Store in a .ads file Specification • Tells the compiler what the package does, but not how to do it package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;

  4. Body • Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;

  5. Client with ada.integer_text_io; use ada.integer_text_io; with pairpkg; use pairpkg; procedure pairclient is p: Pair; begin p := newPair(1, 2); put(distanceToOrigin(p)); p.x:= 3; -- Syntax error end pairclient;

  6. Code • Input parser • Reads a line • Returns int / String / Float based on whitespace • Specification • Body • Client What should be written first? Privacy?

  7. Comparison • Java’s Scanner • Similarities • Differences

  8. Currently What happens when you want to store Strings? packageIntStackPkgistypeStackTypeislimitedprivate;Stack_Is_Empty, Stack_Is_Full : Exception;functionIsEmpty (S : StackType) return Boolean;functionIsFull  (S : StackType) return Boolean;procedure Push (I : Integer; S : inoutStackType);procedure Pop  (S : inoutStackType);function  Top  (S : StackType) return Integer;privateMaxSize : Constant Integer := 1000;typeElementArrayisarray(1 .. MaxSize) of Integer;typeStackTypeisrecord        Elements : ElementArray;TheTop : Integer := 0;end record;endIntStackPkg;

  9. Generic • Formal parameters • Specification • Actual parameters • Client • Need: get away from specific types

  10. Generic generic  -- Generic parameters are declared here    Size : Positive;            -- Size of stack to createtypeItemTypeisprivate;   -- Type of elements packageStackPkgistype Stack islimitedprivate;Stack_Empty: exception; Stack_Full : exception; functionisEmpty (s : Stack) return Boolean;functionisFull  (s : Stack) return Boolean;procedure push (item : ItemType; s : inout Stack);procedure pop  (s : inout Stack);function  top   (s : Stack) returnItemType;privatetypeStackElementsisarray(1 .. Size) ofItemType;type Stack isrecord        Elements : StackElements;        Top : Natural := 0;end record;endStackPkg;

  11. Body with StackPkg;   -- Make the generic stack package availableprocedureavgsis         -- Create the two stack packages that are neededpackageintstkpkgisnewstackpkg(1000, integer);packagefltstkpkgisnewstackpkg(1000, float);   -- package FltStkPkg is new StackPkg(size => 1000, ItemType=> Float);-- A use statement will simplify access to the package members   use IntStkPkg;   use FltStkPkg;   Value           : Integer;   Count, Sum      : Integer := 0;Running_Average : Float;-- Declare the two stack variablesIntStack : IntStkPkg.Stack;  -- Must include package name -- here so that theFltStack : FltStkPkg.Stack;  -- compiler can tell which stack -- is needed

  12. Body(2) beginwhilenotend_of_fileloop      get(Value);      Sum := Sum + Value;      Count := Count + 1;Running_Average := float(Sum) / Float(Count);IntStkPkg.push(Value,           IntStack);FltStkPkg.push(Running_Average, FltStack);end loop;whilenotIsEmpty(IntStack) loop      put(top(IntStack));  put(" ");       put(top(FltStack));  new_line;      pop(IntStack);        pop(FltStack);end loop;endavgs; Note:

  13. Procedures generic type ItemType is private; procedure generic_swap(left, right: in out ItemType); -- Implementation of generic procedure swap procedure generic_swap(left, right : in out ItemType) is old_left: constant ItemType := left; begin left := right; right := old_left; end swap; -- Client file that instantiates generic procedure swap procedure swap_int is new generic_swap(ItemType => Integer); i,j: Integer; begin swap_int(i,j);

  14. Notes • Generic packages cannot be used directly • Make a derived type and use it • Syntax for delaying when a type is applied • Consider in the declare block for packages

  15. Child packages • Additions to existing packages • Currently • Add to specification • Add to body • New • Add functions/procedures/types to existing package • What feature is this similar to?

  16. Method • Come up with a name for the child package • New package is parent.child • ada.text_io… • Specification is named • parent-child.ads • Body is named • parent-child.adb

  17. Rationale • Soft grouping • Really, it just adds name. to a package • Adds new capabilities using existing structure • Logical nesting • Helps guide thinking • Sharing of types between parent / children • Name clash avoidance • Stay away from Ada, System, or Interface

  18. Implicit /Explicit • Consider • Which do you consider implicit, explicit? • What does implicit offer over explicit? • What does explicit offer over implicit? System.out.println(p1.distanceToOrigin()); versus put(distanceToOrigin(p1));

  19. Example 2 // Java Pair p = new Pair(1, 2); Pair q = new Pair(3, 4); System.out.println(p.toString()); System.out.println (q.toString()); -- Ada declare Pair q = newPair(1, 2); Pair r = newPair(3, 4); begin put(toString(q)); put(toString(r));

  20. Conflation • Joins together • Java • Data • Methods • Ada • Types • Function / Procedures Rationale for each choice?

  21. Usage scenarios • What is Java best suited for? • What are Java’s weaknesses? • What is Ada best suited for? • What are Ada’s weaknesses?

  22. Implementation • If you had to write a compiler • Would you choose Java or Ada?

  23. Review • Packages

More Related