Question 5
OCR Computer Science November 2021 Algorithms and programming (H446-02)A level22 marks
On the specification
- (a)
- 2.2 Problem solving and programming
- (b)
- 2.2 Problem solving and programming
- (c)
- 2.2 Problem solving and programming
- (d)
- 2.2 Problem solving and programming
- (e)
- 1.4 Data types, data structures and algorithms2.2 Problem solving and programming
- (f)
- 1.4 Data types, data structures and algorithms2.2 Problem solving and programming
Question text
5 Christoff is writing a program to simulate a city using object-oriented programming. He is designing classes to store different types of buildings and their location on the road. He has created the following plan for some of the buildings: class name : building attributes: private numberFloors private width private height methods: new(pFloors, pWidth, pHeight) function getNumberFloors() function getWidth() function getHeight() function setNumberFloors(pFloors) function setWidth(pWidth) function setHeight(pHeight) class name : office class name : house attributes: attributes: private numDesks : integer private bedrooms private numCompanies : integer private bathrooms methods: methods: new(pFloors, pWidth, pHeight, new(pFloors, pWidth, pHeight, pDesks, pCompanies) pBedrooms, pBathrooms) function getDesks() function getBedrooms() function getCompanies() function getBathrooms() function setDesks(pDesks) function setBedrooms(pBedrooms) function setCompanies(pCompanies) function setBathrooms(pBathrooms) (a) The method new is used to denote the constructor for each class. State the purpose of the constructor. [1] The classes office and house inherit from building. (b) Describe what is meant by inheritance with reference to these classes. [2] (c) Part of the declaration for the class building is shown. Complete the pseudocode declaration by filling in the missing statements. class building private numberFloors private width private public procedure new(pFloors, pWidth, pHeight) numberFloors = width = pWidth height = pHeight endprocedure public function getNumberFloors() return endfunction public function setNumberFloors(pFloors) //sets the value of numberFloors when the parameter is >= 1 //returns true if numberFloors is successfully changed, //returns false otherwise if pFloors >= 1 then numberFloors = return true else return endif endfunction endclass [5] (d) Write program code or pseudocode to declare the class house. Define the attributes and constructor method in your answer. You do not need to write the get or set methods. [6] (e) Christoff develops a new class to store the houses in one road. His class design is shown: class : houseRoad attributes: private buildings(100) //array of class house private numberBuildings //records the number //of houses currently stored in the array //buildings methods: new(building) function getBuilding(buildingNum) procedure newbuilding(pBuilding) The method newbuilding() takes a new building as a parameter, and stores this in the next free space in the array buildings. Write pseudocode or program code for the method newbuilding(). [4] (f) Christoff wants to create a new house called houseOne. It has the properties: 2 floors, 8(m) width, 10(m) height, 3 bedrooms and 2 bathrooms. The house is located on a road with the identifier limeAvenue of type houseRoad, houseOne is the first house in this road. Write pseudocode or program code to declare the house houseOne, road limeAvenue and assign houseOne to the first array position in the road. [4]