Question
A computer game is being designed that will include different vehicles. A prototype for the game is being developed using object‑oriented programming.
The class Vehicle stores data about the vehicles. Each vehicle has an identification name, a maximum speed, a current speed and a horizontal position. The value IncreaseAmount is added to the current speed each time the vehicle increases its speed.
Vehicle
ID : STRING stores the identification name for the vehicle
MaxSpeed : INTEGER stores the maximum speed
CurrentSpeed : INTEGER stores the current speed
IncreaseAmount : INTEGER stores the amount CurrentSpeed increases by
HorizontalPosition : INTEGER stores the horizontal position
Constructor() initialises ID, MaxSpeed and IncreaseAmount to the parameter values initialises both CurrentSpeed and
HorizontalPosition to 0
GetCurrentSpeed() returns the current speed
GetIncreaseAmount() returns the increase amount
GetHorizontalPosition() returns the horizontal position
GetMaxSpeed() returns the maximum speed
SetCurrentSpeed() assigns the parameter to the current speed
SetHorizontalPosition() assigns the parameter to the horizontal position
IncreaseSpeed() calculates and stores the new speed and horizontal position of the vehicle
(a) (i) Write program code to declare the class Vehicle. All attributes must be private.
You only need to declare the class and its constructor. Do not declare any other methods.
Use your programming language’s appropriate constructor.
If you are writing program code in Python, include attribute declarations using comments.
Save your program as Question2_J2023.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[5]
(ii) Write program code for the get methods GetCurrentSpeed(),
GetIncreaseAmount(), GetMaxSpeed() and GetHorizontalPosition()
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
(iii) Write program code for the set methods SetCurrentSpeed() and
SetHorizontalPosition()
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[3]
(iv) The method IncreaseSpeed():
• adds IncreaseAmount to the current speed
• adds the updated current speed to the horizontal position.
The current speed of a vehicle cannot exceed its maximum speed.
Write program code for the method IncreaseSpeed()
Save your program.
Copy and paste the program code into part 2(a)(iv) in the evidence document.
[3]
(b) The child class Helicopter inherits from the parent class Vehicle. A helicopter also has a vertical position and changes the vertical position when it increases speed.
Helicopter
VerticalPosition : INTEGER stores the vertical position
VerticalChange : INTEGER stores the amount VerticalPosition changes by
MaxHeight : INTEGER stores the maximum height the helicopter can reach
Constructor() takes the ID, maximum speed, increase amount, vertical change and maximum height as parameters initialises the vertical position to 0
GetVerticalPosition() returns the vertical position
IncreaseSpeed() changes the current speed, horizontal and vertical position of the helicopter
(i) Write program code to declare the class Helicopter. You only need to declare the class and its constructor. You do not need to declare the other methods.
Use your programming language’s appropriate constructor.
All attributes must be private.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[5]
(ii) The Helicopter method IncreaseSpeed() overrides the method from the parent class and:
• adds the amount of vertical change to the vertical position
• adds IncreaseAmount to the current speed
• adds the updated current speed to the horizontal position.
The vertical position of a helicopter cannot exceed its maximum height.
The current speed of a helicopter cannot exceed its maximum speed.
Write program code for the method IncreaseSpeed()
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[4]
(c) A procedure needs to output the horizontal position and speed of a vehicle. If the vehicle is a helicopter, it also outputs the vertical position.
All outputs must include appropriate messages.
Write program code for this procedure.
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[3]
(d) The main program needs to:
• instantiate a car as a new vehicle with the ID "Tiger", a maximum speed of 100 and an increase amount of 20
• instantiate a new helicopter with the ID "Lion", a maximum speed of 350, an increase amount of 40, a vertical change of 3 and a maximum height of 100
• call IncreaseSpeed() twice for the car and then call the output procedure from part 2(c) for the car
• call IncreaseSpeed() twice for the helicopter and then call the output procedure from part 2(c) for the helicopter.
(i) Write program code for the main program.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
[5]
(ii) Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 2(d)(ii) in the evidence document.
[1]
Mark scheme
2(a)(i) 1 mark each 5
Class header (and close where appropriate)
5 (private) attribute declarations including data types
Constructor header (and close where appropriate) taking 3 parameters (min)
Assigning ID, MaxSpeed and IncreaseAmount to parameters
Assigning CurrentSpeed and HorizontalPosition to 0
Example program code:
VB.NET
Class Vehicle
Private ID As String
Private MaxSpeed As Integer
Private CurrentSpeed As Integer
Private IncreaseAmount As Integer
Private HorizontalPosition As Integer
Sub New(IDP, MaxSpeedP, IncreaseAmountP)
ID = IDP
MaxSpeed = MaxSpeedP
CurrentSpeed = 0
IncreaseAmount = IncreaseAmountP
HorizontalPosition = 0
End Sub
End Class
Java
class Vehicle{
private String ID;
private Integer MaxSpeed;
private Integer CurrentSpeed;
private Integer IncreaseAmount;
private Integer HorizontalPosition;
2(a)(i) public Vehicle(String IDP, Integer MaxSpeedP, Integer IncreaseAmountP){
ID = IDP;
MaxSpeed = MaxSpeedP;
IncreaseAmount = IncreaseAmountP;
CurrentSpeed = 0;
HorizontalPosition = 0;
}}
Python
class Vehicle:
#self.__ID string
#self.__MaxSpeed integer
#self.__CurrentSpeed integer
#self.__IncreaseAmount integer
#self.__HorizontalPosition
def __init__(self, IDP, MaxSpeedP, IncreaseAmountP):
self.__ID = IDP
self.__MaxSpeed = MaxSpeedP
self.__IncreaseAmount = IncreaseAmountP
self.__CurrentSpeed = 0
self.__HorizontalPosition = 0
2(a)(ii) 1 mark each 3
1 get function header (and end where appropriate) with no parameter …
…returning attribute (without overwriting)
3 further correct get methods
Example program code:
VB.NET
Function GetCurrentSpeed()
Return CurrentSpeed
End Function
Function GetIncreaseAmount()
Return IncreaseAmount
End Function
Function GetHorizontalPosition()
Return HorizontalPosition
End Function
Function GetMaxSpeed()
Return MaxSpeed
End Function
Java
public Integer GetCurrentSpeed(){
return CurrentSpeed;
}
public Integer GetIncreaseAmount(){
return IncreaseAmount;
}
public Integer GetHorizontalPosition(){
return HorizontalPosition;
}
public Integer GetMaxSpeed(){
return MaxSpeed;
}
2(a)(ii) Python
def GetCurrentSpeed(self):
return self.__CurrentSpeed
def GetIncreaseAmount(self):
return self.__IncreaseAmount
def GetHorizontalPosition(self):
return self.__HorizontalPosition
def GetMaxSpeed(self):
return self.__MaxSpeed
2(a)(iii) 1 mark each 3
1 set procedure (and end where appropriate) taking parameter …
… assigns parameter to the attribute (without overriding)
Second correct set method
Example program code:
VB.NET
Sub SetCurrentSpeed(CSp)
CurrentSpeed = CSp
End Sub
Sub SetHorizontalPosition(HPP)
HorizontalPosition = HPP
End Sub
Java
public void SetCurrentSpeed(Integer CSP){
CurrentSpeed = CSP;
}
public void SetHorizontalPosition(Integer HPP){
HorizontalPosition = HPP;
}
Python
def SetCurrentSpeed(self, CSP):
self.__CurrentSpeed = CSP
def SetHorizontalPosition(self, HPP):
self.__HorizontalPosition = HPP
2(a)(iv) 1 mark each 3
Method header (and close where appropriate) with no parameter and adding IncreaseAmount to CurrentSpeed
Checking if MaxSpeed is exceeded and limiting to max speed (remove increase or assign maximum)
Adding updated CurrentSpeed to HorizontalPosition in all cases (whether MaxSpeed is exceeded or not)
Example program code:
VB.NET
Sub IncreaseSpeed()
CurrentSpeed = CurrentSpeed + IncreaseAmount
If CurrentSpeed > MaxSpeed Then
CurrentSpeed = MaxSpeed
End If
HorizontalPosition = HorizontalPosition + CurrentSpeed
End Sub
Java
public void IncreaseSpeed(){
CurrentSpeed = CurrentSpeed + IncreaseAmount;
if(CurrentSpeed > MaxSpeed){
CurrentSpeed = MaxSpeed;
}
HorizontalPosition = HorizontalPosition + CurrentSpeed;
}
Python
def IncreaseSpeed(self):
self.__CurrentSpeed = self.__CurrentSpeed + self.__IncreaseAmount
if(self.__CurrentSpeed > self.__MaxSpeed):
self.__CurrentSpeed = self.__MaxSpeed
self.__HorizontalPosition = self.__HorizontalPosition + self.__CurrentSpeed
2(b)(i) 1 mark each 5
Class header (and end where appropriate) inheriting from Vehicle
3 (private) attribute declarations with data types
Constructor (and end where appropriate) with (min) 5 parameters
Calling parent constructor with appropriate parameters
Initialising VerticalPosition to 0 and VerticalChange and MaxHeight to attributes
Example program code:
VB.NET
Class Helicopter
Inherits Vehicle
Private VerticalPosition As Integer
Private VerticalChange As Integer
Private MaxHeight As Integer
Sub New(IDP, MaxSpeedP, IncreaseAmountP, VertChangeP, MaxHeightP)
MyBase.New(IDP, MaxSpeedP, IncreaseAmountP)
VerticalPosition = 0
VerticalChange = VertChangeP
MaxHeight = MaxHeightP
End Sub
End Class
Java
class Helicopter extends Vehicle{
private Integer VerticalPosition;
private Integer VerticalChange;
private Integer MaxHeight;
public Helicopter(String IDP, Integer MaxSpeedP, Integer IncreaseAmountP, Integer
VertChangeP, Integer MaxHeightP){
2(b)(i) super(IDP, MaxSpeedP, IncreaseAmountP);
VerticalPosition = 0;
VerticalChange = VertChangeP;
MaxHeight = MaxHeightP;
}}
Python
class Helicopter(Vehicle):
#VerticalPosition Integer
#VerticalChange Integer
#MaxHeight Integer
def __init__(self, IDP, MaxSpeedP, IncreaseAmountP, VertChangeP, MaxHeightP):
Vehicle.__init__(self,IDP, MaxSpeedP, IncreaseAmountP)
self.__VerticalPosition = 0
self.__VerticalChange = VertChangeP
self.__MaxHeight = MaxHeightP
2(b)(ii) 1 mark each to max 4 4
Method header (overriding where required) with no parameter
Adding vertical change to vertical position …
…limiting to maximum height
Repeating/calling/using the code from original for horizontal increase (in every case)
Example program code:
VB.NET
Overrides Sub IncreaseSpeed()
VerticalPosition = VerticalPosition + VerticalChange
If VerticalPosition > MaxHeight Then
VerticalPosition = MaxHeight
End If
Me.SetCurrentSpeed(GetCurrentSpeed() + GetIncreaseAmount())
If Me.GetCurrentSpeed() > Me.GetMaxSpeed() Then
Me.SetCurrentSpeed(Me.GetMaxSpeed())
End If
Me.SetHorizontalPosition(Me.GetHorizontalPosition() + Me.GetCurrentSpeed())
End Sub
Java
public void IncreaseSpeed(){
VerticalPosition = VerticalPosition + VerticalChange;
if(VerticalPosition > MaxHeight){
VerticalPosition = MaxHeight;
}
super.SetCurrentSpeed(super.GetCurrentSpeed() + super.GetIncreaseAmount());
if(super.GetCurrentSpeed() > super.GetMaxSpeed()){
super.SetCurrentSpeed(super.GetMaxSpeed());
}
super.SetHorizontalPosition(super.GetHorizontalPosition() + super.GetCurrentSpeed());
}
2(b)(ii) Python
def IncreaseSpeed(self):
self.__VerticalPosition = self.__VerticalPosition + self.__VerticalChange
if(self.__VerticalPosition > self.__MaxHeight):
self.__VerticalPosition = MaxHeight
Vehicle.SetCurrentSpeed(self, Vehicle.GetCurrentSpeed(self) +
Vehicle.GetIncreaseAmount(self))
if(Vehicle.GetCurrentSpeed(self) > Vehicle.GetMaxSpeed(self)):
Vehicle.SetCurrentSpeed(self, Vehicle.GetMaxSpeed(self));
Vehicle.SetHorizontalPosition(self, Vehicle.GetHorizontalPosition(self) +
Vehicle.GetCurrentSpeed(self))
2(c) 1 mark each to max 3 3
Suitable method/procedure heading (and end where appropriate) and outputting horizontal position and current speed
in an appropriate message
Checking if object is a Vehicle or Helicopter // overriding methods in each class for output // one method in each class
// try except …
…outputting vertical position only if helicopter with appropriate message
Example program code:
VB.NET
Sub OutputCurrentPosition(ObjectToOutput)
Console.WriteLine("Current position = " & ObjectToOutput.GetHorizontalPosition())
Console.WriteLine("Current speed = " & ObjectToOutput.GetCurrentSpeed())
If TypeOf ObjectToOutput Is Helicopter Then
Console.WriteLine("Current vertical position = " &
ObjectToOutput.GetVerticalPosition())
End If
End Sub
Java
public void OutputCurrentPosition(){
System.out.println("Current position = " + HorizontalPosition);
System.out.println("Current speed = " + CurrentSpeed);
}
public void OutputCurrentPosition(){
System.out.println("Current position = " +super.GetHorizontalPosition());
System.out.println("Current speed = " + super.GetCurrentSpeed());
System.out.println("Current vertical position = " + VerticalPosition);
}
Python
def OutputCurrentPosition(self):
print("Current position = ", self.__HorizontalPosition)
print("Current speed = ", self.__CurrentSpeed)
2(c) def OutputCurrentPosition(self):
print("Current position = ", Vehicle.GetHorizontalPosition(self))
print("Current speed = ", Vehicle.GetCurrentSpeed(self))
print("Current verticalposition = ", self.__VerticalPosition)
2(d)(i) 1 mark each 5
Instantiating an object of type Vehicle with correct parameters ("Tiger", 100, 20)
Instantiating an object of type Helicopter with correct parameters ("Lion", 350, 40, 3, 100)
Calling IncreaseSpeed() twice for the car
Calling IncreaseSpeed() twice for the helicopter
Calling the output for both objects
Example program code:
VB.NET
Sub Main()
Dim Car As Vehicle
Car = New Vehicle("Tiger", 100, 20)
Dim Heli1 As Helicopter
Heli1 = New Helicopter("Lion", 350, 40, 3, 100)
Car.IncreaseSpeed()
Car.IncreaseSpeed()
OutputCurrentPosition(Car)
Console.WriteLine("")
Heli1.IncreaseSpeed()
Heli1.IncreaseSpeed()
OutputCurrentPosition(Heli1)
End Sub
Java
public static void main(String args[]){
Vehicle Car = new Vehicle("Tiger", 100, 20);
Helicopter Heli1 = new Helicopter("Lion", 350, 40, 3, 100);
Car.IncreaseSpeed();
Car.IncreaseSpeed();
Car.OutputCurrentPosition();
System.out.println("");
Heli1.IncreaseSpeed();
Heli1.IncreaseSpeed();
Heli1.OutputCurrentPosition();
}
2(d)(i) Python
#main
Car = Vehicle("Tiger", 100, 20)
Heli1 = Helicopter("Lion", 350, 40, 3, 100)
Car.IncreaseSpeed()
Car.IncreaseSpeed()
Car.OutputCurrentPosition()
print("")
Heli1.IncreaseSpeed()
Heli1.IncreaseSpeed()
Heli1.OutputCurrentPosition()
2(d)(ii) Screenshot of results e.g. 1