Modular Programming
Months
Goal: create a package to display the months of the year.
Steps:
Convert the
Months
procedure below to a package.Create the specification and body of the
Months
package.
Requirements:
Months
must contain the declaration of strings for each month of the year, which are stored in three-character constants based on the month's name.
For example, the string
"January"
is stored in the constantJan
. These strings are then used by theDisplay_Months
procedure, which is also part of theMonths
package.
Remarks:
The goal of this exercise is to create the
Months
package.
In the code below,
Months
is declared as a procedure.
Therefore, we need to convert it into a real package.
You have to modify the procedure declaration and implementation in the code below, so that it becomes a package specification and a package body.
-- Create specification for Months package, which includes -- the declaration of the Display_Months procedure. -- procedure Months;-- Create body of Months package, which includes -- the implementation of the Display_Months procedure. -- procedure Months is procedure Display_Months is begin Put_Line ("Months:"); Put_Line ("- " & Jan); Put_Line ("- " & Feb); Put_Line ("- " & Mar); Put_Line ("- " & Apr); Put_Line ("- " & May); Put_Line ("- " & Jun); Put_Line ("- " & Jul); Put_Line ("- " & Aug); Put_Line ("- " & Sep); Put_Line ("- " & Oct); Put_Line ("- " & Nov); Put_Line ("- " & Dec); end Display_Months; begin null; end Months;with Ada.Command_Line; use Ada.Command_Line; with Ada.Text_IO; use Ada.Text_IO; with Months; use Months; procedure Main is type Test_Case_Index is (Months_Chk); procedure Check (TC : Test_Case_Index) is begin case TC is when Months_Chk => Display_Months; end case; end Check; begin if Argument_Count < 1 then Put_Line ("ERROR: missing arguments! Exiting..."); return; elsif Argument_Count > 1 then Put_Line ("Ignoring additional arguments..."); end if; Check (Test_Case_Index'Value (Argument (1))); end Main;
Operations
Goal: create a package to perform basic mathematical operations.
Steps:
Implement the
Operations
package.
Declare and implement the
Add
function.Declare and implement the
Subtract
function.Declare and implement the
Multiply
: function.Declare and implement the
Divide
function.Implement the
Operations.Test
package
Declare and implement the
Display
procedure.
Requirements:
Package
Operations
contains functions for each of the four basic mathematical operations for parameters ofInteger
type:
Function
Add
performs the addition ofA
andB
and returns the result;Function
Subtract
performs the subtraction ofA
andB
and returns the result;Function
Multiply
performs the multiplication ofA
andB
and returns the result;Function
Divide
performs the division ofA
andB
and returns the result.Package
Operations.Test
contains the test environment:
Procedure
Display
must use the functions from the parent (Operations
) package as indicated by the template in the code below.
package Operations is -- Create specification for Operations package, including the -- declaration of the functions mentioned above. -- end Operations;package body Operations is -- Create body of Operations package. -- end Operations;package Operations.Test is -- Create specification for Operations package, including the -- declaration of the Display procedure: -- -- procedure Display (A, B : Integer); -- end Operations.Test;package body Operations.Test is -- Implement body of Operations.Test package. -- procedure Display (A, B : Integer) is A_Str : constant String := Integer'Image (A); B_Str : constant String := Integer'Image (B); begin Put_Line ("Operations:"); Put_Line (A_Str & " + " & B_Str & " = " & Integer'Image (Add (A, B)) & ","); -- Use the line above as a template and add the rest of the -- implementation for Subtract, Multiply and Divide. end Display; end Operations.Test;with Ada.Command_Line; use Ada.Command_Line; with Ada.Text_IO; use Ada.Text_IO; with Operations; with Operations.Test; use Operations.Test; procedure Main is type Test_Case_Index is (Operations_Chk, Operations_Display_Chk); procedure Check (TC : Test_Case_Index) is begin case TC is when Operations_Chk => Put_Line ("Add (100, 2) = " & Integer'Image (Operations.Add (100, 2))); Put_Line ("Subtract (100, 2) = " & Integer'Image (Operations.Subtract (100, 2))); Put_Line ("Multiply (100, 2) = " & Integer'Image (Operations.Multiply (100, 2))); Put_Line ("Divide (100, 2) = " & Integer'Image (Operations.Divide (100, 2))); when Operations_Display_Chk => Display (10, 5); Display ( 1, 2); end case; end Check; begin if Argument_Count < 1 then Put_Line ("ERROR: missing arguments! Exiting..."); return; elsif Argument_Count > 1 then Put_Line ("Ignoring additional arguments..."); end if; Check (Test_Case_Index'Value (Argument (1))); end Main;