CSE 331: Section 1 — Specifications
Task 1 — At Your Own Pair-il
Suppose price is an integer and items is an array. For each pair of assertions, compare them and state
which one is stronger and which one is weaker (reminder that a stronger assertion implies a weaker one meaning that if the stronger one is true, the weaker one must be true as well!), or if they are incomparable:
-
price > 0 and items is not nulloritems is not null -
price > 0orprice >= 0 -
items is an arrayoritems is a sorted array -
price > 0oritems is not null
Task 2 — The Same Ol' Strong and Dance
We plan to provide the following method:
/** Returns the best menu item for the given price.
* ...
*/
public static MenuItem findBest(int price, MenuItem[] items);
To do so, we need to fill in the rest of the specification. For example, we need to explain exactly which item will be returned. The term "best" is far too vague.
We are considering the following alternatives:
@requires price > 0 and items is not null // Spec A
@return an item T in items with T.price <= price <= T.price + 0.5
or null if none exists
@requires items is not null // Spec B
@throws IllegalArgumentException if price <= 0
@return an item T in items with T.price <= price <= T.price + 0.5
or null if none exists
@requires price > 0 and items is not null // Spec C
@return the item in items whose price is closest to the given price
but not more than the given price or null if none exists
@requires price > 0 // Spec D
@throws NullPointerException if items is null
@return the item in items whose price is closest to the given price
but not more than the given price or null if none exists
@requires price > 0 // Spec E
@return an item T in items with T.price <= price <= T.price + 0.5
or null if none exists or if items is null
-
Fill in the following table explaining the relationships between each pair of specifications. Write an "S" if the spec on left (the row) is stronger than the spec on top (the column), a "W" if the left spec is weaker, and "---" if the specs are incomparable.
A B C D E A X B X C X D X E X -
Now consider a new pair of precondition and postcondition behaviors:
@requires price > 0 @return an item T in items with T.price <= price <= T.price + 0.5This specification is not sensible, what is wrong with it? Why shouldn't we use it?
Task 3 — Great Finds Think Alike
For each of the following implementations, state which of the specifications it satisfies. If it does not satisfy some specification, explain (in as few words as possible) why it does not.
-
public static MenuItem findBest(int price, MenuItem[] items) { if (price <= 0) throw new IllegalArgumentException("bad price"); for (int i = 0; i < items.length; i++) { if (items[i].price <= price && price <= items[i].price + 0.5) return items[i]; } return null; } -
public static MenuItem findBest(int price, MenuItem[] items) { items.sort(); // puts items in order by increasing price MenuItem best = null; for (int i = 0; i < items.length; i++) { if (items[i].price <= price) best = items[i]; } return best; } -
public static MenuItem findBest(int price, MenuItem[] items) { if (items == null) return null; if (price <= 0) throw new IllegalArgumentException("bad price"); for (int i = 0; i < items.length; i++) { if (items[i].price <= price && price <= items[i].price + 0.5) return items[i]; } return null; }