Question 131 marks
Question
A program stores data for a board game using Object-Oriented Programming (OOP). The game has objects that are placed on the board. Each object has a string code (for example "A") and an integer value (for example, 2). The class BoardObject stores the data about the objects that can be placed on the board: BoardObject Code : String stores the board object’s code Value : Integer stores the integer value of the board object Constructor() initialises the attributes to the parameter values GetCode() returns Code GetValue() returns Value (a) (i) Write program code to declare the class BoardObject and its constructor. Do not declare the other methods. Use your programming language appropriate constructor. If you are writing in Python, include attribute declarations using comments. Save your program as Question1_N25. Copy and paste the program code into part 1(a)(i) in the evidence document. [5] (ii) The methods GetCode() and GetValue() return the appropriate attribute. Write program code for GetCode() and GetValue() Save your program. Copy and paste the program code into part 1(a)(ii) in the evidence document. [3] (iii) The table shows the code and value of five board objects. The table has the variable identifier where each of the objects are stored. Variable Code Value identifier Object1 "A" 2 Object2 "B" 3 Object3 "C" 5 Object4 "D" 2 Object5 "E" 7 Write program code for the main program to instantiate each of the five board objects and store them in the variables with the identifiers given. Save your program. Copy and paste the program code into part 1(a)(iii) in the evidence document. [3] (b) The board objects are placed on a board that is represented by a 0-indexed 2D array: • the board is a 10 x 10 grid • each position on the board is identified by a row and column number • rows are numbered 0 to 9 • columns are numbered 0 to 9 • board objects can be placed at a row and column position • each board position is initialised with an empty BoardObject, an empty BoardObject has Code = "-" and Value = 0 For example, the element highlighted in the given board is in row 1 and column 3. Row Column 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 The class Board stores the data about the board and where the board objects are placed. Board TheBoard : ARRAY[0:9, 0:9] of BoardObject stores the board contents as a 2D array of 10 x 10 elements of type BoardObject Constructor() initialises each of TheBoard elements to an empty BoardObject with Code = "-" and Value = 0 GetObject() takes a row and column number as parameters and returns the BoardObject at the row, column position SetObject() takes a BoardObject, row number and column number as parameters. Stores the BoardObject in TheBoard at the given row, column position DisplayBoard() outputs the code of each BoardObject stored in TheBoard, one row at a time (i) Write program code to declare the class Board and its constructor. Do not declare the other methods. Use your programming language appropriate constructor. If you are writing in Python, include attribute declarations using comments. Save your program. Copy and paste the program code into part 1(b)(i) in the evidence document. [4] (ii) The method GetObject() takes a row number and column number as parameters. The method returns the BoardObject stored at the parameter position. Write program code for GetObject() Save your program. Copy and paste the program code into part 1(b)(ii) in the evidence document. [2] (iii) The method SetObject() takes three parameters: a BoardObject, row number and column number. The method stores the BoardObject parameter in the row, column position in TheBoard Write program code for SetObject() Save your program. Copy and paste the program code into part 1(b)(iii) in the evidence document. [2] (iv) The method DisplayBoard() outputs the Code of each BoardObject stored in TheBoard using GetCode() Each row in TheBoard is output on one line with a space between each Code For example, the following board contains these four board objects: • one object has code "A" in row 0 column 7 • one object has code "B" in row 0 column 9 • one object has code "C" in row 1 column 1 • one object has code "E" in row 6 column 5 The other board objects are empty. The output for this board will be: - - - - - - - A - B - C - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - E - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Write program code for DisplayBoard() Save your program. Copy and paste the program code into part 1(b)(iv) in the evidence document. [3] (c) (i) The table gives the row and column position on the board to store each of the five objects created in part 1(a)(iii). Object column row position identifier position Object1 0 0 Object2 9 9 Object3 4 5 Object4 2 2 Object5 8 7 Write program code to amend the main program to: • declare a new instance of Board() • store each BoardObject in the position given in the table • call DisplayBoard() for the new board object. Save your program. Copy and paste the program code into part 1(c)(i) in the evidence document. [3] (ii) Test your program. Take a screenshot of the output(s). Save your program. Copy and paste the screenshot(s) into part 1(c)(ii) in the evidence document. [1] (d) (i) Amend the main program to: • repeatedly take a row position as input until it is between 0 and 9 inclusive • repeatedly take a column position as input until it is between 0 and 9 inclusive • use the appropriate method(s) to identify if there is an object in the array position input • output "Miss" if there is an empty BoardObject in that position • output the Code and Value if there is a non-empty BoardObject in that position. All outputs must include appropriate messages. Save your program. Copy and paste the program code into part 1(d)(i) in the evidence document. [4] (ii) Test your program by entering this test data in the order given: Row position first input: 10 Row position second input: 4 Column position first input: –1 Column position second input: 5 Take a screenshot of the output(s). Save your program. Copy and paste the screenshot(s) into part 1(d)(ii) in the evidence document. [1]
Mark scheme
1(a)(i) 1 mark each 5 • Class header (and end where appropriate) • Declaring Code as string and Value as integer • Constructor header (and end where appropriate) within class … • … taking two parameters … • … assigning parameters to attributes Example program code. Java class BoardObject{ public String Code; public Integer Value; public BoardObject(String pCode, Integer pValue){ Code = pCode; Value = pValue; } } VB.NET Public Class BoardObject Private Code As String Private Value As Integer Sub New(pCode, pValue) Code = pCode Value = pValue End Sub End Class Python class BoardObject(): def __init__(self, Code, Value): self.Code = Code #string self.Value = Value # integer 1(a)(ii) 1 mark each 3 • 1 get header (and end where appropriate) with no parameter … • … returning correct value without overriding • 2nd correct get method Example program code Java public String GetCode(){ return Code; } public Integer GetValue(){ return Value; } VB.NET Function GetCode() Return Code End Function Function GetValue() Return Value End Function Python def GetCode(self): return self.Code def GetValue(self): return self.Value 1(a)(iii) 1 mark each 3 • Creating one instance of BoardObject and storing in correct variable … • … with correct parameters • Remaining four created correctly and stored Example program code Java BoardObject Object1 = new BoardObject("A",2); BoardObject Object2 = new BoardObject("B",3); BoardObject Object3 = new BoardObject("C",5); BoardObject Object4 = new BoardObject("D",2); BoardObject Object5 = new BoardObject("E",7); VB.NET Dim Object1 As BoardObject = New BoardObject("A", 2) Dim Object2 As BoardObject = New BoardObject("B", 3) Dim Object3 As BoardObject = New BoardObject("C", 5) Dim Object4 As BoardObject = New BoardObject("D", 2) Dim Object5 As BoardObject = New BoardObject("E", 7) Python Object1 = BoardObject("A",2) Object2 = BoardObject("B",3) Object3 = BoardObject("C",5) Object4 = BoardObject("D",2) Object5 = BoardObject("E",7) 1(b)(i) 1 mark each 4 • Class header (and end where appropriate) • Constructor header (and end where appropriate) within class • Declaration of 2D array with 10 × 10 elements of type BoardObject • Storing BoardObject object with Code "-" and Value 0 in each array element Example program code Java class Board{ private BoardObject[][] TheBoard = new BoardObject[10][10]; public Board(){ for(Integer x = 0; x < 10; x++){ for(Integer y = 0; y < 10; y++){ TheBoard[x][y] = new BoardObject("-",0); }}}} VB.NET Public Class Board Private TheBoard(9, 9) As BoardObject Sub New() For x = 0 To 9 For y = 0 To 9 TheBoard(x, y) = New BoardObject("-", 0) Next Next End Sub End Class 1(b)(i) Python class Board(): def __init__(self): self.TheBoard = [] #type BoardObject for x in range(10): TempList = [] for y in range(10): TempList.append(BoardObject("-",0)) self.TheBoard.append(TempList) 1(b)(ii) 1 mark each 2 • Get method header taking 2 (integer) parameters … • … returning the BoardObject at Board position of parameters Example program code Java public BoardObject GetObject(Integer Rowpos, Integer Columnpos){ return TheBoard[Rowpos][Columnpos]; } VB.NET Function GetObject(Rowpos, Columnpos) Return TheBoard(Rowpos, Columnpos) End Function Python def GetObject(self, Rowpos, Columnpos): return self.TheBoard[Rowpos][Columnpos] 1(b)(iii) 1 mark each 2 • Set method header taking three parameters (TheObject, row, column) … • … storing parameter TheObject in TheBoard at parameters row and column Example program code Java public void SetObject(BoardObject TheObject, Integer Rowpos, Integer Columnpos){ TheBoard[Rowpos][Columnpos] = TheObject; } VB.NET Sub SetObject(TheObject, Rowpos, Columnpos) TheBoard(Rowpos, Columnpos) = TheObject End Sub Python def SetObject(self, TheObject, Rowpos, Columnpos): self.TheBoard[Rowpos][Columnpos] = TheObject 1(b)(iv) 1 mark each 3 • Method DisplayBoard() header (and end where appropriate) and using GetCode() • Outputting Code of all BoardObject elements in both indices (10 × 10) • … with each row on one line and space between each value Example program code Java public void DisplayBoard(){ String OutputLine; for(Integer x = 0; x < 10; x++){ OutputLine = ""; for(Integer y = 0; y < 10; y++){ OutputLine = OutputLine + TheBoard[x][y].GetCode() + " "; } System.out.println(OutputLine); } } VB.NET Sub DisplayBoard() Dim OutputLine As String For x = 0 To 9 OutputLine = "" For y = 0 To 9 OutputLine = OutputLine & TheBoard(x, y).GetCode() & " " Next Console.WriteLine(OutputLine) Next End Sub 1(b)(iv) Python def DisplayBoard(self): for x in range(10): OutputLine = "" for y in range(10): OutputLine = OutputLine + str(self.TheBoard[x][y].GetCode()) + " " print(OutputLine) 1(c)(i) 1 mark each 3 • Creating instance of Board and storing it • Storing all 5 objects in correct positions • Calling DisplayBoard() Example program code Java Board GameBoard =new Board(); BoardObject Object1 = new BoardObject("A",2); BoardObject Object2 = new BoardObject("B",3); BoardObject Object3 = new BoardObject("C",5); BoardObject Object4 = new BoardObject("D",2); BoardObject Object5 = new BoardObject("E",7); GameBoard.SetObject(Object1, 0, 0); GameBoard.SetObject(Object2, 9, 9); GameBoard.SetObject(Object3, 4, 5); GameBoard.SetObject(Object4, 2, 2); GameBoard.SetObject(Object5, 8, 7); GameBoard.DisplayBoard(); VB.NET Dim GameBoard As Board = New Board() Dim Object1 As BoardObject = New BoardObject("A", 2) Dim Object2 As BoardObject = New BoardObject("B", 3) Dim Object3 As BoardObject = New BoardObject("C", 5) Dim Object4 As BoardObject = New BoardObject("D", 2) Dim Object5 As BoardObject = New BoardObject("E", 7) GameBoard.SetObject(Object1, 0, 0) GameBoard.SetObject(Object2, 9, 9) GameBoard.SetObject(Object3, 4, 5) GameBoard.SetObject(Object4, 2, 2) GameBoard.SetObject(Object5, 8, 7) GameBoard.DisplayBoard() 1(c)(i) Python GameBoard = Board() Object1 = BoardObject("A",2) Object2 = BoardObject("B",3) Object3 = BoardObject("C",5) Object4 = BoardObject("D",2) Object5 = BoardObject("E",7) GameBoard.SetObject(Object1, 0, 0) GameBoard.SetObject(Object2, 9, 9) GameBoard.SetObject(Object3, 4, 5) GameBoard.SetObject(Object4, 2, 2) GameBoard.SetObject(Object5, 8, 7) GameBoard.DisplayBoard() 1(c)(ii) 1 mark for screenshot showing board contents 1 Example 1(d)(i) 1 mark each to max 4 4 • Taking x-axis and y-axis as input repeatedly until each value is between 0 and 9 (inclusive) • Calling Board.GetObject() with input x-axis and y-axis values … • … checking if object exists e.g. if Code is "-" • … outputting "Miss" if no BoardObject and outputting Code and Value in an appropriate message if there is a BoardObject Example program code Java Scanner scanner = new Scanner(System.in); Integer InputRow = -1; while(InputRow < 0 || InputRow > 9){ System.out.println("Enter the row position between 0 and 9 "); InputRow = Integer.parseInt(scanner.nextLine()); } Integer InputColumn = -1; while(InputColumn < 0 || InputColumn > 9){ System.out.println("Enter the column position between 0 and 9 "); InputColumn = Integer.parseInt(scanner.nextLine()); } BoardObject GuessObject = GameBoard.GetObject(InputRow, InputColumn); if((GuessObject.GetCode()).equals("-")){ System.out.println("Miss"); }else{ System.out.println("You found " + GuessObject.GetCode() + " with value " + GuessObject.GetValue()); } VB.NET Dim InputRow, InputColumn As Integer InputRow = -1 InputColumn = -1 While InputRow < 0 Or InputRow > 9 Console.WriteLine("Enter the row position between 0 and 9 ") InputRow = Console.ReadLine() End While 1(d)(i) While InputColumn < 0 Or InputColumn > 9 Console.WriteLine("Enter the column position between 0 and 9 ") InputColumn = Console.ReadLine() End While Dim GuessObject As BoardObject = GameBoard.GetObject(InputRow, InputColumn) If GuessObject.GetCode() = "-" Then Console.WriteLine("Miss") Else Console.WriteLine("You found " & GuessObject.GetCode() & " with value " & GuessObject.GetValue()) End If Python InputRow = -1 while InputRow < 0 or InputRow > 9: InputRow = int(input("Enter the row position between 0 and 9 ")) InputColumn = -1 while InputColumn < 0 or InputColumn > 9: InputColumn = int(input("Enter the column position between 0 and 9 ")) GuessObject = GameBoard.GetObject(InputRow, InputColumn) if GuessObject.GetCode() == "-": print("Miss") else: print("You found " + str(GuessObject.GetCode()) + " with value " + str(GuessObject.GetValue())) 1(d)(ii) 1 mark for screenshot showing inputs and correct output 1 Row 10 4 Column -1 5 Output C 5 Example