Download Package Specification Listing-Computer Programming-Assignment Solution and more Exercises Aeronautical Engineering in PDF only on Docsity!
C-10 1.
For X = 3
Case 1.
IF x >= 0 THEN x:= x+1; ELSIF x >=1 THEN x := x + 2; END IF ;
In the case above, only the x:= x+1 statement is executed and the result is 4;
Case 2.
IF x >= 0 THEN x := x + 1; END IF ; IF x >= 1 THEN x := x + 2; END IF ;
In this case, both the x:=x+1 and x:= x+2 statements will be executed and the result is 6.
C-10 2.
Package Specification Listing
Checking: c:/docume~2/jk/desktop/16070/codeso~1/my_math_package.ads (source file time stamp: 2003- 09-24 03:27:46)
- -- Package specified to implement two arithmetic functions
- -- Specifier : Jayakanth Srinivasan
- -- Date Last Modified : 09/23/
- package My_Math_Package is
- subtype Menu_Choice is Integer range 1 .. 3;
- procedure Menu (
- My_Menu_Choice : out Menu_Choice );
- function Add (
- X : Float;
- Y : Float )
- return Float;
- function Multiply (
- X : Integer;
- Y : Integer )
- return Integer;
- end My_Math_Package;
23 lines: No errors
Package Code Listing
Compiling: c:/docume~2/jk/desktop/16070/codeso~1/my_math_package.adb (source file time stamp: 2003- 09-24 03:27:46)
. ----------------------------------------------------------- . -- Package implementation of My_Math package . -- Implementer : Jayakanth Srinivasan . -- Date Last Modified : 09/23/ . ----------------------------------------------------------- . with Ada.Integer_Text_Io; . with Ada.Text_Io; . with Ada.Float_Text_Io; |
C-10 3.
Algorithm
1. Display the menu to the user
2. Get the menu choice from the user
3. If Choice is 1 then
a. Prompt the user for two floating point numbers
b. Clear the screen
c. Compute the sum using the math package
d. Display the answer in the required format.
4. If Choice is 2 then
a. Prompt the user for two integer numbers
b. Clear the screen
c. Compute the product using the math package
d. Display the answer in the required format.
5. If Choice is 3 then
a. Exit the program
Code Listing
Compiling: c:/docume~2/jk/desktop/16070/codeso~1/test_math.adb (source file time stamp: 2003-09- 03:47:18)
- -- Program to implement a menu driven program using the
- -- the math package
- -- Programmer : Jayakanth Srinivasan
- -- Date Last Modified : 09/23/
- with My_Math_Package;
- with Ada.Text_Io;
- with Ada.Float_Text_Io;
- with Ada.Integer_Text_Io;
- with Screen;
- procedure Test_Math is
- Choice : My_Math_Package.Menu_Choice;
- X,
- Y : Integer;
- Number_X,
- Number_Y : Float;
- begin
- loop
- -- obtain the choice from the user
- My_Math_Package.Menu(Choice);
- -- exit if the user chooses 3
- exit when Choice = 3;
- case Choice is
- when 1 =>
- -- obtain two floating point numbers
- Ada.Text_Io.Put ("Please Enter the Value of X : ");
- Ada.Float_Text_Io.Get(Number_X);
- Ada.Text_Io.Skip_Line;
- Ada.Text_Io.Put("Please Enter the Value of Y : ");
- Ada.Float_Text_Io.Get(Number_Y);
- Ada.Text_Io.Skip_Line;
- -- clear the screen
- Screen.Clearscreen;
- -- display the results
- Ada.Text_Io.Put("Adding");
- Ada.Float_Text_Io.Put(Number_X);
- Ada.Text_Io.Put("and");
- Ada.Float_Text_Io.Put(Number_Y);
- Ada.Text_Io.Put(":");
- Ada.Text_Io.New_Line;
- Ada.Float_Text_Io.Put(Number_X);
- Ada.Text_Io.Put("+");
- Ada.Float_Text_Io.Put(Number_Y);
- Ada.Text_Io.Put("=");
- Ada.Float_Text_Io.Put(My_Math_Package.Add(Number_X, Number_Y));
- Ada.Text_Io.New_Line;
- when 2=>
- -- obtain two integers
- Ada.Text_Io.Put ("Please Enter the Value of X : ");
- Ada.Integer_Text_Io.Get(X);
- Ada.Text_Io.Skip_Line;
- Ada.Text_Io.Put("Please Enter the Value of Y : ");
- Ada.Integer_Text_Io.Get(Y);
- Ada.Text_Io.Skip_Line;
- -- clear the screen
- Screen.Clearscreen;
- -- display the product
- Ada.Text_Io.Put("Multiplying");
- Ada.Integer_Text_Io.Put(X);
- Ada.Text_Io.Put("and");
- Ada.Integer_Text_Io.Put(Y);
- Ada.Text_Io.Put(":");
- Ada.Text_Io.New_Line;
- Ada.Integer_Text_Io.Put(X);
- Ada.Text_Io.Put("*");
- Ada.Integer_Text_Io.Put(Y);
- Ada.Text_Io.Put("=");
- Ada.Integer_Text_Io.Put(My_Math_Package.Multiply(X, Y));