Computer Science Paper 2 SL
Computer Science Paper 2 SL
A delivery company uses trains in its operations. It uses an object-oriented program to keep track of its
trains and the parcels that it carries.
The company has many objects in their program; here are some of them.
Object Description
Each Train is made up of RollingStock objects, each of which is either a
Train
Wagon or an Engine.
A RollingStock object can be an Engine (that can pull) or a Wagon (that needs
RollingStock
to be pulled). Each RollingStock has a unique ID number and a weight.
A variety of RollingStock. Each Engine has a maximum weight that it can
Engine
pull.
Wagon A variety of RollingStock. Each Wagon has a maximum cargo weight.
Each Parcel is tagged with a tracking number, the addresses from where it
Parcel
came (origin) and to where it is going (destination) and its weight.
The code on the following pages implements the Train class used in this program.
–2–
(b) Outline the advantages of polymorphism, using the RollingStock class as an example.
[3]
(c) Construct a unified modelling language (UML) diagram of the Train class. [3]
(d) Construct a method getNumberOfWagons(), part of the Train class, that returns the
number of wagons currently coupled to the train. [2]
(e) Construct the removeWagon() method that will remove one wagon from a train and
return the removed object. Include appropriate error checking. [5]
(b) Describe two ways in which programming by a team differs from programming by an
–4–
The following code implements the Parcel class used in the delivery company’s program.
The origin and destination addresses are stored in a Parcel object as simple strings.
However, addresses are complex and there are a lot of different pieces of information that
may or may not be present such as a first name or a business name, in addition to house
number, street name, city and country.
It has been decided to create a new Address class to handle this information.
(c) State the appropriate data type to be used in the Address class to store
(d) Identify the changes to the Parcel class that will be needed to make use of the new
Address class. [3]
(e) Outline how these two new classes can be created with minimal duplication of code. [3]
–5–
(i) Draw the mEngines array after the code fragment has been executed. [2]
(ii) State the value of mEngineCount after the code fragment has been executed. [1]
(iii) Draw the mWagons array after both the code fragment above and the code fragment
below have been executed. [2]
Wagon F = A.removeWagon();
F = A.removeWagon();
A.addWagon(new Wagon(214));
The parcels loaded into a wagon cannot weigh more than the capacity of the wagon. A train’s
engines must have enough combined power to pull the loaded wagons. The company needs
to be able to check that these requirements are being met.
(b) Construct the getWeight() method in the Wagon class that returns the total combined weight of
the parcels currently in the wagon and the wagon itself. [4]
(c) Construct the getWeight() method in the Train class that returns the total combined weight of
all the parcels, engines and wagons in a train. [4]
(d) Explain why having a getWeight() method in both the Train and Wagon classes
does not cause a compiler error, even though the Train class does not inherit from the
RollingStock class. [2]
End of Option D