![Page 1: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/1.jpg)
ITEC 320
Lecture 10Packages (2)
![Page 2: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/2.jpg)
Packages(2)
Review
• Packages–What parts do they have?– Syntax?
![Page 3: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/3.jpg)
Packages(2)
Specification
• Tells the compiler what the package does, but not how to do it
Store in a .ads file
package PairPkg is type Pair is record
x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer;
end PairPkg;
![Page 4: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/4.jpg)
Packages(2)
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;
![Page 5: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/5.jpg)
Packages(2)
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;
![Page 6: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/6.jpg)
Packages(2)
Code
• Input parser• Reads a line• Returns int / String / Float based on
whitespace• Specification• Body• Client What should be written first?
Privacy?
![Page 7: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/7.jpg)
Packages(2)
Comparison
• Java’s Scanner– Similarities– Differences
![Page 8: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/8.jpg)
Packages(2)
Currently
package IntStackPkg is type StackType is limited private; Stack_Is_Empty, Stack_Is_Full : Exception; function IsEmpty (S : StackType) return Boolean; function IsFull (S : StackType) return Boolean; procedure Push (I : Integer; S : in out StackType); procedure Pop (S : in out StackType); function Top (S : StackType) return Integer;
private MaxSize : Constant Integer := 1000; type ElementArray is array(1 .. MaxSize) of Integer; type StackType is record Elements : ElementArray; TheTop : Integer := 0; end record;end IntStackPkg;
What happens when you want to store Strings?
![Page 9: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/9.jpg)
Packages(2)
Generic
• Formal parameters– Specification
• Actual parameters– Client
• Need: get away from specific types
![Page 10: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/10.jpg)
Packages(2)
Genericgeneric -- Generic parameters are declared here Size : Positive; -- Size of stack to create type ItemType is private; -- Type of elements package StackPkg is type Stack is limited private; Stack_Empty: exception; Stack_Full : exception; function isEmpty (s : Stack) return Boolean; function isFull (s : Stack) return Boolean; procedure push (item : ItemType; s : in out Stack); procedure pop (s : in out Stack);
function top (s : Stack) return ItemType;private type StackElements is array(1 .. Size) of ItemType; type Stack is record Elements : StackElements; Top : Natural := 0; end record;end StackPkg;
![Page 11: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/11.jpg)
Packages(2)
Bodywith StackPkg; -- Make the generic stack package availableprocedure avgs is -- Create the two stack packages that are needed package intstkpkg is new stackpkg(1000, integer); package fltstkpkg is new stackpkg(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 variables IntStack : IntStkPkg.Stack; -- Must include package name
-- here so that the FltStack : FltStkPkg.Stack; -- compiler can tell which stack
-- is needed
![Page 12: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/12.jpg)
Packages(2)
Body(2)
begin while not end_of_file loop 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; while not IsEmpty(IntStack) loop put(top(IntStack)); put(" "); put(top(FltStack)); new_line; pop(IntStack); pop(FltStack); end loop;end avgs;
Note:
![Page 13: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/13.jpg)
Packages(2)
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);
![Page 14: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/14.jpg)
Packages(2)
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
![Page 15: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/15.jpg)
Packages(2)
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?
![Page 16: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/16.jpg)
Packages(2)
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
![Page 17: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/17.jpg)
Packages(2)
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
![Page 18: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/18.jpg)
Packages(2)
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));
![Page 19: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/19.jpg)
Packages(2)
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));
![Page 20: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/20.jpg)
Packages(2)
Conflation
• Joins together• Java– Data–Methods
• Ada– Types– Function / Procedures
Rationale foreach choice?
![Page 21: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/21.jpg)
Packages(2)
Usage scenarios
• What is Java best suited for?• What are Java’s weaknesses?• What is Ada best suited for?• What are Ada’s weaknesses?
![Page 22: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/22.jpg)
Packages(2)
Implementation
• If you had to write a compiler• Would you choose Java or Ada?
![Page 23: ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?](https://reader030.vdocuments.us/reader030/viewer/2022032805/56649ee15503460f94bf1e7f/html5/thumbnails/23.jpg)
Packages(2)
Review
• Packages