Question 2
OCR Computer Science November 2020 Paper 1A level17 marks
On the specification
- (a)(i)
- 2.2 Problem solving and programming
- (a)(ii)
- 2.2 Problem solving and programming
- (a)(iii)
- 2.2 Problem solving and programming
- (b)
- 2.2 Problem solving and programming
- (c)
- 2.2 Problem solving and programming
Question text
2 A supermarket uses an object-oriented approach to organise items that it offers for sale. Part of the class definition for the ItemForSale class is shown below. class ItemForSale public itemName public price public discount endclass (a) The discount attribute represents a percentage discount on the price. The discount can be between 0 and 50 (inclusive). All new items for sale initially have a discount value of 0. (i) Write the constructor method for the ItemForSale class. [4] (ii) Write a line of code to create an object of type ItemForSale called mushypeas that has a name of “mushy peas” and a price of £0.89 [3] (iii) Write the calculatePrice() method, which applies the percentage discount to the price and returns the new value. [3] (b) The supermarket has previously had issues with discounts being set as values above 50. Explain how encapsulation could be applied to the ItemForSale class to stop this problem from occurring. You are not expected to write any code in your answer to this question. [3] Some items in the supermarket are only available through home delivery. These items are the same as ItemsForSale with the following exceptions: • the supermarket also stores the location of the stock • the percentage discount allowed is up to 75 rather than the standard 50. (c) Explain how inheritance can be used to implement the above requirements. [4]