0% found this document useful (0 votes)
16 views7 pages

Oop Questions

The document outlines a practice exercise for object-oriented programming, focusing on creating classes for characters in a computer game. It includes specifications for the Character and BikeCharacter classes, detailing their attributes, constructors, and methods for movement and intelligence management. Additionally, it introduces a MagicCharacter class that inherits from Character, emphasizing the impact of elemental attributes on learning capabilities.

Uploaded by

Getrude Chivige
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Oop Questions

The document outlines a practice exercise for object-oriented programming, focusing on creating classes for characters in a computer game. It includes specifications for the Character and BikeCharacter classes, detailing their attributes, constructors, and methods for movement and intelligence management. Additionally, it introduces a MagicCharacter class that inherits from Character, emphasizing the impact of elemental attributes on learning capabilities.

Uploaded by

Getrude Chivige
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Oriented Practice Exercise

Answer all questions

1. A computer game is written using object-oriented programming.

The game has multiple characters that can move around the screen.

(a) 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

returns the X position


GetXPosition()
returns the Y position
GetYPosition()
adds the parameter to the X position validates that the new
SetXPosition() 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

(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. [4]

(ii) The get methods GetXPosition() and GetYPosition() each return the relevant

attribute.

Write program code for the get methods. [3]


2

(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. [4]

(b) 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(). [4]

(v) 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.

[2]

(c) (i)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
3

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. [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(). [2]

(iii) 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.
4

2. A computer game is written using object-oriented programming.

The game has multiple characters.

(a) The class Character stores data about the game characters. Each character has a name, date of
birth, intelligence value and speed value.

Character

CharacterName : STRING stores the name of the character

DateOfBirth : DATE stores the date of birth of the character

Intelligence : REAL stores the intelligence value of the character

Speed : INTEGER stores the speed value of the character

initialises CharacterName, DateOfBirth, Intelligence and


Constructor()
Speed to the parameter values
SetIntelligence()
assigns the value of the parameter to Intelligence
GetIntelligence()
returns the value of Intelligence
GetName()
returns the name of the character
ReturnAge()
calculates and returns the age of the character as an integer
Learn()
increases the value of Intelligence by 10%

(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. [5]

(ii) The get methods GetIntelligence() and GetName() return the attribute values.

Write program code for the methods GetIntelligence() and GetName(). [3]

(iii) The method SetIntelligence() assigns the value of its parameter to the attribute.

Write program code for SetIntelligence(). [2]

(iv ) The method Learn() increases the current value of Intelligence by 10%.
5

Write program code for Learn(). [1]

(v) The method ReturnAge() calculates and returns the age of the character in years as an integer.

Assume that the current year is 2023 and only use the year from the date of birth for the
calculation. For example, the method returns 18 if the character was born on any date in 2005.

Write program code for the method ReturnAge(). [2]

(b) (i) Write program code to create a new instance of Character with the identifier FirstCharacter.

The name of the character is Royal, date of birth is 1 January 2019, intelligence is 70 and speed is
30. [2]

(ii) Write program code to call the method Learn() for the character created in part 3(b)(i).

Output the name, age and intelligence of the character in an appropriate message. [3]

(iii) Test your program.

Take a screenshot of the output. [1]

(c) The class MagicCharacter inherits from the class Character. A magic character has an element,
for example, water. This element changes how they learn. The magic character’s element is
stored in the additional attribute Element.

MagicCharacter

Element : STRING stores the element for the character

Constructor() takes Element, CharacterName, DateOfBirth, Intelligence and


Speed as parameters

calls its parent class constructor with the appropriate values


initialises Element to its parameter value

Learn() alters the intelligence of the character depending on the


character’s element

(i) Write program code to declare the class MagicCharacter and its constructor.

Do not declare the other method.


6

Use your programming language’s appropriate constructor.

If you are writing in Python, include attribute declarations using comments. [5]

() The method Learn() overrides the parent class method and increases the intelligence depending on
the character’s element.

If the element is fire or water, intelligence increases by 20%.

If the element is earth, intelligence increases by 30%.

If the element is not fire, water or earth the intelligence increases by 10%.

Write program code for Learn(). [3]


7

(i) Write program code to create a new instance of MagicCharacter with the identifier
FirstMagic.

The name of the character is Light, date of birth is 3 March 2018, intelligence is 75, speed
is 22 and element is fire.

Save your program.

Copy and paste the program code into part 3(d)(i) in the evidence document.

[2]

Write program code to call the method Learn() for the character created in part 3(d)(i).

Output the name, age and intelligence of the character in an appropriate message.

Save your program.

Copy and paste the program code into part 3(d)(ii) in the evidence document.

[1]

Test your program.

Take a screenshot of the output. [1]

The end

You might also like