Further programming: paradigms, OOP, files and exceptions – A Level Computer Science (9618)
60 questions · page 4 of 4
Question text
10 (a) Define these Object-Oriented Programming (OOP) terms:
Encapsulation …
…
Getter …
…
Setter …
…
[3]
(b) A school has a program written using OOP to maintain its staff and student records.
The object SubstituteTeacher allows the details of the school’s substitute teachers to be
stored. This includes their full name, telephone number and whether or not they are in school
today. For example:
SubName
Sarah Jones
Telephone
01223658721
InSchool
TRUE
Complete the diagram for the object SubstituteTeacher, including appropriate properties
and their getters and setters.
SubstituteTeacher
SubName : STRING
…
InSchool : BOOLEAN
…
SetTelephone(Tel : STRING)
…
GetSubName()
…
…
[3]
Question text
2
A computer program is being developed that uses a set of cards. The program is written using
object-oriented programming.
The program has two classes: Card and Hand.
The methods and attributes of these classes are shown:
Card
Number : INTEGER
stores the card number from 1 to 5 inclusive
Colour : STRING
stores the card colour: red, blue or yellow
Constructor()
takes a number and colour as parameters and sets
the private values to these parameters
GetNumber()
returns the card number
GetColour()
returns the card colour
Hand
Cards : ARRAY[0:9] OF Card
1D array of type Card
FirstCard : INTEGER
stores the position of the first card in the hand
NumberCards : INTEGER
stores the number of cards in the hand
Constructor()
takes five card objects as parameters, assigns each
card to the array Cards[], initialises FirstCard to 0
and NumberCards to 5
GetCard()
takes an index as a parameter and returns the card at
that index in the array
(a) (i)
Write program code to declare the class Card, its attributes and constructor.
Do not write program code for the get methods.
Use your programming language appropriate constructor.
All attributes must be private. If you are writing in Python, include attribute declarations
using comments.
Save your program as Question2_N22.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[5]
(ii)
Write program code for the class methods GetNumber() and GetColour().
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
(iii)
The program is tested with the following cards:
Number
Colour
1
red
2
red
3
red
4
red
5
red
1
blue
2
blue
3
blue
4
blue
5
blue
1
yellow
2
yellow
3
yellow
4
yellow
5
yellow
Write program code to declare each of these cards as a variable of type Card in the
main program.
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[2]
(b) (i)
Write program code to declare the class Hand, its attributes and constructor.
Do not write the get methods.
Use your programming language 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.
[6]
(ii)
The get method GetCard() takes an index as a parameter and returns the card stored
at that index in the array.
Write program code for the method GetCard().
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[2]
(iii)
Two players are declared with 5 cards each.
Player 1 has the cards: 1 red, 2 red, 3 red, 4 red, 1 yellow.
Player 2 has the cards: 2 yellow, 3 yellow, 4 yellow, 5 yellow, 1 blue.
Write program code to declare player 1 and player 2 as objects of type Hand, with the
cards indicated.
Save your program.
Copy and paste the program code into part 2(b)(iii) in the evidence document.
[2]
(c) The function CalculateValue() takes a player’s hand as a parameter and returns a score
calculated using the following rules:
•
If a card is red, 5 points are added to the player’s score.
•
If a card is blue, 10 points are added to the player’s score.
•
If a card is yellow, 15 points are added to the player’s score.
•
The number of each card in the hand is added to the player’s score.
(i)
Write program code for the function CalculateValue().
Assume that there are only 5 cards in the player’s hand in this function.
Save your program.
Copy and paste the program code into part 2(c)(i) in the evidence document.
[6]
(ii)
Amend the main program by writing program code to use the function
CalculateValue() for each of the two players. The player with the highest score wins.
Output an appropriate message to identify the winning player, or if the game was a draw
(both players have the same number of points).
Save your program.
Copy and paste the program code into part 2(c)(ii) in the evidence document.
[4
Question text
2
A computer game is being developed. The game has 10 different characters that are all active in
the game.
Part of the game is being written using object-oriented programming.
The class Character stores data about the characters. Each character has a name and the x
coordinate and y coordinate of their current position.
Character
Name : STRING
stores the name of the character
XCoordinate : INTEGER
stores the x coordinate
YCoordinate : INTEGER
stores the y coordinate
Constructor()
initialises Name, XCoordinate and YCoordinate from the
values passed as parameters
GetName()
returns the name of the character
GetX()
returns the x coordinate of the character
GetY()
returns the y coordinate of the character
ChangePosition()
takes XChange as an integer parameter and adds it to the
x coordinate
takes YChange as an integer parameter and adds it to the
y coordinate
(a) Write program code to declare the class Character and its constructor. Do not write
program code for the other methods.
Use your programming language appropriate constructor.
All attributes must be private. If you are writing in Python, include attribute declarations using
comments.
Save your program as Question2_N22.
Copy and paste the program code into part 2(a) in the evidence document.
[4]
(b) Write program code for the three get methods for the class Character.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) Write program code for the method ChangePosition().
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) The main program has a 1D array of characters. Each character is stored as an object of type
Character.
The game has a maximum of 10 characters. The character names, x coordinates and y
coordinates are stored in the file Characters.txt in the order:
•
name
•
x coordinate
•
y coordinate.
For example, the first character in the file is named Amal, with the x coordinate 0 and the y
coordinate 2.
Amend the main program by writing program code to:
•
declare the array
•
read in all 10 characters from Characters.txt
•
store each character as an object in the array.
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[7]
(e) The main program needs to read in a character’s name from the user, search for the character
in the array and store the index of its position. It repeats until the user enters a name that
exists in the array.
Amend the main program by writing program code to perform this task.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[5]
(f)
The user will enter a letter to identify the direction the chosen character from part 2(e) should
move.
•
If ‘A’ is input, the character moves left (x coordinate minus 1).
•
If ‘W’ is input, the character moves up (y coordinate plus 1).
•
If ‘S’ is input, the character moves down (y coordinate minus 1).
•
If ‘D’ is input, the character moves right (x coordinate plus 1).
Amend the main program by writing program code to:
•
take a letter as input until it is a valid move (A, W, S or D)
•
change the position of the character using the appropriate method.
Save your program.
Copy and paste the program code into part 2(f) in the evidence document.
[7]
(g) (i)
When a change to a character’s position has been made, the program needs to output
the character’s name and the new x and y coordinates of the character, in the format:
Qui has changed coordinates to X = 83 and Y = 0
Amend the main program by writing program code to perform these tasks.
Save your program.
Copy and paste the program code into part 2(g)(i) in the evidence document.
[2]
(ii)
Test your program by inputting the following four items of data in the order given:
THOMAS
qui
X
A
Take a screenshot of the output.
Copy and paste the screenshot into part 2(g)(ii) in the evidence document.
[1]
Question text
2
A declarative language is used to represent the following facts about cats.
01
type(leopard, wild).
02
type(lion, wild).
03
type(cheetah, wild).
04
type(savannah, hybrid).
05
type(persian, domestic).
06
07
hair(leopard, medium).
08
hair(lion, short).
09
hair(cheetah, medium).
10
hair(savannah, medium).
11
hair(persian, long).
12
13
spots(leopard, yes).
14
spots(lion, no).
15
spots(cheetah, yes).
16
spots(savannah, yes).
17
spots(persian, no).
These clauses have the following meaning:
Clause
Meaning
01
A leopard is a type of wild cat.
08
A lion has short hair.
16
A savannah has spots.
(a) More facts are to be included. A caracal is a wild cat with short hair.
Write the additional clauses to record these facts.
18 …
19 …
[2]
(b) Using the variable Cat, the goal:
hair(Cat, medium)
returns
Cat = leopard, cheetah, savannah
Write the result returned by the goal:
hair(Cat, long)
Cat = … [1]
(c) (i)
Write the goal, using the variable Pet, to find all the domestic cats.
…
… [1]
(ii)
Write the goal, using the variable WildSpotty, to find all the wild cats with spots.
…
…
…
… [2]
Question text
7
A program is to be written using Object-Oriented Programming (OOP) for a shop that sells knitting
yarn. There are three types of yarn: acrylic, wool or mix.
The following data are stored for each type.
•
Name
•
Colour
•
Batch code
•
Weight
•
Number of balls of yarn in stock (can be edited)
•
Type of yarn
The following statements apply to yarn.
•
Acrylic can be soft or not soft.
•
Wool can be lamb, merino or alpaca.
•
Mix contains a percentage of acrylic.
Each type of yarn has a method that will display all the information about the yarn.
(a) Complete this class inheritance diagram to show the properties, methods and inheritance.
Yarn
Name: STRING
Colour: STRING
BatchCode: STRING
Weight: INTEGER
NumberBalls: INTEGER
Type: STRING
Constructor()
EditNumberBalls()
YarnInfo()
Acrylic
Wool
Mix
Percentage: INTEGER
Constructor()
…
…
YarnInfo()
Constructor()
Constructor()
…
…
[5]
(b) Describe what is meant by the terms properties, methods and inheritance.
Properties …
…
…
…
Methods …
…
…
…
Inheritance …
…
…
…
[6]
Question text
1
Data types can be defined using pseudocode.
The data type, BuildingRecord, is defined in pseudocode as:
TYPE BuildingRecord
DECLARE BuildingID : INTEGER
DECLARE BuildingGroup : STRING
DECLARE OwnerName : STRING
DECLARE BuildingAddress : STRING
DECLARE DateLastSold : DATE
DECLARE PriceLastSold : REAL
ENDTYPE
A variable, BuildingRegister, is declared in pseudocode as:
DECLARE BuildingRegister : BuildingRecord
(a) Write pseudocode statements to assign:
•
1067 to the BuildingID of BuildingRegister
•
house to the BuildingGroup of BuildingRegister
…
…
…
… [2]
(b) The type definition for BuildingRecord is changed. The data type for BuildingGroup is
changed to an enumerated type, BuildingType, with values of house, bungalow, apartment
and farm.
(i)
Write the type declaration for BuildingType in pseudocode.
…
…
…
… [2]
(ii)
Write the new declaration for BuildingGroup in pseudocode.
…
… [1]
(iii)
Write the new pseudocode statement to assign house to BuildingGroup of
BuildingRegister.
…
… [1]
(c) The program is to be rewritten using Object-Oriented Programming (OOP). The data type
BuildingRecord is to be changed to a class, BuildingClass.
The properties for BuildingClass are BuildingID, BuildingGroup, OwnerName,
BuildingAddress, DateLastSold and PriceLastSold.
All the properties are set to PRIVATE, for example:
PRIVATE PriceLastSold : REAL
(i)
Write the declaration in pseudocode for OwnerName as PRIVATE.
������������������������������������������������������������������������������������������������������������������������������������������������������������
����������������������������������������������������������������������������������������������������������������������������������������������������� [1]
(ii)
Explain why the properties have been set to PRIVATE.
������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������
��������������������������������������������������������������
Question text
2
A declarative language is used to represent the following facts about a school.
01
teaches(alan, mathematics).
02
teaches(ioana, geography).
03
teaches(nina, history).
04
teaches(alan, statistics).
05
06
studies(ahmed, history).
07
studies(freya, history).
08
studies(kim, history).
09
studies(freya, geography).
10
studies(hua, mathematics).
11
studies(hua, statistics).
12
studies(hua, geography).
13
14
tutors(alan,
kim).
15
tutors(alan,
hua).
16
tutors(alan,
freya).
17
tutors(nina,
ahmed).
These clauses have the following meaning:
Clause
Meaning
01
Alan teaches mathematics.
06
Ahmed studies history.
14
Alan is Kim’s tutor.
(a) More facts are to be included. Sam studies history and Nina is his tutor.
Write the additional clauses to record these facts.
18 …
19 …
[2]
(b) Using the variable Student, the goal:
studies(Student, history)
returns
Student = freya, ahmed, kim
Write the result returned by the goal:
studies(Student, geography)
Student = … [1]
(c) Write the goal, using the variable X, to find all the students who have a tutor that teaches
them. For example, Hua has Alan for a tutor and is also taught mathematics by Alan.
…
…
…
… [4]
Question text
9
State the reasons for including exception handling routines when writing a program.
Include an example of an exception in your answer.
…
…
…
…
…
…
…
… [4]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
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.
The class has the following attributes and methods.
Balloon
Health : INTEGER
The health of the balloon
Colour : STRING
The colour of the balloon
DefenceItem : STRING
The item the balloon uses to defend itself
Constructor()
Initialises the defence item and colour using the
parameters
Initialises health to 100
ChangeHealth()
Takes the change as a parameter and adds this to the
health
GetDefenceItem()
Returns the defence item of the object
CheckHealth()
If the health is 0 or less, it returns TRUE, otherwise it
returns FALSE
(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.
Use your language appropriate constructor.
All attributes should be private. If you are writing in Python include attribute declarations using
comments.
Save your program as Question2_J2022.
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.
Amend your program code to include the get method GetDefenceItem().
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[2]
(c) The object’s method ChangeHealth() takes an integer number as a parameter and adds
this to the health attribute of the object.
Amend your program code to include the method ChangeHealth().
Save your program.
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).
Amend your program code to include the method CheckHealth().
Save your program.
Copy and paste the program code into part 2(d) in the evidence document.
[2]
(e) Amend the main program to:
•
take as input a defence item and colour from the user
•
create a new balloon with the identifier Balloon1 using the data input.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[3]
(f)
The function Defend():
•
takes a balloon object as a parameter
•
takes as input the strength of an opponent from the user
•
uses the ChangeHealth() method to subtract the strength input from the object’s
health
•
outputs the defence item of the balloon
•
checks the health of the object and outputs an appropriate message if it has no health
remaining, or if it has health remaining
•
returns the amended balloon object.
Write program code to declare the function Defend().
Save your program.
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().
Save your program.
Copy and paste the program code into part 2(g)(i) in the evidence document.
[2]
(ii)
Test your program using the following inputs:
•
balloon defence method "Shield"
•
balloon colour "Red"
•
strength of opponent 50
Take a screenshot to show the output.
Copy and paste the screenshot into part 2(g)(ii) in the evidence document.
[1]
Question text
3
A programmer is designing a computer game that uses a set of cards.
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
Colour : STRING
The colour of the card
Constructor()
Takes two values as parameters and sets them to the private
attributes
GetNumber()
Returns the number of the card
Returns the colour of the card
GetColour()
(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.
Use your programming language appropriate constructor.
All attributes should be private. If you are writing in Python, include attribute declarations
using comments.
Save your program as Question3_J22.
Copy and paste the program code into part 3(a) in the evidence document.
[5]
(b) The two get methods return the associated attribute.
Write program code for the get methods GetNumber() and GetColour().
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[3]
(c) The text file CardValues.txt stores the data for 30 cards, in the order: number, colour.
For example, the first card in the text file:
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
Write the main program to:
•
declare an array of type Card with 30 elements
•
read in the data for the 30 cards from CardValues.txt and assign each to the array.
Save your program.
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.
The function, ChooseCard():
•
takes as input an integer to represent an array index from 1 to 30
•
validates that the value is between 1 and 30 inclusive
•
checks if the card is available (it has not already been selected)
•
loops until an available card is selected
•
returns the index of the card if it is available.
Amend the program to store which cards have already been selected and write program
code for the function ChooseCard().
Save your program.
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.
(i)
Amend the main program to:
•
create an array, Player1, for player 1 of type Card
•
ask player 1 to input 4 integers using the function from part 3(d)
•
store the cards in Player1
•
output the number and colour of the 4 cards in Player1.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[5]
(ii)
Test your program with the following test data:
Test 1: 1
5
9
10
Test 2: 2
2
3
4
4
5
Take a screenshot to show the output.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[1]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
2
Draw one line from each programming paradigm to its most appropriate description.
Programming paradigm
Description
Programs using the instruction set of a
processor
Declarative
Programs based on events such as user
actions or sensor outputs
Imperative
Programs using the concepts of class,
inheritance, encapsulation and
polymorphism
Low-level
Programs with an explicit sequence of
commands that update the program state,
with or without procedure calls
Object-oriented
Programs that specify the desired result
rather than how to get to it
[4]
Question text
5
(a) Compare sequential and serial methods of file organisation.
…
…
…
…
…
…
…
…
… [4]
(b) State the most suitable method of file access when a record is referenced by a unique
address on a disk-type storage medium.
… [1]
(c) State the most suitable method of file access when a bank stores its data records in ascending
order of account number.
… [1]
Question text
2
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).
The class has the following attributes and methods.
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
Constructor()
// takes all four values as parameters and
sets them to the private attributes
GetDescription()
// returns the description of the picture
GetHeight()
// returns the height
GetWidth()
// returns the width
GetColour()
// returns the frame colour
SetDescription()
// takes the new description as a parameter
and writes the value to description
(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.
Use your language appropriate constructor. All attributes should be private.
If you are writing in Python programming language, include attribute declarations using
comments.
Save your program as question 2.
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.
Write the four get methods.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[3]
(c) The method SetDescription() takes a new description as a parameter, and writes this
value to the appropriate attribute.
Write the method SetDescription().
Save your program.
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.
Save your program.
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.
For example, for the first picture in the text file:
Flowers is the description
45 is the width
50 is the height
black is the frame 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.
The function ReadData():
•
opens the file Pictures.txt
•
reads the data from the file
•
creates a new object of type Picture for each picture
•
writes each object to the array
•
raises an exception if the file cannot be found
•
counts and returns the number of pictures in the array.
Write program code for the function ReadData().
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[8]
(f)
The main program calls the function ReadData().
Write the main program.
Save your program.
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.
Edit the main program to perform the described actions.
Save your program.
Copy and paste the program code into part 2(g) in the evidence document.
[7]
(h) Test your program by inputting the following search criteria:
•
BLACK, 100, 100
•
silver, 25, 25
Take screenshots to show the output for both search criteria.
Copy and paste the screenshots into part 2(h) in the evidence document.
Question text
9
(a) Describe what is meant by an imperative (procedural) programming language.
…
…
…
… [2]
(b) Describe what is meant by a declarative programming language.
…
…
…
… [2]
(c) Identify the programming paradigm for each of these program code examples.
Program code example
Programming paradigm
male(john).
female(ethel).
parent(john, ethel).
FOR Counter = 1 TO 20
X = X * Counter
NEXT Counter
Start: LDD Counter
INC ACC
STO Counter
public class Vehicle
{
private speed;
public Vehicle()
{
speed = 0;
}
}
[4]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Question text
3
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.
The program will be created using object-oriented programming (OOP).
The following class diagram describes the class TreasureChest.
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
getQuestion()
// returns the question
checkAnswer()
// takes the user’s answer as a parameter and
returns True if it is correct,
otherwise returns False
getPoints()
// takes the number of attempts as a
parameter and returns the number of
points awarded
(a) Create a new program.
Write program code to declare the class TreasureChest.
Do not write any other methods.
The attributes are private.
If you are using the Python programming language, include attribute declarations using
comments.
Save your program as question3.
Copy and paste the program code into part 3(a) in the evidence document.
[5]
(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
Write program code for the procedure, readData() to:
•
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.
Save your program.
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.
Write the method getQuestion().
Save your program.
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.
Write the method checkAnswer().
Save your program.
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.
•
If the number of attempts is 1, it returns the value of points.
•
If the number of attempts is 2, it returns the integer value of points divided by
2 (DIV 2).
•
If the number of attempts is 3 or 4, it returns the integer value of points divided by
4 (DIV 4).
•
If the number of attempts is not 1 or 2 or 3 or 4, it returns 0 (zero).
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).
Write the method getPoints().
Save your program.
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:
•
call the procedure readData()
•
ask the user to enter a question number between 1 and 5
•
output the question that matches the question number entered by the user
•
check if the answer input by the user is correct using the method checkAnswer()
•
repeat the question until the user inputs the correct answer
•
count how many times the user attempted the question
•
use the method getPoints() to return the number of points awarded
•
output the number of points the user is awarded.
Save your program.
Copy and paste