Presentation is loading. Please wait.

Presentation is loading. Please wait.

Invitation to Computer Science 5 th Edition Chapter Ada Programming in Ada.

Similar presentations


Presentation on theme: "Invitation to Computer Science 5 th Edition Chapter Ada Programming in Ada."— Presentation transcript:

1 Invitation to Computer Science 5 th Edition Chapter Ada Programming in Ada

2 Invitation to Computer Science, 5th Edition2 Objectives In this chapter, you will learn about: Creating and running a simple program in Ada Virtual data storage Statement types An example of an Ada program

3 Invitation to Computer Science, 5th Edition3 Objectives (continued) Managing complexity Object-oriented programming Graphical programming

4 Invitation to Computer Science, 5th Edition44 Introduction to Ada Ada language –Developed by the United States Department of Defense in the 1980s and upgraded to include object- oriented capabilities in the mid-1990s In Ada –Keywords are used as delimiters Example: BEGIN... END

5 Invitation to Computer Science, 5th Edition55 A Simple Ada Program Comments –Anything appearing on a line after the double dash (--) –Ignored by the compiler Prologue comment –Introductory comment Blank lines in Ada programs –Ignored and used like comments

6 Invitation to Computer Science, 5th Edition6 Figure 1 A Simple Ada Program

7 Invitation to Computer Science, 5th Edition7 Figure 1 A Simple Ada Program (continued)

8 Invitation to Computer Science, 5th Edition8 Figure 2 The Overall Form of a Typical Ada Package Body Program

9 Invitation to Computer Science, 5th Edition9 Figure 3 The Program of Figure 1 (line numbers added for reference)

10 Invitation to Computer Science, 5th Edition10 A Simple Ada Program (continued) Strongly-typed language –Compiler will not allow you to mix up integers, floating-point numbers, and strings in the same statement Syntax –The correct form for each component of the language Free-format language –It does not matter where things are placed on a line

11 Invitation to Computer Science, 5th Edition11 Creating and Running an Ada Program First step –Type the program into a text editor Second step –The program in the.adb file must be prepared for execution Third step –Operates on the.exe file and loads and executes the program

12 Creating and Running an Ada Program (continued) Integrated Development Environment (IDE) –Lets the programmer perform a number of tasks within the shell of a single application program –Usually has a GUI (graphical user interface) with menu choices for the different task Invitation to Computer Science, 5th Edition12

13 Invitation to Computer Science, 5th Edition13 Virtual Data Storage Identifiers –Names in a programming language –Cannot be one of the few reserved words Constants –Values are fixed and known ahead of time Variables –Values that change as the program executes

14 Virtual Data Storage (continued) Variable declaration –Consists of a list of one or more identifiers of the same data type followed by that data type Array –Groups together a collection of memory locations, all storing data of the same type Invitation to Computer Science, 5th Edition14

15 Invitation to Computer Science, 5th Edition15 Figure 4 Some of the Ada Primitive Data Types

16 Invitation to Computer Science, 5th Edition16 Figure 5 A 12-Element Array hits

17 Invitation to Computer Science, 5th Edition17 Statement Types Input statement –Collects a specific value from the user for a variable within the program Output statement –Writes a message or the value of a program variable to the user’s screen Assignment statement –Assigns a value to a program variable

18 Invitation to Computer Science, 5th Edition18 Statement Types (continued) Control statements –Affect the order in which instructions are executed Flow of control in the program –Path through the program that is traced by following the currently executing statement

19 Invitation to Computer Science, 5th Edition19 Input/Output Statements Literal strings –Enclosed in double quotes –Printed out exactly as is Single Ada statement –Can be spread over multiple lines, but a line break cannot occur in the middle of a literal string & –The Ada concatenation operator

20 Invitation to Computer Science, 5th Edition20 The Assignment Statement Pseudocode operation Set the value of “variable” to “arithmetic expression” Ada equivalent variable := expression Basic arithmetic operations +Addition -Subtraction *Multiplication /Division

21 Invitation to Computer Science, 5th Edition21 Control Statements Control mechanisms –Sequential: instructions are executed in order –Conditional: which instruction executes next depends on some condition –Looping: a group of instructions may be executed many times Boolean condition –Can be either true or false –Often involves comparing the values of two expressions and determining whether they are equal

22 Invitation to Computer Science, 5th Edition22 Figure 6 Sequential Flow of Control

23 Invitation to Computer Science, 5th Edition23 Figure 7 Ada Comparison Operators

24 Invitation to Computer Science, 5th Edition24 Figure 8 Ada Boolean Operators

