Question
A computer game is written using object-oriented programming.
The game has multiple characters that can move around the screen.
The class Character stores data about the characters. Each character has a name, a current X
(horizontal) position and a current Y (vertical) position.
Character
Name : STRING stores the name of the character as a string
XPosition : INTEGER stores the X position as an integer
YPosition : INTEGER stores the Y position as an integer
Constructor() initialises Name, XPosition and YPosition to its parameter values
GetXPosition() returns the X position
GetYPosition() returns the Y position
SetXPosition() adds the parameter to the X position validates that the new X position is between 0 and 10 000 inclusive
SetYPosition() adds the parameter to the Y position validates that the new Y position is between 0 and 10 000 inclusive
Move() takes a direction as a parameter and calls either
SetXPosition or SetYPosition with an integer value
(a) (i) Write program code to declare the class Character and its constructor.
Do not declare the other methods.
Use your programming language’s appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_N23.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[4]
(ii) The get methods GetXPosition() and GetYPosition() each return the relevant attribute.
Write program code for the get methods.
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
(iii) The set methods SetXPosition() and SetYPosition() each take a value as a parameter and add this to the current X or Y position.
If the new value exceeds 10 000, it is limited to 10 000.
If the new value is below 0, it is limited to 0.
Write program code for the set methods.
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[4]
(iv) The method Move() takes a string parameter: "up", "down", "left" or "right".
The table shows the change each direction will make to the X or Y position.
Use the appropriate method to change the position value.
Direction Value change up Y position + 10 down Y position − 10 left X position − 10 right X position + 10
Write program code for Move().
Save your program.
Copy and paste the program code into part 3(a)(iv) in the evidence document.
[4]
(b) Write program code to declare a new instance of Character with the identifier Jack.
The starting X position is 50 and the starting Y position is 50, the character’s name is Jack.
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The class BikeCharacter inherits from the class Character.
BikeCharacter
Constructor() takes Name, XPosition and YPosition as parameters calls its parent class constructor with the appropriate values
Move() overrides the method Move() from the parent class by changing either the X position or the Y position by 20 instead of 10
(i) Write program code to declare the class BikeCharacter and its constructor.
Do not declare the other method.
Use your programming language’s appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[3]
(ii) The method Move() overrides the method from the parent class.
The table shows the change each direction will make to the X or Y position.
Direction Value change
up Y position + 20 down Y position − 20 left X position − 20 right X position + 20
Write program code for Move().
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[2]
(d) Write program code to declare a new instance of BikeCharacter with the identifier Karla.
The starting X position is 100, the starting Y position is 50 and the character’s name is Karla.
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[1]
(e) (i) Write program code to:
• take as input which of the two characters the user would like to move
• take as input the direction the user would like the character to move
• call the appropriate method to move the character
• output the character’s new X and Y position in an appropriate format, for example:
"Karla's new position is X = 100 Y = 200"
All inputs require appropriate prompts and must be validated.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[5]
(ii) Test your program twice with the following inputs.
Test 1: jack right
Test 2: karla down
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[2]
Mark scheme
3(a)(i) One mark each to max 4 4
• Class header (and end where appropriate)
• Three attributes with correct names and data types
• Constructor header (and end where appropriate) with 3 parameters
• Within constructor, assigns attributes to parameters
Example program code:
Java
class Character{
private Integer XPosition;
private Integer YPosition;
private String Name;
public Character(Integer XPositionP, Integer YPositionP, String NameP){
XPosition = XPositionP;
YPosition = YPositionP;
Name = NameP;
}
}
VB.NET
Class Character
Private XPosition As Integer
Private YPosition As Integer
Private Name As String
Sub New(XPositionP, YPositionP, NameP)
XPosition = XPositionP
YPosition = YPositionP
Name = NameP
End Sub
End Class
3(a)(i) Python
class Character:
#self.XPosition integer
#self.YPosition integer
#self.Name string
def init (self, XPositionP, YPositionP, NameP):
self.XPosition = XPositionP
self.YPosition = YPositionP
self.Name = NameP
3(a)(ii) One mark each 3
• 1 get header with no parameter …
• … returning correct value
• 2nd get method
Example program code:
Java
public Integer GetXPosition(){
return XPosition;
}
public Integer GetYPosition(){
return YPosition;
}
VB.NET
Function GetXPosition()
Return XPosition
End Function
Function GetYPosition()
Return YPosition
End Function
Python
def GetXPosition(self):
return self. XPosition
def GetYPosition(self):
return self. YPosition
3(a)(iii) One mark each to max 4 4
• 1 set method header (and end where appropriate) with parameter …
• … adding parameter to X/Y Position attribute and storing in the X/Y attribute
• If (resulting value is) more than 10 000 limiting to 10 000 and if less than 0 limiting to 0
• Second correct set method
Example program code:
Java
public void SetXPosition(Integer Value){
XPosition = XPosition + Value;
if(XPosition > 10000){
XPosition = 10000;
}else if(XPosition < 0){
XPosition = 0;
}
}
public void SetYPosition(Integer Value){
YPosition = YPosition + Value;
if(YPosition > 10000){
YPosition = 10000;
}else if(YPosition < 0){
YPosition = 0;
}
}
VB.NET
Function SetXPosition(Value)
XPosition = XPosition + Value
If XPosition > 10000 Then
XPosition = 10000
3(a)(iii) ElseIf XPosition < 0 Then
XPosition = 0
End If
End Function
Function SetYPosition(Value)
YPosition = YPosition + Value
If YPosition > 10000 Then
YPosition = 10000
ElseIf YPosition < 0 Then
YPosition = 0
End If
End Function
Python
def SetXPosition(self, Value):
self. XPosition = self. XPosition + Value
if(self.XPosition > 10000):
self.XPosition = 10000
elif self.XPosition < 0:
self.XPosition = 0
def SetYPosition(self, Value):
self.YPosition = self.YPosition + Value
if(self.YPosition > 10000):
self.YPosition = 10000
elif self.YPosition < 0:
self.YPosition = 0
3(a)(iv) One mark each 4
• Method header with (string) parameter
• Checking parameter for direction …
• … using SetYPosition() and SetXPosition() correctly …
• … with correct parameters
Example program code:
Java
public void Move(String Direction){
if(Direction.equals("up")){
SetYPosition(10);
}else if(Direction.equals("down")){
SetYPosition(-10);
}else if(Direction.equals("right")){
SetXPosition(10);
}else{
SetXPosition(-10);
}
}
VB.NET
Overridable Sub Move(Direction)
If Direction = "up" Then
SetYPosition(10)
ElseIf Direction = "down" Then
SetYPosition(-10)
ElseIf Direction = "right" Then
SetXPosition(10)
ElseIf Direction = "left" Then
SetXPosition(-10)
End If
End Sub
3(a)(iv) Python
def Move(self, Direction):
if(Direction == "up"):
self.SetYPosition(10)
elif(Direction == "down"):
self.SetYPosition(-10)
elif(Direction == "right"):
self.SetXPosition(10)
else:
self.SetXPosition(-10)
3(b) One mark each 2
• New instance of Character created with identifier Jack …
• … correct constructor called and values passed
Example program code:
Java
Character Jack = new Character(50, 50, "Jack");
VB.NET
Dim Jack As Character = New Character(50, 50, "Jack")
Python
Jack = Character(50, 50, "Jack")
3(c)(i) One mark each 3
• Class header inheriting from Character
• Constructor taking all 3 parameters …
• … calling parent/super constructor with the 3 parameters
Example program code:
Java
class BikeCharacter extends Character{
public BikeCharacter(Integer XPositionP, Integer YPositionP, String NameP){
super(XPositionP, YPositionP, NameP);
}
}
VB.NET
Class BikeCharacter
Inherits Character
Sub New(XPositionP, YPositionP, NameP)
MyBase.New(XPositionP, YPositionP, NameP)
End Sub
End Class
Python
class BikeCharacter(Character):
def init (self, XPositionP, YPositionP, NameP):
super(). init (XPositionP, YPositionP, NameP)
3(c)(ii) One mark each 2
• Method header taking parameter and overriding parent/super Move()
• Correct changes to method to update values by 20
Example program code:
Java
public void Move(String Direction){
if(Direction.equals("up")){
super.SetYPosition(20);
}else if(Direction.equals("down")){
super.SetYPosition(-20);
}else if(Direction.equals("right")){
super.SetXPosition(20);
}else{
super.SetXPosition(-20);
}
}
VB.NET
Overrides Sub
Move(Direction) If
Direction = "up" Then
SetYPosition(20)
ElseIf Direction = "down" Then
SetYPosition(-20)
ElseIf Direction = "right" Then
SetXPosition(20)
ElseIf Direction = "left" Then
SetXPosition(-20)
End If
End Sub
3(c)(ii) Python
def Move(self, Direction):
if(Direction == "up"):
super().SetYPosition(20)
elif(Direction == "down"):
super().SetYPosition(-20)
elif(Direction == "right"):
super().SetXPosition(2)
else:
super().SetXPosition(-20)
3(d) One mark each 1
• Declaring new BikeCharacter with correct values e.g.
Java
BikeCharacter Karla = new BikeCharacter(100, 50, "Karla");
VB.NET
Dim Karla As BikeCharacter = New BikeCharacter(100, 50, "Karla")
Python
Karla = BikeCharacter(100, 50, "Karla")
3(e)(i) One mark each to max 5 5
• Reading in both values (character and direction) with appropriate prompts
• Character name is validated as e.g. Jack/Karla, and direction is validated as e.g. up/down/left/right
• Calling Move() for the character input, with direction input as a parameter
• Outputting character's new X and Y position in a suitable format …
• … using get methods
Example program code:
Java
System.out.println("Would you like to move Jack or Karla?");
CharacterToMove = (scanner.nextLine()).toLowerCase();
while(CharacterToMove.equals("jack") == false &&
CharacterToMove.equals("karla") == false){
System.out.println("Invalid, try again");
CharacterToMove = (scanner.nextLine()).toLowerCase();
}
System.out.println("Which direction? Up, down, left or right?");
Direction = (scanner.nextLine()).toLowerCase();
while(Direction.equals("up") == false && Direction.equals("down") == false
&& Direction.equals("left") == false && Direction.equals("right")== false){
System.out.println("Invalid, try again");
Direction = (scanner.nextLine()).toLowerCase();
}
if(CharacterToMove.equals("jack")){
Jack.Move(Direction);
System.out.println("Jack's new position is X = "
+ Jack.GetXPosition() + " Y = " + Jack.GetYPosition());
}else{
Karla.Move(Direction);
System.out.println("Karla's new position is " +
Karla.GetXPosition()
+ " " + Karla.GetYPosition());
}
3(e)(i) VB.NET
Console.WriteLine("Would you like to move Jack or Karla?")
CharacterToMove = Console.ReadLine.ToLower()
While CharacterToMove <> "jack" And CharacterToMove <> "karla"
Console.WriteLine("Invalid try again")
CharacterToMove = Console.ReadLine
End While
Console.WriteLine("Which direction? Up, down, left or right")
Direction = Console.ReadLine.ToLower()
While Direction <> "up" And Direction <> "down" And Direction <> "left" And Direction <>
"right"
Console.WriteLine("Invalid try again")
Direction = Console.ReadLine
End While
If CharacterToMove = "jack"
Then Jack.Move(Direction)
Console.WriteLine("Jack's new position is X = " & Jack.GetXPosition & " Y = " &
Jack.GetYPosition)
Else
Karla.Move(Direction)
Console.WriteLine("Karla's new position is X = " & Karla.GetXPosition & " Y = " &
Karla.GetYPosition)
End If
Console.WriteLine("Would you like to Continue? Enter True to continue, or anything else to
quit")
3(e)(i) Python
CharacterToMove = input("Would you like to move Jack or Karla?").lower()
while CharacterToMove != "jack" and CharacterToMove != "karla":
CharacterToMove = input("Invalid try again")
Direction = input("Which direction? Up, down, left or right?")
while Direction != "up" and Direction != "down" and Direction != "left" and Direction !=
"right":
Direction = input("Invalid try again")
if CharacterToMove == "jack":
Jack.Move(Direction)
print("Jack's new position is X =", Jack.GetXPosition(), "Y =", Jack.GetYPosition())
else:
Karla.Move(Direction)
print("Karla's new position is X =", Karla.GetXPosition(), "Y =", Karla.GetYPosition())
3(e)(ii) One mark for each test 2