Check requests before changing state

Two separate SnackDispenser objects, A and B, each hold at most 20 packets. Initially, A holds 8 packets and B holds 13 packets.


For each positive integer request, vend(k) removes exactly kk packets if enough are available. The method load(k) adds exactly kk packets if the resulting stock does not exceed 20. Otherwise, the request is rejected and the target object's stock stays unchanged. Neither method changes the other object.


Complete the table after each call, processing the rows from top to bottom. Enter yes or no for acceptance and exact integers for both stock counts.

CallAcceptedA stock (packets)B stock (packets)
A.vend(3)
B.load(9)
A.load(8)
B.vend(4)

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 The Object-Oriented Paradigm Questions: