Which cruise controllers can stand in?

A base class CruiseControl defines setSpeed(s), where ss is an integer target speed in km/h and vv is the speed the vehicle settles at after the call. Four subclasses override the routine with the contracts below. Client code is written against the base contract and may receive any of the subclasses at run time.


Class

Precondition

Postcondition

CruiseControl (base)

0≤s≤1200 \le s \le 120

∣v−s∣≤2\lvert v - s \rvert \le 2

FastCruise

0≤s≤1500 \le s \le 150

∣v−s∣≤1\lvert v - s \rvert \le 1

CityCruise

0≤s≤1000 \le s \le 100

∣v−s∣≤1\lvert v - s \rvert \le 1

EcoCruise

0≤s≤1200 \le s \le 120

∣v−s∣≤4\lvert v - s \rvert \le 4

TruckCruise

10≤s≤13010 \le s \le 130

∣v−s∣≤2\lvert v - s \rvert \le 2


For each subclass, enter how many integer speeds that are legal under the base precondition its own precondition rejects. Then answer yes or no: does its precondition accept every base-legal input, does its postcondition guarantee at least the base promise, and can it safely substitute for the base class?

SubclassBase-legal speeds rejectedAccepts every base inputKeeps base guaranteeValid substitute
FastCruise
CityCruise
EcoCruise
TruckCruise

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Design by Contract Questions: