Copy the file I:\adaclass\sample1.adb to a directory where you plan to do your homework. you may want to first create a directory adaclass or use temp, use this directory for all your homework. Now, test to be sure Ada95 is installed correctly. (if no Ada95 icon, click on program, run, type in adaguide ) Click on the Ada95 icon, click on file, open and move to your directory adaclass, open sample1.adb (It just says sample1). Click run then build. It should show a message indicating completed. Click run then execute. A new window should pop up. Click in the new window, then type 25 followed by an enter. You should see output something like 5 = mod( 25, 10) Type control Z, ^Z, and see the exception message. Kill the window. Ada95 is installed and working. For future hands on work, copy the needed files from I:\adaclass\ Put your name in every file you edit, e.g. -- modified by John Doe 5/29/00
HOMEWORK PROBLEM 1 INTRODUCTION TO Ada'95 PURPOSE: To gain experience with IF-THEN-ELSE Ada structures Introduction to Text_IO Get, Put, Put_Line Single procedure main program structure Handling Text_IO exceptions Learning to compile and run Ada programs END RESULT: A complete Ada program that is a single procedure that compiles and executes with no errors PROBLEM: Use the skeleton structure from the attached change.adb or change2.adb and add your code to compute change for an Amount between 1 and 99 cents. Print number of quarters, dimes, nickels, and pennies with reasonable structure. Zero counts for coins are not to be printed out. Use an exception handler to catch bad input. TURN IN: Printout of file ( As run with no errors!) Printout of execution for test data (in order) 99 94 25 10 5 1 -5 100 ABC 1or use change.dat . OBSERVE: The input to Ada programs uses the operating system terminal handler. Thus nothing might happen until a is typed ( this allows for input line editing ). METHOD: In learning a new programming language it is most efficient to do it interactively. Before trying the first homework it is recommended that you type in the sample program that follows. Try the emacs editor or other editor of your choice. Compile interactively if your system provides the capability. Use the debugger only if desperate, usually not needed with Ada. Then set up .BAT or 'make' for the "production" runs. This will get the mechanics of the procedures established for the rest of the course. EXTRA CREDIT: Do not change 1..99 to 0..99 or Integer or natural for any variable. In other words never try to store a zero. You may eliminate the Variables Quarters, Dimes, Nickels and Pennies if you do not need them. You can add a Constraint_Error exception handler if you wish. READING: BARNES 5.2, 7.1, 14.1 ISO 8652:1995 2.2, 5.3, 11.2 COMMANDS: DOS, Windows 95, 98, NT, 2000 MSDOS window or Unix gcc -c change.adb gnatbl change.ali change < change.dat change < change.dat > change.txt gnatchop change.ada writes file change.adb gnatmake change.adb does both gcc -c and gnatbl adaguide provides an interactive Ada environment FILES: change.adb change2.adb no "with" change.dat
HOMEWORK PROBLEM 2 INTRODUCTION TO Ada'95 PURPOSE: To gain experience with array structures More on Text_IO Get, Put, on various types Internal procedures in a main program structure Handling characters and enumeration types END RESULT: A complete Ada program that is a single procedure with two internal procedures that compiles and executes with no errors PROBLEM: Write an Ada program to simulate a simple bank. You may use the skeleton program bank.adb that is attached or do your own from scratch. The skeleton program may be copied using copy I:\adaclass\bank*.* Each input line contains a simple bank activity: 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 At the end of the run, print out the status of the accounts. TURN IN: Printout of source code (No compilation errors) Printout of execution for test data (in order) of the data listed above. OBSERVE: Character strings require exact match. This means quoted character constants need trailing blanks. Input data using Get on character strings also require trailing blanks (exact count!). Operating systems provide line editing, thus a carriage return is needed to commit the input. Input can be for one or more GET procedure calls. The physical location of an internal procedure or function is critical. They must be after all declarations and before the 'begin'. EXTRA CREDIT Give error message if opening an open account if closing a non open account if overdrawing, negative amounts, etc. Physically delete closed accounts to be ready for future homework assignments READING: Barnes 8.1 8.2 8.3 8.4 ISO 8652:1995 3.6 The following commands compile, link and run Homework 2 on Unix, W95, NT gcc -c bank.adb gnatbl bank.ali bank < bank.dat > bank.txt print bank.adb or may need copy bank.adb prn print bank.txt or print from an editor (If you had an Ada'83 .ada file, first do gnatchop xxx.ada, then use gnat on the resulting .adb and .ads files. Don't just rename file) The input data is (bank.dat): JOHN DOE OPEN 10.00 opens an account JIM JONES OPEN 37.50 opens an account JOHN DOE DEPOSIT 5.00 deposites $5 JIM JONES WITHDRAW 7.50 withdraws $7.50 JOHN DOE CLOSE 0.00 closes his account, takes out all his money BAD TRANS FOUL 1.00 bad transaction NOT OPEN CLOSE 1.00 bad, not open JIM JONES OPEN 15.00 no Jim, its open JIM JONES DEPOSIT -5.00 no Jim, try again, must be positive JIM JONES WITHDRAW -5.00 no Jim, positive ! JOHN DOE DEPOSITE 5.00 not open, forget it JIM JONES WITHDRAW 99.99 too much, reject Results of program as given to you are (bank.out): ENTER Transaction. Name,Transaction,Amount Name= JOHN DOE Transaction OPEN Amount = 1.00000E+01 Open ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction OPEN Amount = 3.75000E+01 Open ENTER Transaction. Name,Transaction,Amount Name= JOHN DOE Transaction DEPOSIT Amount = 5.00000E+00 Deposit ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction WITHDRAW Amount = 7.50000E+00 Withdraw ENTER Transaction. Name,Transaction,Amount Name= JOHN DOE Transaction CLOSE Amount = 0.00000E+00 Close ENTER Transaction. Name,Transaction,Amount Name= BAD TRANS BAD INPUT DATA ENTER Transaction. Name,Transaction,Amount Name= NOT OPEN Transaction CLOSE Amount = 1.00000E+00 Close ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction OPEN Amount = 1.50000E+01 Open Account Already Open ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction DEPOSIT Amount =-5.00000E+00 Deposit ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction WITHDRAW Amount =-5.00000E+00 Withdraw ENTER Transaction. Name,Transaction,Amount Name= JOHN DOE BAD INPUT DATA ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction WITHDRAW Amount = 9.99900E+01 Withdraw ENTER Transaction. Name,Transaction,Amount end of input data Bank Is Closed. FILES: bank.adb banknu.adb bank.dat bank.out
HOMEWORK PROBLEM 3 INTRODUCTION TO Ada'95 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 I:\adaclass\SORT.DAT TURN IN: Printout 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. FILES: your homework 2 sort.dat sort.ada sort_test.ada shellitst.out demo of sort shellitst.ada Optional shellitst.for shellitst.f90 shellitst.c shellitst.cc shellitst.bas
HOMEWORK PROBLEM 4 INTRODUCTION TO Ada 95 PURPOSE: To learn about matrices, overloaded operators, defining and using exceptions. To specify types and exceptions in a utility library package END RESULT: A complete Ada program that is a main procedure that WITH's a package you modify. PROBLEM: Modify the attached package Real_Matrix_Arithmetic , a skeleton for a complete real matrix arithmetic package. Use the matrix multiply "*" as a guide to write matrix add "+". Keep the matrix multiply and use both in the main procedure. The only requirement for a matrix add to be legal is that A'Length(1) = B'Length(1) and A'Length(2) = B'Length(2). TURN IN: Printout of main procedure and package. Printout of execution for test data. ( modify to suit your needs. Matrices as small as 3 by 2 are acceptable , not square matrices) OBSERVE: The type Real_Matrix is defined in the package and is visible in the main procedure because of the "with" and "use" on Real_Matrix_Arithmetic. The objects MAT_1 etc. are declared and assigned storage in the main procedure. The object C is dynamically assigned storage with a size that depends on the formal parameter A when "*" is called. The user defined exception Matrix_Error is defined and may be raised in the package. This exception may then be handled as the user desires in the main procedure. This avoids the need for math routines to print out errors and allows the calling procedure to take corrective action for some types of errors. Be sure to handle first and second subscript ranges having different first subscripts. Only the length of the side of the matrix is important. EXTRA Add tests to make sure all cases work. READING: BARNES 9.1-9.6 A1.2 ISO 8652:1995 6.1 6.2 6.3 6.4 Annex K LECTURE: will also cover attributes in Annex K (follow AdaGide help to Annex K) The following are some convenient packages to have compiled. Int_IO.ads with Ada.Text_IO ; package Int_IO is new Ada.Text_IO.Integer_IO ( Integer ) ; NOTE: compiling the above package may allow faster compilation of later units. Replace "package Int_IO is new Integer_IO ( Integer ) ; use Int_IO ;" with "with Int_IO ; use Int_IO ;" before the package or procedure Define_Real.ads -- This package provides the definition of the type Real and Long_Real. -- For computers with only one floating point hardware both subtypes -- become the same. package Define_Real is subtype Real is Float ; -- may be Short_Float subtype Long_Real is Long_Float ; -- may be Float end Define_Real ; Real_IO.ads with Define_Real ; with Ada.Text_IO ; package Real_IO is new Ada.Text_IO.Float_IO ( Define_Real.Real ) ; Note: As projects get larger, there is more importance on not changing package specifications. This has two ramifications. First put the package specification in one file and the package body in another. (.ads and .adb for gnat) Second, only put "with" statements on the specification that are needed by the specification. The body typically needs more "with" statements. This speeds up compilations and reduces dependencies. More stray information : The VAX VMS naming convention for files containing package specifications is to add an underscore to the package name in order to get the file name. The package body file name is the same as the package name. The gnat naming convention is to use the package name with extensions .ads for specifications and .adb for bodies. Verdix now part of Rational uses xxx.a and xxx_b.a Remember to use gnatchop on starter files named *.ada FILES: matdemo.ada has it all int_io.ads define_real.ads real_io.ads real_matrix_arithmetic.ads real_matrix_arithmetic.adb matrix_demo.adb Optional seq_io_demo.adb direct_io_demo.ada direct_io_demo_nouse.ada
HOMEWORK PROBLEM 5 INTRODUCTION TO Ada 95 PURPOSE: To gain experience in Ada strong typing. This problem will use private types, derived types, subtypes, type conversion, real types and qualified expressions. END RESULT: A set of packages that allow the attached procedure to compile and execute correctly. PROBLEM: You are to provide a set of packages that physics students are to use in programming their homework. The problems are limited to distance, time, velocity and acceleration. The student must not be allowed to write code that violates dimensional analysis of physics. Still the student should be able to use square root and trig functions as well as non dimensional quantities. The only units that need to be considered are meter, second, meter per second and meter per second squared. Information supplied in the handout includes: dimensional analysis, definition of MKS and English systems of units, physical constants and equations of physics. It adds too much complexity to be able to handle all equations in any system of units. Conversion to base units and back is the most practical way to solve the general problem. Get the file PHYSICS.ADA and use gnatchop on it. These do not have to be used. You can solve the problem by any method you choose. You will be making small changes in a number of files. TURN IN: Printout of source program and execution output. OBSERVE: The distinction between type conversion and qualified expression. The uses of derived types and subtypes. At least some of the packages would be generic in their final versions. Note that many Ada papers and text books show a lack of understanding for dimensions and units as related to Ada's strong typing capability. METHOD: Use the starter set of files or develop your own code. READING: BARNES 6.3, 6.4, 11.2, 11.3 11.4, IS0 8652:1995 3.2, 3.4, 3.5.6, 3.5.7, 4.6, 4.7 Units definitions FILES: physics.ada has it all hw5.adb
HOMEWORK PROBLEM 9 INTRODUCTION TO Ada 95 PURPOSE: To learn more about Ada 95 object oriented programming and how to use it, some uses of child packages, class wide types, and polymorphism. END RESULT: A complete Ada program that is a main procedure that WITH's a package you write containing more types of figures. PROBLEM: Write a package that has a shape derived from the Figure type. Do minimum editing to a copy of the Rectangle_Pkg to make a Diamond_Pkg and define a Diamond type. Add corresponding tests in the main procedure FigureC to test your Diamond type. NOTE: A diamond just has a height and width like a rectangle. This is meant to be easy. The extra credit is much harder. EXTRA CREDIT: Make a child package of the child package Figure_Pkg.Circle_Pkg for an ellipse. The package would be called Figure_Pkg.Circle_Pkg.Ellipse_Pkg . Make the type Ellipse from the type Circle by adding one item to the record called Y_Radius. Add the corresponding tests to FigureC for your new type Ellipse OBSERVE: Watch the naming conventions. They get long. READING: BARNES 12.3 13.1 .. 13.5 ISO 8652:1995 3.9, 3.10, 10. FILES: figurec.ada figurec.bat figure_tagged.ada figure_tagged.bat
HOMEWORK PROBLEM 10 INTRODUCTION TO Ada 95 PURPOSE: To learn about access types Binary trees are used because they rather naturally dictate the use of access types. END RESULT: A complete Ada program that is a main procedure that WITH's a package containing the binary tree manipulation routines INSERT, FIND, DELETE and OUTPUT. Use OUTPUT as a debugging aid as each feature is added. PROBLEM: Use the attached samples as a guide to write a package that has binary tree manipulation routines. You must add a procedure DELETE that takes a name and deletes the names node from the tree. You can build the same tree. To show that the cases work delete ( in order ) "E " , "B " , and "D " . Make provision for doing nothing to the tree if the name is not in the tree. Output the tree after every delete. TURN IN: Printout main procedure and package. Printout of execution for built in test data. OBSERVE: These tree manipulation routines are messy if recursive procedures and functions are not used. These happen to be in the class of "simple" recursive routines that can be thoroughly tested to be sure they terminate. READING : ISO 8652:1995 3.10 plus use the index for all references to "access" Barnes 10.1, 10.2, 10.4 EXTRA : For extra credit make the INSERT and DELETE work to maintain a balanced binary tree. This uses the balance factor BF. The INSERT is not too difficult but the DELETE is a bear! It turns out that just plain binary trees are very inefficient. But, balanced binary trees are very efficient. If the tree is maintained in balanced form at most O(log2 N) searches are needed for an INSERT, FIND or DELETE. On a Baltimore phone book N is a large number! An unbalanced tree could be the worse possible structure if the names were entered alphabetically. Zzepth could be all day looking for his phone number. FILES: hw10.ada hw10.bat Optional taccess1.adb taccess2.adb taccess3.adb taccess4.adb taccess5.adb taccess6.adb
Last updated 5/29/00