IB DP Computer Science Option D: Object-oriented programming -: D.1 – Objects as a programming concept 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
                   }
}
he 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
$[4]$ 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 fours [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 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;
}

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

A company provides car parking services in large cities and needs a computer system to keep track of the number of vehicles using its parking areas.
When a vehicle arrives, its registration plate is recorded on the system and it is allocated a number that identifies where it should park. When the vehicle leaves, that space is made available for other vehicles to use.
Vehicles are identified by their unique registration plate, which is an alphanumeric code of eight characters (e.g. x1234567). This is clearly displayed on the vehicle.
A programmer created the classes ParkingArea and vehicle to model the above situation.

public class ParkingArea {
              private Vehicle vehicles[];
              private String name;

                   ParkingArea(String name, int capacity) {
                     this.name = name;
                    if (capacity > 300) capacity = 300;
                    this.vehicles = new Vehicle[capacity];
               }

                    String getName() {
                    return name;
             }

                   public int getCapacity() {
                return vehicles.length;
             }

             public int findVehicle(String reg) {
               //find where the vehicle is located in the array and
               //return the index not yet written
             }
}

public class Vehicle {
            private String registration;
            private byte colour;
            private boolean broken;

           public final static byte BLACK=1;
           public final static byte WHITE=2;
           public final static byte BLUE=3;
          public final static byte RED=4;
           public final static byte GREEN=5;
            private final static double ADMIN_FEE = 3;

     public Vehicle() {}

         public Vehicle(String registration) {
                 this.registration = registration;
   }
             public Vehicle(String registration, byte colour) {
                 this.registration = registration;
this.colour=colour;
  }
          public void setBroken(boolean broken) {
           this.broken=broken;
 }
           public void setColour(byte colour) {
            this.colour=colour;
 }
            public boolean getBroken() {
           return broken;
  }
        public String getRegistration() {
         return registration;
 }
          public double pay(int hours) {
   // code to return admin fee – only if applicable
       }
}

a. Outline one effect of using the modifier static when declaring a variable.[2]

b. Describe the relationship between the classes vehicle and ParkingArea.[3]

c. Outline why it is necessary to use the keyword this in the setBroken method of the vehicle class.

d.i. Construct code to create an instance of the vehicle class that has a registration of 1234567.$[2]$

d.ii.Construct code that sets the colour of the object created in part (i) as black.[2]

▶️Answer/Explanation

Ans:

a. )
Static means it is the same for all instances of the class;
because it is contained in the class rather than in an instance of the class/object / it is defined at the class level;
Static means that less memory is taken up;
as only 1 memory allocation it created for all instances rather than 1 per instance;
b. )
Award [1] for aggregation (accept Composition also);
Award [1] for An instance of ParkingArea can contain Vehicles;
Award [1] for A single ParkingArea can contain between 0 and 300 instances of Vehicle;
c. )
To distinguish between 2 variables with the same name;
Allows the local/instance variable to be set to the parameter/class variable;
Note: Award [1] if the incorrect technical terms for variables are given but the idea of distinguishing between 2 types of variables with the same name is expressed.
d.i. )
Award [1] for correctly declaring the variable as type Vehicle;
Award [1] for using the correct constructor;
e.g.

Vehicle v=new Vehicle(“X1234567”);

d.ii )
Award [1] for calling the correct method on the correct instance variable name;
Award [1] for using the constant correctly, i.e. Vehicle.BLACK;
v.setcolour (Vehicle.BLACK);
Note: accept the constant prefixed by the name of the instance instead of the class

Scroll to Top