HOMEWORK PROBLEM 4 INTRODUCTION TO Ada I 586.1 PURPOSE: To learn about characters and strings To learn about some conversions To design a small package. Introduction to Object Oriented Programming in Ada Tagged Types and 'Class END RESULT: A complete Ada program that is a main procedure that WITH's a package you write. PROBLEM: Write a program that does a printer plot. A plot of a circle or ellipse with a diagonal line through it is sufficient. Call a procedure in a package you write to generate the points. Put in at least one line of text such as : "THIS IS MY PRINTER PLOT OUTPUT" not using PUT or PUT_LINE. You may use Ada '83 discriminated record or Ada '95 tagged types. TURN IN: Printout of main procedure and package Printout of execution results OBSERVE: Setting up a two dimensional array of characters makes it easy to put in points yet hard to put in text. Setting up a one dimensional array of strings makes it easy to put in text yet the notation to put in points is more difficult. Pick either method. The first example, STRING_ARRAY , that is attached shows a one dimensional array of strings and the subscripting for line number and character position. It also has extra conversion examples . The second example, STRING_PLOT , shows the two dimensional array of characters and use of SIN and COS to compute an integer column and row that draw an ellipse . OTHER: Additional topics include enumeration types and associated attributes. The lecture will also include sample programs for creating, writing and reading of disk files. READING : Barnes Chapters 3, 13 and 19 (Lots of reading) ISO 8652:1995 3.8, 3.9, 3.10, 10.1.1 The concept of a package design is to collect related procedures and functions that work together in one place. In addition to the functions and procedures there are usually type definitions and object definitions. One specific method of designing packages is object oriented design. The ideal implementation of OOD is to completely hide the structure of objects from the user. Only the creation, initialization, use, modification, and disposal are made available to the user. Full OOP would define a figure then use that in defining ellipse and line, etc. One possibility for this homework assignment is to write a package specification and package body that make the following main procedure work. with PRINTER_PLOT ; use PRINTER_PLOT ; procedure MY_PLOT is begin INITIALIZE ; DRAW_ELLIPSE ( X => 0.0 , Y => 0.0 , XR => 23.0 , YR => 9.0 ) ; DRAW_LINE ( X1 => -30.5 , Y1 => -8.1 , X2 => 30.5 , Y2 => 8.1 ) ; WRITE_TEXT ( X => -10.0 , Y => -6.0 , TEXT => "This is just some text" ) ; PRINT ; end MY_PLOT ; The package may raise an exception PLOT_ERROR which the main procedure chooses not to handle. In this way diagnostics will be printed and a walk back generated for bad calls. The writer of MY_PLOT does have the option of catching the exception by including an exception handler. The extension to add to the package should be obvious. Procedures could be added to DRAW_BOX , and other shapes. A procedure could be added that returns the the minimum and maximum X and Y values. Error checking can be done in the package to be sure initialization is performed before anything is drawn. The initialization may be automatic so that the user only calls CLEAR_PLOT when more than one plot is to be performed. The package designer needs to plan for future use. Part of the concept of Ada is to have reusable packages. To make them reusable the package must be almost self documenting, must be user friendly, must be robust and must work. Below is the same program coded in a different style. Readability is in the eyes of the beholder. with PRINTER_PLOT ; procedure MY_PLOT is begin PRINTER_PLOT.INITIALIZE ; PRINTER_PLOT.DRAW_ELIPSE ( 0.0 , 0.0 , 23.0 , 9.0 ) ; PRINTER_PLOT.DRAW_LINE ( -30.5 , -8.1 , 30.5 , 8.1 ) ; PRINTER_PLOT.WRITE_TEXT ( -10.0 , -6.0 , "This is just some text" ); PRINTER_PLOT.PRINT ; end MY_PLOT ; The package body only exposes what the user needs. e.g. package PRINTER_PLOT is -- this is the specification procedure INITIALIZE; procedure ... end PRINTER_PLOT; The package body encapsulates (hides), the data structures (types). e.g. with TEXT_IO; -- do not "with" into spec unless used there package body PRINTER_PLOT is -- this is the implementation type ... -- hidden types PAGE : ... -- hidden data structures procedure INITIALIZE is -- implementation begin ... end INITIALIZE; procedure ... end PRINTER_PLOT;