Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical OOP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

A-Level Computer Science (9618)

Work sheet No:06


Topic:Practical OOP Student Name:_____________

1 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

GetColour() Returns the colour 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.

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]

Compiled by: Mr. Fazal-e-Malik Ph:0333 4732544 9618/42/M/J/22 Page-1


(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]

Page-2
9

(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]

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.

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]

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.

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]
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).

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]

9618/42/O/N/21
(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.

[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.

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]

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

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 the program code into part 3(c)(iv) in the evidence document.
[7]

(v) Test the program.

Take a screenshot showing the input(s) and output(s) for each of the following two tests.

In the first test:

• select question 1 and answer it correctly the first time.

In the second test:

• select question 5 and answer it correctly the second time.

Save your program.

Copy and paste the screenshots into part 3(c)(v) in the evidence document.
[2]
Page-5

You might also like