Question 129 marks
Question
A computer game is designed for users to select characters. Each character can take part in a group of events. Each group has five events. There are four types of event: jump, swim, run, drive. The program is written using object-oriented programming. (a) The class EventItem stores data about the events. EventItem EventName : STRING stores the name of the event Type : STRING stores the type of event, either: jump, swim, run or drive Difficulty : INTEGER stores the difficulty of the event from 1 (easiest) to 5 (hardest) Constructor() initialises EventName, Type and Difficulty to its parameter values GetName() returns the name of the event GetDifficulty() returns the difficulty of the event GetEventType() returns the type of event (i) Write program code to declare the class EventItem and its constructor. Do not declare the other methods. Use your programming language’s appropriate constructor. All attributes must be private. If you are writing in Python, include attribute declarations, using comments. Save your program as Question1_N24. Copy and paste the program code into part 1(a)(i) in the evidence document. [4] (ii) The get methods GetName(), GetDifficulty() and GetEventType() each return the relevant attribute. Write program code for the three get methods. Save your program. Copy and paste the program code into part 1(a)(ii) in the evidence document. [3] (b) The array Group stores five objects of type EventItem. (i) Write program code to declare Group local to the main program. Save your program. Copy and paste the program code into part 1(b)(i) in the evidence document. [1] (ii) One group has the following events: Event name Event type Event difficulty Bridge jump 3 Water wade swim 4 100 mile run run 5 Gridlock drive 2 Wall on wall jump 4 Write program code to create an instance of EventItem for each of the five events, and store them in Group. Save your program. Copy and paste the program code into part 1(b)(ii) in the evidence document. [3] (c) The class Character stores data about the characters in the game. Each character has a skill level for each type of event. The skill level is an integer between 1 and 5 inclusive. Skill level 1 is the lowest skill level, and skill level 5 is the highest skill level. Character CharacterName : STRING stores the name of the character Jump : INTEGER stores the character’s skill level at events of type jump Swim : INTEGER stores the character’s skill level at events of type swim Run : INTEGER stores the character’s skill level at events of type run Drive : INTEGER stores the character’s skill level at events of type drive Constructor() initialises CharacterName, Jump, Swim, Run and Drive to its parameter values GetName() returns the name of the character CalculateScore() takes the type of event and difficulty as parameters. Calculates and returns the chance of the character completing the event Write program code to declare the class Character, its constructor and get method. Use your programming language’s 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 1(c) in the evidence document. [4] (d) The method CalculateScore() in the Character class calculates and returns the percentage chance of a character completing an event. When a character’s skill level is greater than or equal to the difficulty of that event, the percentage chance of completing the event is 100%. When a character’s skill level is less than the difficulty of that event, the character’s skill level is subtracted from the difficulty of that event. This difference is used to identify the percentage chance of success using this table: Percentage chance Difference of success 1 80 2 60 3 40 4 20 For example: • A character has a skill level of 3 for events of type run. • An event of type run has a difficulty level of 5 • The character’s skill level is less than the difficulty, therefore the difference is calculated. • The difference is the character’s skill level subtracted from the event difficulty, 5 – 3 = 2 • The difference is 2, therefore the percentage chance of succeeding is 60% Write program code for the method CalculateScore() to: • take the type of event and difficulty as parameters • calculate the percentage chance of the character completing the event • return the percentage chance of completing the event as an integer number, for example 60 Save your program. Copy and paste the program code into part 1(d) in the evidence document. [4] (e) Two characters are attempting each event in the group you created in part 1(b). One character has the name Tarz and the skill levels: Jump 5 Swim 3 Run 5 Drive 1 The second character has the name Geni and the skill levels: Jump 2 Swim 2 Run 3 Drive 4 Each Character object is stored in a variable. (i) Amend the main program to declare and create an instance of a Character object for Tarz and for Geni. Save your program. Copy and paste the program code into part 1(e)(i) in the evidence document. [2] (ii) Both characters Tarz and Geni take part in each event in the group you created in part 1(b). These steps are repeated for all five events: • The percentage chance of each character completing the event is calculated and compared. • The character with the highest percentage chance of completing the event gets 1 point. Their character name is output together with a message telling them they have won that event. • If both characters have the same percentage chance of completing the event, the scores do not change, and a message is output stating that the event is a draw. When the total score for each character has been calculated, the name of the character with the highest score is output stating they have won and the number of points they have. If both characters have the same number of points, a message is output telling them the group is a draw. Amend the main program to: • calculate the score for each character in each event • output the name of the character that wins each event or that the event is a draw • output the name of the character that has the most points for the group or that the group is a draw. Save your program. Copy and paste the program code into part 1(e)(ii) in the evidence document. [7] (iii) Test your program. Take a screenshot of the output. Save your program. Copy and paste the screenshot into part 1(e)(iii) in the evidence document. [1]
Mark scheme
1(a)(i) 1 mark each 4 • Class EventItem header (and end where appropriate) • 3 private attributes with suitable data types • Constructor header (and end where appropriate) with 3 (min) parameters within class declaration … • … assigning parameters to attributes within constructor e.g. Python class EventItem(): def __init__(self, pName, pType, pDifficulty): self.__EventName = pName #String self.__EventType = pType #String self.__Difficulty = pDifficulty #Integer VB.NET Class EventItem Private EventName As String Private EventType As String Private Difficulty As Integer Sub New(pName, pType, pDifficulty) EventName = pName EventType = pType Difficulty = pDifficulty End Sub End Class 1(a)(i) Java class EventItem{ private String EventName; private String EventType; private Integer Difficulty; public EventItem(String pName, String pType, Integer pDifficulty){ EventName= pName; EventType = pType; Difficulty = pDifficulty; } } 1(a)(ii) 1 mark each 3 • 1 get method with no parameter … • … return correct attribute (without changing) • Remaining 2 correct get methods returning the attributes e.g. Python def GetName(self): return self.__EventName def GetEventType(self): return self.__EventType def GetDifficulty(self): return self.__Difficulty 1(a)(ii) VB.NET Function GetName() Return EventName End Function Function GetEventType() Return EventType End Function Function GetDifficulty() Return Difficulty End Function Java public String GetName(){ return EventName; } public String GetEventType(){ return EventType; } public Integer GetDifficulty(){ return Difficulty; } 1(b)(i) 1 mark each: 1 • 1D array name Group with (min 5 elements and of type EventItem) e.g. Python Group = [] #type Event, 5 spaces VB.NET Dim Group(4) As EventItem Java EventItem[] Group = new EventItem[5]; 1(b)(ii) 1 mark each 3 • Any 1 instance of EventItem declared with values passed in correct order … • … stored in the array Group … • … remaining 4 correctly instantiated and stored in Group e.g. Python Group.append(EventItem("Bridge", "jump", 3)) Group.append(EventItem("Water wade", "swim", 4)) Group.append(EventItem("100 mile run", "run", 5)) Group.append(EventItem("Gridlock", "drive", 2)) Group.append(EventItem("Wall on wall", "jump", 4)) VB.NET Group(0) = New EventItem("Bridge", "jump", 3) Group(1) = New EventItem("Water wade", "swim", 4) Group(2) = New EventItem("100 mile run", "run", 5) Group(3) = New EventItem("Gridlock", "drive", 2) Group(4) = New EventItem("Wall on wall", "jump", 4) Java Group[0] = new EventItem("Bridge", "jump", 3); Group[1] = new EventItem("Water wade", "swim", 4); Group[2] = new EventItem("100 mile run", "run", 5); Group[3] = new EventItem("Gridlock", "drive", 2); Group[4] = new EventItem("Wall on wall", "jump", 4); 1(c) 1 mark each 4 • Class Character declared (and end where appropriate) • 5 private attributes with correct data types • Constructor header (and end) taking (min) 5 parameters and parameters assigned to attributes • Get method (with no parameter) returning name attribute 1(c) e.g. Python class Character(): def __init__(self, pName, pJump, pSwim, pRun, pDrive): self.__CName = pName #string self.__Jump = pJump #integer chance of success self.__Swim = pSwim #integer chance of success self.__Run = pRun #integer chance of success self.__Drive = pDrive #integer chance of success def GetName(self): return self.__CName #STRING VB.NET Class Character Private CName As String Private Jump As Integer Private Swim As Integer Private Run As Integer Private Drive As Integer Sub New(pName, pJump, pSwim, pRun, pDrive) CName = pName Jump = pJump Swim = pSwim Run = pRun Drive = pDrive End Sub Function GetName() Return CName End Function End Class 1(c) Java class Character{ private String CName; private Integer Jump; private Integer Swim; private Integer Run; private Integer Drive; public Character(String pName, Integer pJump, Integer pSwim, Integer pRun, Integer pDrive){ CName= pName; Jump = pJump; Swim = pSwim; Run = pRun; Drive = pDrive; } public String GetName(){ return CName } } 1(d) 1 mark each to max 4: 4 • Method header CalculateScore (and end where appropriate) taking (min) 2 parameters • Selection on the type of event using the parameter • … if skill value is > = difficulty return 100 • …otherwise subtracting skill value from the difficulty and return correct value 80 (diff 1), 60 (diff 2), 40 (diff 3) and 20 (diff 4) • Using the correct attributes and parameters throughout 1(d) e.g. Python def CalculateScore(self, Type, Difficulty): if Type == "jump": Chance = self.__Jump elif Type == "swim": Chance = self.__Swim elif Type == "run": Chance = self.__Run else: Chance = self.__Drive if Chance >= Difficulty: return 100 else: Difference = Difficulty - Chance if Difference == 1: return 80 elif Difference == 2: return 60 elif Difference == 3: return 40 elif Difference == 4: return 20 else: return 0 1(d) VB.NET Function CalculateScore(Type, Difficulty) Dim Chance As Integer Dim Difference As Integer If Type = "jump" Then Chance = Jump ElseIf Type = "swim" Then Chance = Swim ElseIf Type = "run" Then Chance = Run Else Chance = Drive End If If Chance >= Difficulty Then Return 100 Else Difference = Difficulty - Chance If Difference = 1 Then Return 80 ElseIf Difference = 2 Then Return 60 ElseIf Difference = 3 Then Return 40 ElseIf Difference = 4 Then Return 20 Else Return 0 End If End If End Function 1(d) Java public Integer CalculateScore(String Type, Integer Difficulty){ Integer Chance = 0; Integer Difference = 0; if(Type.equals("jump")){ Chance = Jump; }else if(Type.equals("swim")){ Chance = Swim; }else if(Type.equals("run")){ Chance = Run; }else{ Chance = Drive; } if(Chance >= Difficulty){ return 100; }else{ Difference = Difficulty - Chance; if(Difference == 1){ return 80; }else if(Difference == 2){ return 60; }else if(Difference == 3){ return 40; }else if(Difference == 4){ return 20; } } } 1(e)(i) 1 mark each 2 • Creating one new instance of Character with correct name and values for 1 character and storing • 2nd correct instance of Character and storing e.g. Python P1 = Character("Tarz", 5, 3, 5, 1) P2 = Character("Geni", 2, 2, 3, 4) VB.NET Dim P1 As Character = New Character("Tarz", 5, 3, 5, 1) Dim P2 As Character = New Character("Geni", 2, 2, 3, 4) Java Character P1 = new Character("Tarz", 5, 3, 5, 1); Character P2 = new Character("Geni", 2, 2, 3, 4); 1(e)(ii) 1 mark each 7 • Looping through each event in Group (or checking each of the 5 events manually) • Using CalculateScore() for each Character object with parameters of type and difficulty • …comparing the return values from the two function calls … • …incrementing points for winning player and outputting their name and message stating they have won for each event. • …outputting message if it's a draw. • Comparing the total points for each character after all events checked and outputting name of player with most points (and their points) and outputting message if it's a draw • Using get methods correctly throughout 1(e)(ii) e.g. Python P1Points = 0 P2Points = 0 for x in range(0, 5): P1EventScore = P1.CalculateScore(Group[x].GetEventType(), Group[x].GetDifficulty()) P2EventScore = P2.CalculateScore(Group[x].GetEventType(), Group[x].GetDifficulty()) if P1EventScore > P2EventScore: P1Points = P1Points + 1 print(P1.GetName(), "you win this event") elif P2EventScore > P1EventScore: P2Points = P2Points + 1 print(P2.GetName(), "you win this event") else: print("This event is a draw") if P1Points > P2Points: print(P1.GetName(), "you have won with", P1Points) elif P2Points> P1Points: print(P2.GetName(), "you have won with", P2Points) else: print("It's a draw") 1(e)(ii) VB.NET Dim P1 As Character = New Character("Tarz", 5, 3, 5, 1) Dim P2 As Character = New Character("Geni", 2, 2, 3, 4) Dim P1Points As Integer = 0 Dim P2Points As Integer = 0 Dim P1EventScore As Integer = 0 Dim P2EventScore As Integer = 0 For x = 0 To 4 P1EventScore = P1.CalculateScore(Group(x).GetEventType(), Group(x).GetDifficulty()) P2EventScore = P2.CalculateScore(Group(x).GetEventType(), Group(x).GetDifficulty()) If P1EventScore > P2EventScore Then P1Points = P1Points + 1 Console.WriteLine(P1.GetName() & " you win this event") ElseIf P2EventScore > P1EventScore Then P2Points = P2Points + 1 Console.WriteLine(P2.GetName() & " you win this event") Else Console.WriteLine("This event is a draw") End If Next x If P1Points > P2Points Then Console.WriteLine(P1.GetName() & " you have won with " & P1Points) ElseIf P2Points > P1Points Then Console.WriteLine(P2.GetName() & " you have won with " & P2Points) Else Console.WriteLine("It's a draw") End If 1(e)(ii) Java Integer P1Points = 0; Integer P2Points = 0; Integer P1EventScore = 0; Integer P2EventScore = 0; for(Integer x = 0; x < 5; x++){ P1EventScore = P1.CalculateScore(Group[x].GetEventType(), Group[x].GetDifficulty()); P2EventScore = P2.CalculateScore(Group[x].GetEventType(), Group[x].GetDifficulty()); System.out.println("P1 " + P1EventScore + " P2 " + P2EventScore); if(P1EventScore > P2EventScore){ P1Points++; System.out.println(P1.GetName() + " you win this event"); }else if(P2EventScore > P1EventScore){ P2Points++; System.out.println(P2.GetName() + " you win this event"); }else{ System.out.println("This event is a draw"); } } if(P1Points > P2Points){ System.out.println(P1.GetName() + " you have won with " + P1Points); }else if(P2Points > P1Points){ System.out.println(P2.GetName() + " you have won with " + P2Points); }else{ System.out.println("It's a draw"); } 1(e)(iii) 1 mark for output showing the correct winner for each event, the final winner’s name (and their points) 1 e.g.