This exercise is based on the Directions exercise from the Records
labs. In this version, however,
Ext_Angle is a private type.
In the implementation of the
Test_Directions procedure below, the
Ada developer tried to initialize
All_Directions — an array
Ext_Angle type — with aggregates. Since we now have a
private type, the compiler complains about this initialization. Your job
for this exercise is to fix the compilation error by using the appropriate
API function from the
Note: the initialization of
All_Directions in the code below
contains a consistency error where the angle doesn't match the assessed
direction. See if you can spot this error! This kind of errors can happen
when record components that have correlated information are initialized
individually without consistency checks — using private types helps
avoiding the problem.
In this exercise, we'll use
limited private types. The code below
Lim_String type (in the
specification), which acts as a container for strings. In the background
(i.e. in the private part), it's actually declared as an access type to a
Lim_String type is initialized with
functions — either by taking another string, or by indicating the
number of characters for a string container — and displayed with
As we've discussed in the course, variables of limited types cannot be
assigned to. Also, they don't have an equality operator (
can, however, define our own, custom subprograms to circumvent this
- In order to copy instances of a limited type, we can define a custom
- In order to compare instances of a limited type, we can define an
Your job for this exercise is to complete the implementation of
= in the package body of
These are further requirements for the implementation:
For both subprograms, the two parameters may refer to strings with different lengths. We'll limit the implementation to just take the minimum length:
- In case of copying the string "Hello World" to a string with 5 characters, the copied string is "Hello":S1 : constant Lim_String := Init ("Hello World"); S2 : Lim_String := Init (5); begin Copy (From => S1, To => S2); Put_Line (S2); -- This displays "Hello".
- When comparing "Hello World" to "Hello", the
=operator indicates that these strings are equivalent:S1 : constant Lim_String := Init ("Hello World"); S2 : constant Lim_String := Init ("Hello"); begin if S1 = S2 then -- True => This branch gets selected.
- You can use the
Min_Lastconstant — which is already declared in the implementation of these subprograms — in the code you write.
When copying from a short string to a longer string, the remaining characters of the longer string must be initialized with underscores (
_). For example:S1 : constant Lim_String := Init ("Hello"); S2 : Lim_String := Init (10); begin Copy (From => S1, To => S2); Put_Line (S2); -- This displays "Hello_____".
Note: the design and implementation of the
package is very simplistic. A good design would have better handling of
access types, for example.
In previous labs, we had many source-code snippets containing records that
could be declared private. The source-code for the exercise above
(Directions) is an example: we've modified the type declaration of
Ext_Angle, so that the record is now private. Encapsulating the
record components — by declaring record components in the private
part — makes the code safer. Also, because many of the code snippets
weren't making use of record components directly (but handling record
types via the API instead), they continue to work fine after these
This exercise doesn't contain any source-code. In fact, the goal here is to modify previous labs, so that the record declarations are made private. You can look into those labs, modify the type declarations, and recompile the code. The corresponding test-cases must still pass.
In case you don't have a working version of the source-code of previous labs, you can look into the corresponding solutions.
This is a list of selected labs that you can work on, including changes that you can make:
Exercise: List of Names:
People_Arraytype declaration to private part.
More About Types labs:
Exercise: Price List: