# 1 Abstract Data Types (10 Points)

## 1.

new_cd

### (b)

title, artist, price, quantity

### (c)

set_price, sell, order_new

## 2.

new_cd, set_price, sell, order_new

## 3.

P1 new_cd(t, a, p, q) require p>0 and q>0 ["...a new CD can only be created if there is at least one copy on stock..."]
P2 set_price(c, x) require x > 0
P3 sell(c, x) require quantity(c) >= x [It seems reasonable to add "and x > 0" because of "“sell” reduces the quantity" even though it's not specifically stated elsewhere]
P4 order_new(c,x) require x > 0 and quantity(c)=0 ["..order new copies of a certain CD when there are none left on stock"]

## 4.

t: STRING (Title), a: STRING (Artist), p: INTEGER (Price), q: INTEGER (Quantity)

title(new_cd(t, a, p, q)) = t
artist(new_cd(t, a, p, q)) = a
price(new_cd(t, a, p, q)) = p
quantity(new_cd(t, a, p, q)) = q

title(set_price(c, p)) = title(c)
artist(set_price(c, p)) = artist(c)
price(set_price(c, p)) = p
quantity(set_price(c, p)) = quantity(c)

title(sell(c, q)) = title(c)
artist(sell(c, q)) = artist(c)
price(sell(c, q)) = price(c)
quantity(sell(c, q)) = quantity(c) - q

title(order_new(c, q)) = title(c)
artist(order_new(c, q)) = artist(c)
price(order_new(c, q)) = price(c)
quantity(order_new(c, q)) = q

# 2 Design by Contract (10 Points)

<eiffel> class

``` COUNTER
```

feature increment

``` ensure
item = old item + 1
```

decrement

``` require
item > 0
ensure
item = old item -1
```

invariant

``` item >= 0
```

class

``` STUDENT
```

feature make

``` require
n /= void and then not n.is_empty
ensure
name = n
```

set_name

``` require
n /= void and then not n.is_empty
ensure
name = n
```

invariant

``` n /= void and then not name.is_empty
```

class COURSE

feature make

``` require
n /= void and then not n.is_empty
ensure
name = n and then not n.is_empty and then count_students /= void and then students /= void
```

register

``` require
s /= void
not students.has(s)
ensure
students.has(s) and count_students.item = old count_students.item + 1
```

delete

``` require
s /= void
students.has(s)
ensure
not students.has(s) and count_students.item = old count_students.item - 1
```

invariant

``` name /= void and then not name.is_empty
students /= void
count_students /= void
count_students.item = students.count
```

</eiffel>

# 3 Design Pattern Categories (11 Points)

## 1. Creational design patterns

Singleton, Abstract Factory, Builder

## 2. Behavioral design patterns

Chain of Responsibility, Strategy, State

## 3. Structural design patterns

Composite, Flyweight, Decorator, Bridge

# 5 Concurrent Programming (17 Points)

T, T, F (??)

## 5.4 Programming

<eiffel> m: MUTEX -- access to queue s: SEMAPHORE -- number of jobs waiting create m.make(1) create s.make(0) </eiffel>