From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: ** X-Spam-Status: No, score=2.4 required=5.0 tests=BAYES_50,INVALID_MSGID, SYSADMIN autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ed6f612e60cd2790 X-Google-Attributes: gid103376,public From: mfb@mbunix.mitre.org (Michael F Brenner) Subject: Re: Ada Student Needs Help.. Date: 1997/01/14 Message-ID: <5bg4un$bct@top.mitre.org>#1/1 X-Deja-AN: 209734539 references: <5bdl4i$ffl@saturn.brighton.ac.uk> <1997Jan13.130528.1@eisner> organization: The MITRE Corporation, Bedford Mass. newsgroups: comp.lang.ada Date: 1997-01-14T00:00:00+00:00 List-Id: Yes, Rage Matrix, you can use record types. We presume that you are spelling Berverage as you did to avoid speculation that you or your instructor partake or preach partakement in Beverages. In setting up the objects and methods for your program architecture, you will probably decide to have an object called a Drink which sends messages to objects called Berverages. The messages ask questions like , , and . From the responses to these messages you could compute the number of 10 ml doses of alcohol the imbiber injests. So far, no record types or arrays at all. However, INSIDE the methods that ask the questions of the Berverage objects, there will be some kind of data structure which stores the table of strengths and volumes. This table can be done in several different ways, and you will have to make two choices. First, the abstract data type to use for the Berverages must be selected, for example, a record type, an array, a character string, a linked list, a double linked list, a heap, a balanced or semi-balanced tree, a graph, an unordered set of tuples or associative array, a relational database, a sequential disk file, a random disk file, a human-in-the-loop help desk, a CGI Net search, a data mining encyclopedia CD-ROM search application, an electronic interface to a chemical measuring tool, a punched card or paper tape reader, an indexed sequential floppy dataset, a multiplicative group based on a numerical generating function, a polynomial approximation, a dynamic system (with or without chaos), a voice recognition circuit that rings the 900 hundred phone number for Bartenders anonymous, or an interface to your robot who knocks up the local pub and gets advice. Second, the abstract data type to use for the table itself must be selected, from the list above or others. Actually, there is a data type called a two-dimensional array which would be a single data structure that takes the place of the individual data types for the Berverages and the Table above. Next, you code the test programs to exercise these data structures completely. Finally, you fill in the code for the methods used by the ADTs you have chosen. The best way to do this is to reuse code from existing libraries, for example, the PAL. That way you dont actually have to program any arrays or record types at all. Programming data types is the most fruitful source of bugs in languages like Ada where you dont use pointers or go_tos. If you reuse every data structure, you can just do applications level programming from your operating environment. Another way of reusing code is dynamically loaded through your operating environment, for example, dynamically load the table type through CORBA from another computer that happens to have the volumes and strengths already loaded, such as the UBREW brewery in Ottawa, Ontario, Canada that makes this beer and wine information available through the Algae Gateway at the Net address http://www.synapse.net/~fti/al.htm. If you absolutely refuse to reuse the code for your abstract data structures, then you could read a good book on how to do arrays, such as Edsger Dijkstra, Discipline of Programming. An example of the code for implementing an array reference to get the number of millilitres in each container of Berverage is as follows: type container_volumes is array (berverages) of ounces; container_volume: constant container_volumes := (8, 16, 20); function volume (berverage: berverages) return millilitres is -- precondition: The container volume array has been -- initialized with an entry for berverage, -- which, when multiplied by the number of -- millilitres_per_ounce, will not overflow -- millilitres'last. container: constant ounces := container_volume (berverage); berverage_volume: constant millilitres := millilitres_per_ounce * container; begin -- invariant: If the program counter has arrived at this point -- in the program, then the berverage_volume is the number of -- millilitres in one container of the requested berverage. return berverage_volume; end volume; Coding arrays is like learning algebra, it requires lots of practice to get the preconditions and invariants correctly, and in some cases, to ensure termination of the loops. I recommend a lot of practice, and a lot of tolerance for an educational system that does not teach programming by giving you lots of examples of working code, and methods of modifying that code to do what you want. Attempting to program an array from scratch is bound to result in many frustrating false starts.