Download presentation
Presentation is loading. Please wait.
Published byChristopher French Modified over 9 years ago
1
ITEC 320 Lecture 10 Packages (2)
2
Review Packages –What parts do they have? –Syntax?
3
Packages(2) Specificatio n 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;
4
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;
5
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;
6
Packages(2) Code Input parser Reads a line Returns int / String / Float based on whitespace Specification Body Client What should be written first? Privacy?
7
Packages(2) Comparison Java’s Scanner –Similarities –Differences
8
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?
9
Packages(2) Generic Formal parameters –Specification Actual parameters –Client Need: get away from specific types
10
Packages(2) Generic generic -- 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;
11
Packages(2) Body with StackPkg; -- Make the generic stack package available procedure 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
12
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:
13
Packages(2) Procedure s 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
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
15
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?
16
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
17
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
18
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));
19
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));
20
Packages(2) Conflation Joins together Java –Data –Methods Ada –Types –Function / Procedures Rationale for each choice? Rationale for each choice?
21
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?
22
Packages(2) Implementatio n If you had to write a compiler Would you choose Java or Ada?
23
Packages(2) Review Packages
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.