25 Invitation to Computer Science, 5th Edition25 Figure 9 Conditional Flow of Control (if-else)

26 Invitation to Computer Science, 5th Edition26 Figure 10 If-Else with Empty Else

27 Control Statements (continued) Compound statement –Can be used anywhere a single statement is allowed Initialization of variables –Using assignment statements to set the values of certain variables before they are used by the program Invitation to Computer Science, 5th Edition27

28 Invitation to Computer Science, 5th Edition28 Figure 11 The TravelPlanner Program with a Conditional Statement

29 Invitation to Computer Science, 5th Edition29 Figure 11 The TravelPlanner Program with a Conditional Statement (continued)

30 Invitation to Computer Science, 5th Edition30 Figure 12 While Loop

31 Control Statements (continued) Sentinel value –One extra integer that is not part of the legitimate data but is instead a signal that there are no more data Infinite loop –The condition, once true, would remain true forever, and the loop body would be endlessly executed Invitation to Computer Science, 5th Edition31

32 Invitation to Computer Science, 5th Edition32 Figure 13 The TravelPlanner Program with Looping

33 Invitation to Computer Science, 5th Edition33 Figure 13 The TravelPlanner Program with Looping (continued)

34 Another Example Ada –We can do input and output –We can assign values to variables within the program –We can direct the flow of control by using conditional statements or looping Invitation to Computer Science, 5th Edition34

35 Invitation to Computer Science, 5th Edition35 Figure 14 A Pseudocode Version of the SportsWorld Program

36 Invitation to Computer Science, 5th Edition36 Figure 15 The SportsWorld Program

37 Invitation to Computer Science, 5th Edition37 Figure 15 The SportsWorld Program (continued)

38 Invitation to Computer Science, 5th Edition38 Figure 16 A Sample Session Using the Program of Figure 15

39 Managing Complexity Divide and conquer –Problem-solving approach and not just a computer programming technique Figure 17(a) –An example of a structure chart (structure diagram) Invitation to Computer Science, 5th Edition39

40 Invitation to Computer Science, 5th Edition40 Figure 17 Structure Charts

41 Using Functions/Procedures Functions and procedures –Nested within one another Functions/procedure –Each should do one and only one subtask Invitation to Computer Science, 5th Edition41

42 Invitation to Computer Science, 5th Edition42 Figure 18 Structure Chart for the SportsWorld Task

43 Invitation to Computer Science, 5th Edition43 Figure 19 A High-Level Modular View of the SportsWorld Program

44 Invitation to Computer Science, 5th Edition44 Figure 20 The Main Program Code in a Modularized Version of the SportsWorld Program

45 Writing Functions/Procedures Header consists of four parts –The keyword FUNCTION or PROCEDURE –The function or procedure identifier –A parameter list –A return indicator (for a function) Return indicator –Indicates the data type of the one and only value computed and returned by the function Invitation to Computer Science, 5th Edition45

46 Invitation to Computer Science, 5th Edition46 Figure 21 The Outline for an Ada Function/Procedure

47 Writing Functions/Procedures (continued) Argument is passed by value –If the value is one that the module must know to do its job but that should not change Argument is passed by reference –If value passed to the module is one that the module should change, and the main program code should know the new value Invitation to Computer Science, 5th Edition47

48 Invitation to Computer Science, 5th Edition48 Figure 22 The getInput Procedure

49 Invitation to Computer Science, 5th Edition49 Figure 23 The doCircumference Procedure

50 Invitation to Computer Science, 5th Edition50 Figure 24 The Complete Modularized SportsWorld Program

51 Invitation to Computer Science, 5th Edition51 Figure 24 The Complete Modularized SportsWorld Program (continued)

52 Invitation to Computer Science, 5th Edition52 Figure 24 The Complete Modularized SportsWorld Program (continued)

53 Writing Functions/Procedures (continued) Modularizing a program is useful for: –Planning –Coding –Testing –Modifying –Reading Function –Special type of Ada module that can be written to compute a single value as its subtask Invitation to Computer Science, 5th Edition53

54 Invitation to Computer Science, 5th Edition54 Figure 25 The SportsWorld Program Using Functions

55 Invitation to Computer Science, 5th Edition55 Figure 25 The SportsWorld Program Using Functions (continued)

56 Invitation to Computer Science, 5th Edition56 Figure 26 Some Ada Terminology

57 An Ada Feature: User-Defined Subtypes You can define your own subtypes of the Ada standard data types –Strong typing will apply to these new types as well Strong typing in Ada –Requires that appropriate versions of the I/O package be used for variables declared Invitation to Computer Science, 5th Edition57

