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
Connected topic: Decomposition and abstraction (§7.1)
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
Start with the complete job: serve a restaurant customer. Decomposition means breaking that goal into smaller jobs with clear purposes.
One workable three-part tree is:
Main task Two example sub-tasks Take the order Approach and greet the customer; record the requested meals. Serve the order Collect the prepared meals; bring each meal to the correct table. Close the service Ask whether anything else is needed; prepare and present the bill. 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.
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
Treat the communication function as an interface: describe what enters and leaves without claiming how the sensors or software work inside it.
One restaurant example is:
Direction Example signals Possible meaning Customer → robot input Spoken request “Please bring the menu.” Customer → robot input Touchscreen selection Selected menu item or table option. Customer → robot input Hand gesture Calling the waiter or indicating readiness. Robot → customer output Spoken response Greeting, clarification, or confirmation. Robot → customer output Screen message Shows the current order or bill. Robot → customer output Sound signal Confirms a request or asks for attention. 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.
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
Connected topic: Decomposition and abstraction (§7.1)
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
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.
For example:
Entity Attributes (state) Actions (behaviour) Customer Table identifier; current order; dietary request. Ask for a menu; place or change an order; request the bill. Service robot Current table; order list; meal availability or status. Move to a table; record an order; deliver meals; present a bill. 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.
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
Connected topic: Defining and calling functions (§7.2) · Return values (§7.3)
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
A function should have one job. Let
servehandle one customer's order and letstate_accountcalculate and present that customer's bill.Function Parameters Steps Output 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. Passing the order list into
servemakes the data dependency visible. The example sheet's illustrativeget_orderrefers to a list in its description that is not in its parameter list; passing such state explicitly avoids relying on an undocumented global.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
Assume the robot and restaurant provide these interface helpers:
prepare_meal(meal_id),deliver_meal(customer, meal_id),price_for(meal_id), andpresent_bill(customer, total). Their hardware or restaurant-system internals are outside this exercise's abstraction.Use a
forloop 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 totalThe 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.
Check: if the list is empty, both loops do zero iterations;
servereturns an empty list andstate_accountpresents a total of zero.
Exercise 4.4: Problem solving
Connected topic: Decomposition and abstraction (§7.1) · Functions (§7.2)
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
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.
One decomposition is
check availability→prepare and deliver available mealornotify customer and record unavailable meal→continue order. Ask before substituting a different meal; a replacement should not be chosen without the customer's agreement.Adapt
serveto 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_mealsUse
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.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).