Practical OOP
Practical OOP
Practical OOP
Each card has a number and a colour. The cards are saved in the text file CardValues.txt
The program has a class named Card. The class has the following attributes and methods.
Card
Number : INTEGER The number of the card
(a) The constructor takes the number and colour of the card as parameters and sets them to the
private attributes.
Write program code to declare the class Card and its constructor. Do not write any other
methods.
All attributes should be private. If you are writing in Python, include attribute declarations
using comments.
Copy and paste the program code into part 3(a) in the evidence document.
[5]
Write program code for the get methods GetNumber() and GetColour().
Copy and paste the program code into part 3(b) in the evidence document.
[3]
1 is the number
red is the colour.
A 1D array of type Card is declared to store all the cards read in from CardValues.txt
Copy and paste the program code into part 3(c) in the evidence document.
[7]
(d) The program needs to allow all players (maximum of 5) to select 4 cards from the 30 available.
A card can only be selected once, so the program needs to record which cards have already
been selected.
Amend the program to store which cards have already been selected and write program
code for the function ChooseCard().
Copy and paste the program code into part 3(d) in the evidence document.
[6]
(e) The main program needs to allow one player to select all their 4 cards.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[5]
Page-2
9
Test 1: 1 5 9 10
Test 2: 2 2 3 4 4 5
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[1]
Page-3
2 A computer game is being developed using object-oriented programming.
One element of the game is a balloon. This is designed as the class Balloon.
Balloon
Health : INTEGER The health of the balloon
(a) The constructor takes the name of the defence item and the balloon’s colour as parameters
and sets these to the attributes. The health is initialised to 100.
Write program code to declare the class Balloon and its constructor. Do not write any other
methods.
All attributes should be private. If you are writing in Python include attribute declarations using
comments.
Copy and paste the program code into part 2(a) in the evidence document.
[5]
(b) The get method GetDefenceItem() returns the defence item of the object.
Copy and paste the program code into part 2(b) in the evidence document.
[2]
9618/41/M/J/22
Page-4
(c) The object’s method ChangeHealth() takes an integer number as a parameter and adds
this to the health attribute of the object.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) The object’s method CheckHealth() returns TRUE if the health of the object is 0 or less (no
health remaining) and returns FALSE otherwise (health remaining).
Copy and paste the program code into part 2(d) in the evidence document.
[2]
Copy and paste the program code into part 2(e) in the evidence document.
[3]
Copy and paste the program code into part 2(f) in the evidence document.
[8]
(g) (i) Amend the main program to call the function Defend().
Copy and paste the program code into part 2(g)(i) in the evidence document.
[2]
Copy and paste the screenshot into part 2(g)(ii) in the evidence document.
[1]
3 A program, written using object-oriented programming, stores pictures as objects.
The program stores the dimensions of the picture (width and height), the colour of the frame (e.g.
black), and a description of the picture (e.g. flowers).
Picture
Description : STRING // stores a description of the picture
Width : INTEGER // stores the width e.g. 30
Height : INTEGER // stores the height e.g. 40
FrameColour : STRING // stores the colour e.g. black
(a) The constructor takes the picture description, frame colour, height, and width as parameters
and sets these to the private attributes.
Write the program code to declare the class Picture and its constructor.
Do not write any other methods.
If you are writing in Python programming language, include attribute declarations using
comments.
Copy and paste the program code into part 2(a) in the evidence document.
[5]
(b) The four get methods return the associated attribute, for example, GetDescription()
returns the description of the picture.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
9618/42/O/N/21
(c) The method SetDescription() takes a new description as a parameter, and writes this
value to the appropriate attribute.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) Write program code to declare an array of type Picture with 100 elements.
Copy and paste the program code into part 2(d) in the evidence document.
[1]
(e) The text file Pictures.txt stores the data for the pictures in the order: description, width,
height, colour.
The data read into the program from the text file is stored in an array of type Picture.
The main program and the function will need to access the array data.
Copy and paste the program code into part 2(e) in the evidence document.
[8]
Copy and paste the program code into part 2(f) in the evidence document.
[2]
(g) The main program needs to ask the user to input their requirements for a picture. The user
will enter the colour of the frame, the maximum width, and the maximum height of the picture.
The program will then search the array of pictures, and output the picture description, the
width, and the height of any picture that meets the user’s requirements.
The program should allow the user to input the colour in any case (e.g. Silver, silver, or
SILVER), and still output the correct results.
Copy and paste the program code into part 2(g) in the evidence document.
[7]
Copy and paste the screenshots into part 2(h) in the evidence document.
[2]
4 A computer game requires users to travel around a world to find and open treasure chests. Each
treasure chest has a mathematics question inside. The user enters the answer. The number of
points awarded depends on the number of attempts before the user gives the correct answer.
TreasureChest
question : STRING // stores the question
answer : INTEGER // stores the answer
points : INTEGER // stores the maximum possible number of
points available for this chest
constructor() // takes question, answer and points as
parameters and creates an instance of an
object
If you are using the Python programming language, include attribute declarations using
comments.
Copy and paste the program code into part 3(a) in the evidence document.
[5]
9618/42/M/J/21
(b) The text file TreasureChestData.txt stores data for five questions, in the order of
question, answer, points.
For example, the first three lines of the file are for the first question:
2*2 question
4 answer
10 points
• read each question, answer and points from the text file
• create an object of type TreasureChest for each question
• declare an array named arrayTreasure of type TreasureChest
• append each object to the array
• use exception handling to output an appropriate message if the file is not found.
Copy and paste the program code into part 3(b) in the evidence document.
[8]
(c) The main program repeats each question until the user inputs the correct answer. The
number of points awarded depends on the number of attempts before the user gives the
correct answer.
(i) The class TreasureChest has a method getQuestion() that returns the question.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[1]
(ii) The class TreasureChest has a method checkAnswer() that takes the user’s
answer as a parameter. It returns True if the answer is correct and False otherwise.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[3]
(iii) The class TreasureChest has a method getPoints() that takes the number of
attempts as a parameter.
For example, a question is worth 100 points and the user took 2 attempts to give the
correct answer. The user is awarded 50 points (100 DIV 2).
Copy and paste the program code into part 3(c)(iii) in the evidence document.
[5]
(iv) Write program code for the main program to:
Copy and paste the program code into part 3(c)(iv) in the evidence document.
[7]
Take a screenshot showing the input(s) and output(s) for each of the following two tests.
Copy and paste the screenshots into part 3(c)(v) in the evidence document.
[2]
Page-5