APDR

Blackboard Exercise 4: Worked solutions

Algorithms, Programming, and Data Representation · Module 92313 · Blackboard Exercise 4

Break a restaurant robot into manageable jobs, describe its customer-facing black box, then specify and implement functions for taking orders, serving meals, and presenting the bill.

Exercise 4.1: Decomposition

4.1.1 Decompose the waiter robot

Try first: Split the restaurant-waiter program into three main jobs and give two smaller tasks under each.

Attempted it? Open the worked steps

4.1.1 Decompose the waiter robot · worked solution

  1. Start with the complete job: serve a restaurant customer. Decomposition means breaking that goal into smaller jobs with clear purposes.

  2. One workable three-part tree is:

    Main taskTwo example sub-tasks
    Take the orderApproach and greet the customer; record the requested meals.
    Serve the orderCollect the prepared meals; bring each meal to the correct table.
    Close the serviceAsk whether anything else is needed; prepare and present the bill.
  3. The diagram is one possible design, not the only valid decomposition. Each child should help complete its parent task, and the six leaf tasks together should cover the service the program is expected to provide.

  4. Check: the list moves from the customer's request, through delivery, to the end of the interaction; payment processing can be added as a child of closing service if the robot is responsible for it.

4.1.2 Model communication as a black box

Try first: Give three inputs to the robot's communication box, three possible outputs, and show which way each signal travels.

Attempted it? Open the worked steps

4.1.2 Model communication as a black box · worked solution

  1. Treat the communication function as an interface: describe what enters and leaves without claiming how the sensors or software work inside it.

  2. One restaurant example is:

    DirectionExample signalsPossible meaning
    Customer → robot inputSpoken request“Please bring the menu.”
    Customer → robot inputTouchscreen selectionSelected menu item or table option.
    Customer → robot inputHand gestureCalling the waiter or indicating readiness.
    Robot → customer outputSpoken responseGreeting, clarification, or confirmation.
    Robot → customer outputScreen messageShows the current order or bill.
    Robot → customer outputSound signalConfirms a request or asks for attention.
  3. Draw the three input arrows toward the black box and the three output arrows away from it. Other choices are valid if they are plausible communication signals and the directions are correct.

  4. Check: each item belongs to one side of the interface; a spoken order is input to the robot, while its spoken confirmation is output.

Exercise 4.2: Abstraction

4.2.1 Describe the customer and service robot

Try first: List at least two useful attributes and two actions for each of the customer and the service robot. Which details does the waiter program need?

Attempted it? Open the worked steps

4.2.1 Describe the customer and service robot · worked solution

  1. An abstraction keeps the details needed for the job and leaves out unrelated facts. Attributes describe the current state; actions describe something an actor can do.

  2. For example:

    EntityAttributes (state)Actions (behaviour)
    CustomerTable identifier; current order; dietary request.Ask for a menu; place or change an order; request the bill.
    Service robotCurrent table; order list; meal availability or status.Move to a table; record an order; deliver meals; present a bill.
  3. For the waiter program, the table identifier connects the customer to the correct delivery, and the order list records meal identifiers. The robot's current position and meal status support navigation and serving. A customer's hair colour or the robot's internal motor temperature can be left out unless another requirement uses them.

  4. Check: each listed attribute helps the service task, and each action is a verb that changes the interaction or its state.

Exercise 4.3: Functions

4.3.1 Specify serve and state_account

Try first: For each function, choose only the parameters it needs, outline its steps, and name its output.

Attempted it? Open the worked steps

4.3.1 Specify serve and state_account · worked solution

  1. A function should have one job. Let serve handle one customer's order and let state_account calculate and present that customer's bill.

  2. FunctionParametersStepsOutput
    servecustomer, customer_order_listFor each meal: arrange preparation, deliver it to that customer, record it as served.The list of meals actually served.
    state_accountcustomer, served_mealsAdd the price of each served meal, present the total to the customer.The amount presented as due.
  3. Passing the order list into serve makes the data dependency visible. The example sheet's illustrative get_order refers to a list in its description that is not in its parameter list; passing such state explicitly avoids relying on an undocumented global.

  4. Check: the account uses the returned served-meal list, so an unserved item need not be charged later.

4.3.2 Write the functions in Python

Try first: Turn the two function designs into Python, using a loop for the list of meals and returning useful results.

Attempted it? Open the worked steps

4.3.2 Write the functions in Python · worked solution

  1. Assume the robot and restaurant provide these interface helpers: prepare_meal(meal_id), deliver_meal(customer, meal_id), price_for(meal_id), and present_bill(customer, total). Their hardware or restaurant-system internals are outside this exercise's abstraction.

  2. Use a for loop because the order is a finite collection of meal identifiers:

    def serve(customer, customer_order_list):
        served_meals = []
        for meal_id in customer_order_list:
            prepare_meal(meal_id)
            deliver_meal(customer, meal_id)
            served_meals.append(meal_id)
        return served_meals
    
    
    def state_account(customer, served_meals):
        total = 0
        for meal_id in served_meals:
            total += price_for(meal_id)
        present_bill(customer, total)
        return total
  3. The first function returns the meals it served. The second totals only those meals, presents the bill, and returns the amount so another function could use it.

  4. Check: if the list is empty, both loops do zero iterations; serve returns an empty list and state_account presents a total of zero.

Exercise 4.4: Problem solving

4.4.1 Continue when a meal is unavailable

Try first: Decompose the response to one unavailable meal so the other meals in the same order can still be served.

Attempted it? Open the worked steps

4.4.1 Continue when a meal is unavailable · worked solution

  1. Separate the decision for each meal: check availability, serve it if available, otherwise tell the customer and record it as unavailable. Then continue to the next meal rather than returning from the whole function.

  2. One decomposition is check availability → prepare and deliver available meal or notify customer and record unavailable meal → continue order. Ask before substituting a different meal; a replacement should not be chosen without the customer's agreement.

  3. Adapt serve to return both outcomes:

    def serve(customer, customer_order_list):
        served_meals = []
        unavailable_meals = []
        for meal_id in customer_order_list:
            if meal_available(meal_id):
                prepare_meal(meal_id)
                deliver_meal(customer, meal_id)
                served_meals.append(meal_id)
            else:
                notify_unavailable(customer, meal_id)
                unavailable_meals.append(meal_id)
        return served_meals, unavailable_meals
  4. Use state_account(customer, served_meals) after serving. This bills the meals actually delivered; unavailable items are excluded. If the customer accepts a substitute, add its identifier to the served list only after it has been delivered.

  5. Check: the unavailable branch records and reports the item but does not exit the loop, so the next ordered meal is still checked.

Personal worked solutions based on APDR Blackboard Exercise 4, written in my own words; no exercise-sheet prose or figures are reproduced. The restaurant helper functions are abstract interfaces assumed for the example. Not an official FAU publication; check course details against the current materials. Made by Thanate Amornjiraporn (Focus).