Question 1(b)(ii) 2021 · May-June · Paper 43 · 5 marks
Reveal official mark-scheme answer
Official answer for Question 1
Part (b)
(ii)
Question 1
mark per bullet point to max 5
Question 5
- Function header taking parameter (and close where appropriate)
- Finding position in array // finding if not in array …
- … if not found, return −1
- Checking cost against moneyIn …
- … if not enough money, return –2
- … if found and enough money, return position
- Using Items, getCost() and getCode() throughout
Show text version of question
Question 1
Part (b)
(ii)
Question 1
A vending machine allows users to insert coins to purchase an item.
The user then enters the code for the item they would like the machine to dispense (give out). The user must re-enter the code until it is valid.
If the code is valid but the user has not inserted enough money for the item chosen, the machine waits for more coins to be inserted. The user then has to re-enter the code.
The user can press cancel at any time to return the money inserted into the machine. The vending machine is part of a program that is written using object-oriented programming (OOP). The vending machine makes use of two classes that are described in the following tables.
All attributes are declared as private.
foodItem
name : STRING // the name of the item of food
code : STRING // the code to be entered for that item to be // selected
cost : REAL // the cost of the item constructor(nameP, codeP, // creates an instance of foodItem costP) // takes the name, code and cost as parameters
getCode() // returns the code for the item getCost() // returns the cost of the item getName() // returns the name of the item
vendingMachine
items : ARRAY[0:3] OF foodItem // stores four items of type foodItem moneyIn : REAL // stores the total money inserted by the // user, initialised to 0 in the constructor constructor(item1, item2, // creates an instance of vendingMachine, item3, item4) // takes four objects of type foodItem as // parameters and stores them in array items
insertMoney() // takes the value of the coin as a parameter // and adds it to moneyIn checkValid () // takes a code as a parameter and checks it is // valid against the food item codes getItemName() // takes the array index as a parameter and // returns the name of the food items
Question 4
(ii)
The method checkValid() takes the food item code as a parameter. It checks the
code against each element in items and returns:
- –1 if the code is not valid
- -2 if the code is valid, but the moneyIn is less than the cost of the item
- the index of the item, if the code is valid and the moneyIn is greater than or equal
to the cost of the item.
Write program code for the method checkValid().
Programming language .....................................................................................................
Program code
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.....................................................................................................................................
Why this question appears here
98% assigned-topic match · Further programming › Programming paradigms
Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.
Permanent source page: Educate A Change · From 9608_s21_qp_43.pdf
