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 answer : INTEGER points : INTEGER // stores the question // stores the answer // 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)(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]
- (c)(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]
- (c)(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]
- (c)(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]
- (c)(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]
Mark scheme
Marking points from the official mark scheme (May/June 2021). The mark or mark type is shown in brackets.
- [5] 1 mark per bullet point • Class named treasureChest and end • Question declared as string as a class attribute • Answer declared as integer as a class attribute • Points declared as integer as a class attribute • All 3 attributes are private
- [8] 1 mark per bullet point to max 8 • procedure declared as readData • declare array arrayTreasure with 4 elements type treasureChest • opening correct file for read • looping until EOF/5 questions ...reading in and storing each group of 3 lines appropriately • creating object of type treasureChest ...with question, answer and points from file as parameters • ..adding to next array element/appending • repeatedly for all 5 questions in correct order • Use of appropriate exception handler...appropriate output if file not found • Closing correct file
- [1] 1 mark for getQuestion returning the value of question
- [3] 1 mark per bullet point • Function checkAnswer taking in the parameter, returning Boolean • Comparing parameter to that object's answer... • ...returning True if correct and False otherwise
- [5] 1 mark per bullet point • Function getPoints taking attempts as parameter and returning integer • If attempts is 1 returning points • If attempts is 2 returns points DIV 2 • If attempts is 3 or 4 returns points DIV 4 • otherwise returns 0
- [7] 1 mark per bullet point to max 7 • Call the procedure readData() • Take the question number as input from user ..validated between 1 and 5 • Output the question stored at user's input value • Read answer from user • Check the answer input against question's answer ...looping until the answer is correct • Keeping track of the number of attempts using a variable • Using getPoints() and sending the number of attempts as a parameter ...outputting the number of points returned • Using .getQuestion and .checkAnswer to access question number input by user and answer input by used
- [2] 1 mark per screenshot • Screenshot: outputting 2*2 entering 4 outputting 10 • Screenshot: outputting 3000+4000 entering an incorrect value entering 7000 outputting 9
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →