User interface language: English | Español

Date November 2020 Marks available 2 Reference code 20N.2.SL.TZ0.11
Level SL Paper 2 Time zone no time zone
Command term Construct Question number 11 Adapted from N/A

Question

Two further classes, Car and Motorbike, are created.

public class Car extends Vehicle{
public static double hourlyFee=3.5;
public double pay(int hours) {
//code to calculate and return the complete price
}
}

public class Motorbike extends Vehicle{
public static double hourlyFee=2.5;
public double pay(int hours) {
//code to calculate and return the complete price
}
}

The method pay in the Vehicle class returns the administration fee (which is only part of the total price), while the method pay of the Car class calculates the total price for a car staying in the parking area.

Outline two differences between inheritance and aggregation.

[4]
b.

Construct a UML diagram that shows the relationships between the ParkingArea, Vehicle, Motorbike and Car classes. There is no need to include the attributes or methods of each class.

[4]
c.

Construct the method pay in the Vehicle class that returns the admin fee stored in the variable AdminFee if the vehicle has stayed for five hours or less; otherwise, it returns 0.

[2]
d.i.

Construct the method pay in the Car class, where it uses the vehicle method pay but adds the charge for the amount of time spent in the parking area.

[2]
d.ii.

The array vehicles[] in the ParkingArea class is used to store instances of the Car or Motorbike class.

Outline why Vehicle is a valid type for this array.

[2]
e.

Markscheme

Award [4 max].
Inheritance is when the behaviour of a class is transferred to another class;
whereas aggregation is when a class is contained inside another and used;

Aggregation is useful for modelling a real life situation of containment aka. “has a” (e.g. contents of a box);
whereas Inheritance is useful for modelling real life situation that one entity is a particular (more specific) type of another entity, aka. “is a”;

While both can achieve the same result, Aggregation (composition) can lead to more robust and safer code;
whereas inheritance is a more flexible solution but prone to errors due to the behaviour of subclasses being changed;

Inheritance allows for code reuse / lowers maintenance cost;
whereas aggregation does neither of the above;

Can have more than 1 aggregation;
but only 1 superclass (in Java);

Inheritance allows for code reuse as in the creation of sub-classes;
whereas with aggregation each class must be written out in full;

Mark as [2] + [2].

b.

Award [4 max].
Award [1] for all four classes shown;
Award [1] for showing the links in the correct place (any arrow or line is acceptable);
Award [1] for distinguishing the relationship ParkingArea - Vehicle;
Award [1] for distinguishing the relationships Car/Motorbike -Vehicle;

c.

Award [1] for returning admin fee if <=5 hours;
Award [1] for returning 0 if >5 hours;

public double pay(int hours) {
if (hours <= 5) {
return ADMIN_FEE;
} else return 0;
}
d.i.

Award [2 max].
Award [1] for for super.pay(hours);
Award [1] for returning result of correct formula (i.e. adding on hourlyFee*hours);

Example:

public double pay(int hours) {
return super.pay(hours)+hourlyFee*hours;
}
d.ii.

Award [2 max].
Cars and Motorbikes are also Vehicles as they extend the Vehicle class / Vehicle is the superclass and Car/Motorbike are subclasses;
The subclasses can be cast as/considered to be/referred to as Vehicle which is its superclass;
As Cars and Motorbikes inherit from Vehicles they are also Vehicles and can therefore be stored in an array of type Vehicle;
Arrays must be declared as 1 type, and the state and behaviour of Vehicle is what both Car and Motorbike have in common. Therefore they can be referenced as Vehicles and stored in an array of Vehicles, even though they are subclasses;

e.

Examiners report

This tested the level of understanding of OOP principles. Not many could clearly put into words the specific differences and tended to provide more in the line of definitions. The better students clarified the dependency levels between the different classes.

b.

This was well-answered. Most students either used the correct type of arrows or indicated the relationships with the correct expressions (has a etc.).

c.

Part (i) was well-answered, but few students understood the use of the super modifier in part (ii). All aspects of the OOP course (including inheritance) should be subject to comprehensive practical work in order to promote full understanding.

d.i.

Part (i) was well-answered, but few students understood the use of the super modifier in part (ii). All aspects of the OOP course (including inheritance) should be subject to comprehensive practical work in order to promote full understanding.

d.ii.

The comments for part (d) are also appropriate here.

e.

Syllabus sections

Option D: Object-oriented programming » D.3 Program development
Show 45 related questions
Option D: Object-oriented programming

View options