IB DP Computer Science Option D: Object-oriented programming -: D.2 – Features of OOP SL Paper 2

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.

b. Outline two differences between inheritance and aggregation. $[4]$

c. 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.

d.i. 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 [2]

or less; otherwise, it returns 0.
d.ii.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 [2]

area.
e. The array vehicles [] in the ParkingArea class is used to store instances of the car or Motorbike class.
$[2]$
Outline why vehicle is a valid type for this array.

▶️Answer/Explanation

Ans:

b. )
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;

c. )
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;

d.i. )
Award [1] for returning 0 if $>5$ hours;
             public double pay(int hours) \{
                 if (hours <= 5) \{
              $\quad$ return ADMIN_FEE;
\} else return 0 ;
d.ii)
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) thourlyFeethours;
\}

e. )
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;

Question 

The supermarket chain wants to use this OOP simulation to experiment with different ways of organizing their check-out system. For example, it is possible to have different check-out counters such as cash-only or card only, or to have check-out counters for ten items or fewer.
It is also possible to have one line that serves a number of check-out counters.

a. Define the term inheritance.$[2]$

b. Explain one advantage of the OOP feature “inheritance” with reference to this scenario.[3]

c. Describe two advantages of using libraries of classes.$[4]$

e. Outline two reasons why the use of multiple programming teams in different locations may be problematic when developing an integrated [4]

software solution.

▶️Answer/Explanation

Ans:

a.)
A new class is derived from an existing class;
The new class inherits all variables/data/properties and methods/behaviours of the other class;
The derived class is called a subclass/child, and the original is called a superclass/parent;

b. )
Award [1] for an advantage;
Award [1] for some elaboration;
Award [1] for a reference to the context;
Note: to a certain extent mixing and matching can take place
It promotes code reuse;
because the superclass posline;
holds common data and actions that are shared by all newly developed classes;
It reduces maintenance overhead;
because you only have to update the superclass;
in this case posline;
allows extensibility / ability to create other classes easily;
specific types of check-out can be created;
reducing development time / costs / testing;

c. )
Saves development time;
Since classes and their methods do not need to be rewritten;
Promotes abstraction;
Because reusable code exists that functions without knowledge of internal working;
Libraries contain error-free / robust code; because it has been used and tested many times;
Promotes efficiency / organization;
As code will be shorter / easier to read / develop;
Familiarity with libraries;
Allow for easier maintenance / modification;

e. )

Teams may be located in different countries;
therefore have communication issues;
due to different languages;
or different time zones;
inability to discuss face-to-face;
Problems with different conventions (e.g. date format);
Managing the teams in different locations may be problematic (allow example);
Teams may not collaborate well due to personality issues;
Development time might increase;
Due to time lags between communications;

Scroll to Top