CS/SWE 332 Assignment 7
Fall 2020


Goal: Type Abstraction.

Consider the following market class.

class Market {
   private Set<Item> wanted;           // items for which prices are of interest
   private Bag<Item, Money> offers;    // offers to sell items at specific prices
   // Note:  Bag isn't a Java data type.  Here, the bag entries are pairs.

   public void offer (Item item, Money price)
      // Requires: item is an element of wanted
      // Effects:  add (item, price) to offers
   
   public Money buy(Item item)
      // Requires: item is an element of the domain of offers
      // Effects: choose and remove some (arbitrary) pair (item, price) from
      //          offers and return the chosen price
}
  1. Suppose that offers are only accepted if they are lower than previous offers.
    class Low_Bid_Market extends Market {
       public void offer (Item item, Money price)
          // Requires: item is an element of wanted
          // Effects:  if (item, price) is not cheaper than any existing pair
          //           (item, existing_price) in offers do nothing
          //           else add (item, price) to offers
    

    Is Low_Bid_Market a valid subtype of Market? Appeal to the methods rule to back up your answer.
  2. Suppose that the buy() method always chooses the lowest price on an item.
    class Low_Offer_Market extends Market {
       public Money buy(Item item)
          // Requires: item is an element the domain of offers
          // Effects: choose and remove pair (item, price) with the 
          //          lowest price from offers and return the chosen price
    

    Is Low_Offer_Market a valid subtype of Market? Appeal to the methods rule to back up your answer.
Note: This is purely a "paper and pencil" exercise. No code is required. Write your answer so that it is easily understandable by someone with only a passing knowledge of Liskov's rules for subtypes.