HOMEWORK PROBLEM 3 INTRODUCTION TO Ada'95 586 PURPOSE: To learn to build your own program library. To write an Ada package. Get an n log n sort for your future applications. Learn more on simple user created types. END RESULT: A complete Ada program that is a main procedure that WITH's a package you write. PROBLEM: Modify the bank account program from Homework 2 to With a package that contains a procedure to sort the account names / balances data. At the end of the run, print out the status of the accounts. First unsorted, then sorted by calling the sort procedure in the package. Add or rearrange data so that unsorted data is not in sorted order by account name. Sample enhanced data is on ECLUS::DISK2:[ADA.ADA95]SORT.DAT TURN IN: Printout of compilation of main procedure and package ( with no errors!) Printout of execution for test data. OBSERVE: A package consists of two parts. The specification part and the body part. The rules of the Ada language allow the body to be changed without impacting any thing else. When the specification is changed ( i.e. recompiled ) then the body must be recompiled and all compilation units that WITH the specification must be recompiled. In general, a package specification can exists without a package body. ( a body must have a spec ) A package specification may contain any or all of the following: Type specifications, object specifications, named numbers, procedure, function and task specifications. NOTE: In order for a type to be known to both a package and a procedure that "with's" the package, the type must be defined in the package specification. This means some of the type declarations in HW2 must be moved from the HW2 procedure into the sort package and deleted from the HW2 procedure. For two objects to be the same type, you must be able to trace both back to the same physical type definition. ( Not a type definition that looks the same !) The package Standard that is automatically "withed" contains the physical type definitions for Integer, Float, String etc. READING: Barnes 9.1, 9.3, 12.1, 12.5, 12.7 ISO 8652:1995 6.1, 6.3, 7.1, 7.2, 8.4 Sample data for homework problem 3, SORT.DAT JOHN DOE OPEN 10.00 opens an account JIM JONES OPEN 37.50 opens an account JOHN DOE DEPOSIT 5.00 deposits $5 JIM JONES WITHDRAW 7.50 withdraws $7.50 JOHN DOE CLOSE 0.00 closes his account MARY SMITH OPEN 20.00 open another account AARON ADD OPEN 99.00 should sort first ZEB ZZEPT OPEN 99.00 should sort last BAD TRANS FOUL 1.00 bad transaction NOT OPEN CLOSE 1.00 bad, not open MARY SMITH OPEN 15.00 no Mary, its open MARY SMITH DEPOSIT -5.00 no Mary, try again MARY SMITH WITHDRAW -5.00 no Mary, positive ! JOHN DOE DEPOSIT 5.00 not open JIM JONES WITHDRAW 99.99 too much, reject JOHN DOE OPEN 99.00 back again, OK EXTRA CREDIT: The passing of SIZE on line 13 of SORT_TEST is not typical Ada style. This line would more commonly read: NNsort ( Data ) ; or possibly NNsort ( Data(2..7) ) ; Similarly the passing of OPEN_ACCOUNTS on line 86 of TEST_SHELLI_SORT can be eliminated by the procedure call: Shelli ( Account_Names(1..Open_Accounts),Balances(1..Open_Accounts)); In this example the call increased in length although the parameters were reduced from three to two. The corresponding procedure definitions would change on line 6 of SORT to: procedure NNsort ( Arr1 : in out Int_Array ) is and on line 47 of Test_Shelli_Sort to: procedure Shelli ( Arr1 : in out A_Names ; Arr2 : in out A_Balances ) is The Ada language feature that makes this style preferred is the attributes that are defined for arrays. Arr1'First is the first, lowest, subscript of ARR1 Arr1'Last is the last, largest, subscript of ARR1 Arr1'Range is a short hand for the range ARR1'FIRST..ARR1'LAST Arr1'Length is the number of values in the ARR1 For extra credit, modify SHELLI to have two parameters and use attributes as appropriate in the procedure body. Array attributes will be covered again in a later lecture.