As discussed in the course, ranges are a form of contract. For example,
Price below indicates that a value of this subtype must
always be positive:
subtype Price is Amount range 0.0 .. Amount'Last;
Interestingly, you can also use predicates to specify ranges. In this
exercise, your job is to rewrite the type declaration of
Prices package) using a predicate.
Pythagorean Theorem: Predicate¶
As you probably remember, the Pythagoras' theorem states that the square of the hypotenuse of a right triangle is equal to the sum of the squares of the other two sides.
In this exercise, you'll write a predicate that ensures that the
Pythagorean theorem holds for the
Pythagorean Theorem: Precondition¶
In this exercise, you'll work again with the
This time, your job is to write a precondition for the
that ensures that the Pythagorean theorem holds.
Pythagorean Theorem: Postcondition¶
In the previous exercise, you've written a precondition for the
Init function that ensures that the Pythagorean theorem holds. In
this exercise, your job is to replace that precondition with a
Pythagorean Theorem: Type Invariant¶
In this exercise, you'll use a private type for the
declaration. Now, your job is to write a type invariant for
Right_Triangle to check the Pythagorean theorem.
As a bonus, after completing the exercise, you may analyse the effect that
default values have on type invariants. For example, the declaration of
Right_Triangle uses zero as the default values of the three
triangle lengths. If you replace those default values with
Length'Last, you'll get different results. Make sure you understand
why this is happening.
In this exercise, you'll reuse the code of the
exercise from the Arrays labs. Just to recapitulate, these were
the hexadecimal values of the colors that we used in the original
Color Value Salmon
In the code below, the
HTML_Color type from the
package declares these colors, while the
implements a lookup-table to convert the colors into a hexadecimal value
for the RGB color components (i.e.
In this exercise, your goal is to implement the new
function, which retrieves the hexadecimal value of a RGB color component.
For example, the hexadecimal value of
the first part of this hexadecimal value (
#FA) corresponds to the red
component, the second part (
#80) corresponds to the green component,
and the last part (
#72) corresponds to the blue component of this color.
Therefore, if we call
To_Int_Color (Salmon, Red), the function
#FA, which is the hexadecimal value of the red component of the
Salmon color. This means that the second parameter of
To_Int_Color indicates which RGB component is selected.
Your goal is also to correctly declare the
using a predicate, so that only RGB colors can be used for selecting the
RGB component in calls to