Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Online Courses / University Track / Computing Foundations

Module 6 · Lab

Lab: design, trace and test a fines function

Decompose a library fines task, write a function with a cap, trace it and choose a data structure.

  • 50 minutes
  • 14 marks
  • Self-marked

50 minutes. Self-marked against the rubric. 14 marks. In the full course this counts toward the labs and cases share (15% of the course grade). Write your answers in the boxes, then open the model answer and tick each mark you earned honestly. Nothing is sent or stored.

Scenario (invented)

A small library (invented) charges a fine for late books: 20 pence for each day late, but never more than 500 pence (£5) per book. Members are identified by a number, and the library wants to look up a member by number.

Tasks

Task 1 (4 marks)

Split the task “work out and record the fine for a returned book” into at least three parts. For each, give its inputs and its output.

Private: nothing you type here is sent or stored.

Task 2 (4 marks)

Write fine_for(days_late) so that it returns 20 pence per day, capped at 500 pence. Use a condition.

Private: nothing you type here is sent or stored.

Task 3 (3 marks)

Trace fine_for for 3, 25 and 30 days. Say which are boundary tests.

Private: nothing you type here is sent or stored.

Task 4 (3 marks)

Which data structure should hold fines by member number, and why? Name the key and the value.

Private: nothing you type here is sent or stored.

Rubric and model answer

Task 1: decompose (4 marks)

Model: (1) days_late(due, returned): inputs two dates, output a number of days. (2) fine_for(days_late): input days, output pence. (3) record_fine(member, fine): inputs member number and pence, output a saved record.

Task 2: write the function (4 marks)

Model: fine_for(days_late): amount = days_late × 20; if amount > 500 then return 500; otherwise return amount.

Task 3: trace and test (3 marks)

Model: 3 days: 60 pence. 25 days: 500 pence (exactly the cap). 30 days: 600, over the cap, so 500 pence. The 25-day case is the boundary; also test 24 days (480) and 26 days (500).

Task 4: choose a structure (3 marks)

Model: a dictionary, with the member number as the key and the total fine in pence as the value, because the library looks up a member by number and not by position.

Marks so far: 0 of 14