58 Invitation to Computer Science, 5th Edition58 Figure 27 The SportsWorld Program with Defined Subtypes

59 Invitation to Computer Science, 5th Edition59 Figure 27 The SportsWorld Program with Defined Subtypes (continued)

60 Object-Oriented Programming A program is considered a simulation of some part of the world that is the domain of interest –“Objects” populate this domain Terms associated with object-oriented programming –Encapsulation –Inheritance –Polymorphism Invitation to Computer Science, 5th Edition60

61 Invitation to Computer Science, 5th Edition61 Figure 28 Three Key Elements of OOP

62 Ada and OOP In February 1995 –Ada 95 became the first internationally standardized object-oriented programming language –New standard, officially ISO/IEC 8652:1995, added many new and important features to the language Invitation to Computer Science, 5th Edition62

63 Invitation to Computer Science, 5th Edition63 Figure 29 Object-Oriented Terminology and Usage: Standard and Ada

64 Invitation to Computer Science, 5th Edition64 Figure 30 An Object-Oriented SportsWorld Program

65 Invitation to Computer Science, 5th Edition65 Figure 30 An Object- Oriented SportsWorld Program (continued)

66 Invitation to Computer Science, 5th Edition66 Figure 30 An Object-Oriented SportsWorld Program (continued)

67 One More Example In Figure 31 –The CIRCLE object has a radius property –The RECTANGLE object has a width attribute and a height attribute –Any CIRCLE object can set the value of its radius and can compute its area –A RECTANGLE object can set the value of its width and height and can compute its area –Both SQUARE and SQUARE2 objects have a side property that they can set Invitation to Computer Science, 5th Edition67

68 Invitation to Computer Science, 5th Edition68 Figure 31 An Ada Program with Polymorphism and Inheritance

69 Invitation to Computer Science, 5th Edition69 Figure 31 An Ada Program with Polymorphism and Inheritance (continued)

70 Invitation to Computer Science, 5th Edition70 Figure 31 An Ada Program with Polymorphism and Inheritance (continued)

71 Invitation to Computer Science, 5th Edition71 Figure 31 An Ada Program with Polymorphism and Inheritance (continued)

72 Invitation to Computer Science, 5th Edition72 Figure 32 Output from the Program of Figure 31

73 What Have We Gained? Reasons why OOP is a popular way to program –Software reuse –A more natural “worldview” Software reuse –Useful class that has been implemented and tested becomes a component available for use in future software development Invitation to Computer Science, 5th Edition73

74 A More “Natural” Worldview Object-oriented programming –Recognizes that in the “real world,” tasks are done by entities (objects) –Allows the programmer to come closer to modeling or simulating the world as we see it Invitation to Computer Science, 5th Edition74

75 Graphical Programming Graphics –Make it easier to manage tasks of the operating system –Can help us visualize and make sense of massive amounts of output produced by programs that model complex physical, social, and mathematical systems Invitation to Computer Science, 5th Edition75

76 Graphics Hardware Bitmapped display –Screen is made up of thousands of individual picture elements, or pixels, laid out in a two-dimensional grid High-resolution terminals –Terminals with a high density of pixels Frame buffer –Memory that stores the actual screen image Invitation to Computer Science, 5th Edition76

77 Invitation to Computer Science, 5th Edition77 Figure 34 Pixel-Numbering System in a Bitmapped Display

78 Invitation to Computer Science, 5th Edition78 Figure 35 Display of Information on the Terminal

79 Graphics Software Graphics library –Collection of procedures that are part of a special package The following graphics procedures are at our disposal Draw_Line (x1, y1, x2, y2, Color) Draw_Box (x1, y1, x2, y2, Color, Filled) Draw_Circle (x, y, Radius, Color, Filled) Display_Text (x, y, string, Color) Invitation to Computer Science, 5th Edition79

80 Summary In a high-level language, the programmer: –Need not manage the storage or movement of data values in memory –Can think about the problem at a higher level –Can use program instructions that are both more powerful and in a more natural language –Can write a program that is much more portable among various hardware platforms Invitation to Computer Science, 5th Edition80

81 Summary (continued) Modularization –Allows the program to be more cleanly structured Object-oriented programming –Allows a more intuitive view of the problem solution and provides the possibility for reuse of helpful types Invitation to Computer Science, 5th Edition81


Download ppt "Invitation to Computer Science 5 th Edition Chapter Ada Programming in Ada."

Similar presentations


Ads by Google