# Contracts¶

## Price Range¶

As discussed in the course, ranges are a form of contract. For example,
the subtype `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 `Price`

(in
the `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 `Right_Triangle`

type.

## Pythagorean Theorem: Precondition¶

In this exercise, you'll work again with the `Right_Triangle`

type.
This time, your job is to write a precondition for the `Init`

function
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
postcondition.

## Pythagorean Theorem: Type Invariant¶

In this exercise, you'll use a private type for the `Right_Triangle`

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.

## Primary Color¶

In this exercise, you'll reuse the code of the `Colors: Lookup-Table`

exercise from the Arrays labs. Just to recapitulate, these were
the hexadecimal values of the colors that we used in the original
exercise:

Color Value Salmon `#FA8072`

Firebrick `#B22222`

Red `#FF0000`

Darkred `#8B0000`

Lime `#00FF00`

Forestgreen `#228B22`

Green `#008000`

Darkgreen `#006400`

Blue `#0000FF`

Mediumblue `#0000CD`

Darkblue `#00008B`

In the code below, the `HTML_Color`

type from the `Color_Types`

package declares these colors, while the `To_RGB_Loopup_Table`

array
implements a lookup-table to convert the colors into a hexadecimal value
for the RGB color components (i.e. `Red`

, `Green`

and
`Blue`

)

In this exercise, your goal is to implement the new `To_Int_Color`

function, which retrieves the hexadecimal value of a RGB color component.
For example, the hexadecimal value of `Salmon`

is `#FA8072`

, where
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
returns `#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 `HTML_RGB_Color`

subtype
using a predicate, so that only RGB colors can be used for selecting the
RGB component in calls to `To_Int_Color`

.