Question 128 marks
Question
A program stores data about birds using Object-Oriented Programming (OOP). The class Bird stores the data about the birds: Bird DistancePerHour : Real stores the number of kilometres per hour (km/h) the bird can fly (between 0.0 and 100.0 inclusive) Species : String stores the species of the bird, for example Pigeon XPosition : Real stores the current horizontal position of the bird YPosition : Real stores the current vertical position of the bird Constructor() initialises Species and DistancePerHour to the parameter values; initialises XPosition and YPosition to 500.0 GetPosition() returns a string message that contains the horizontal and vertical position of the bird GetSpecies() returns the species of the bird Move() takes a direction and number of minutes flying (between 0 and 500 inclusive) as parameters and updates the appropriate horizontal or vertical position (a) (i) Write program code to declare the class Bird and its constructor. Do not declare the other methods. All attributes should be private. 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. [4] (ii) The method GetSpecies() returns the species of the bird. Write program code for GetSpecies() Save your program. Copy and paste the program code into part 1(a)(ii) in the evidence document. [2] (iii) The method GetPosition() returns a string of the bird’s data in the format: "X = " & <XPosition> & " Y = " & <YPosition> An example string for a bird is: X = 500.0 Y = 250.0 Write program code for GetPosition() Save your program. Copy and paste the program code into part 1(a)(iii) in the evidence document. [3] (iv) If the bird is travelling north, the vertical position increases. If the bird is travelling south, the vertical position decreases. If the bird is travelling east, the horizontal position increases. If the bird is travelling west, the horizontal position decreases. The method Move(): • takes the direction of travel as a parameter in the form ‘N’ for north, ‘S’ for south, ‘E’ for east or ‘W’ for west • takes the number of minutes that the bird has been flying (to the nearest minute) as a parameter • calculates the distance travelled by the bird using the formula: distance travelled = (distance per hour / 60) * minutes flying • updates the vertical or horizontal position using the distance calculated. You do not need to validate the parameters. Write program code for Move() Save your program. Copy and paste the program code into part 1(a)(iv) in the evidence document. [5] (b) The main program creates two instances of Bird The first bird is the species ‘Cockatiel’ and flies 71.0 km/h. The second bird is the species ‘Macaw’ and flies 56.0 km/h. Write program code to declare and initialise the two birds. Save your program. Copy and paste the program code into part 1(b) in the evidence document. [3] (c) (i) The main program needs to: • output a message that includes the species and current X position and Y position for each bird • prompt the user to select one of the birds to move and take this as an input • prompt the user to enter the direction the bird has been travelling and take this as an input • prompt the user to enter the time to the nearest minute that the bird has been travelling and take this as an input • call the appropriate method to update the chosen bird’s position • output an update on the bird’s new position. Each input needs to repeat until valid data is entered. All outputs must be meaningful. Write program code to amend the main program. Save your program. Copy and paste the program code into part 1(c)(i) in the evidence document. [8] (ii) Test your program four times to meet these criteria: Test 1: The Cockatiel travels north for 60 minutes. Test 2: The Macaw travels south for 30 minutes. Test 3: The Cockatiel travels west for 30 minutes. Test 4: The Macaw travels east for 60 minutes. 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. [3]
Mark scheme
1(a)(i) 1 mark each 4 • Class header (and end) • 4 private attributes with correct data types • Constructor header (and end) taking 2 parameters within class … • … within constructor assigning parameters to Species and DistancePerHour and assigning 500.0 to XPosition and YPosition Example program code Java class Bird{ private String Species; private Double DistancePerHour; private Double XPosition; private Double YPosition; public Bird(Double pDistancePerHour, String pSpecies){ Species = pSpecies; DistancePerHour = pDistancePerHour; XPosition = 500.0; YPosition = 500.0;}} VB.NET Class Bird Private Species As String Private DistancePerHour As Single Private XPosition As Single Private YPosition As Single Sub New(pDistancePerHour, pSpecies) Species = pSpecies DistancePerHour = pDistancePerHour XPosition = 500.0 YPosition = 500.0 End Sub End Class 1(a)(i) Python class Bird: def __init__(self, pDistancePerHour, pSpecies): self.__Species = pSpecies #string self.__DistancePerHour = pDistancePerHour #real self.__XPosition = 500.0 #real self.__YPosition = 500.0 #real 1(a)(ii) 1 mark each 2 • Get method header (and end) with no parameter …. • … returning Species Example program code Java public String GetSpecies(){ return Species; } VB.NET Function GetSpecies() Return Species End Function Python def GetSpecies(self): return self.__Species 1(a)(iii) 1 mark each 3 • Get method header (and end) with no parameter, returning a value • Creating correct string using attributes … • … returning this string Example program code Java public String GetPosition(){ String ReturnValue = "X = " + XPosition + " Y = " + YPosition; return ReturnValue; } VB.NET Function GetPosition() Dim ReturnValue As String = "X = " & XPosition & " Y = " & YPosition Return ReturnValue End Function Python def GetPosition(self): ReturnValue = "X = " + str(self.__XPosition) + " Y = " + str(self.__YPosition) return ReturnValue 1(a)(iv) 1 mark each 5 • Method header (and end) taking direction and minutes flying as parameters • Calculation of distance using minutes flying parameter and attribute DistancePerHour • Selection based on direction parameter … • … north adding to YPosition and south subtracting from YPosition • … east adding to XPosition and west subtracting from XPosition Example program code Java public Boolean Move(String Direction, Integer MinsFlying){ if(Direction.compareTo("E") == 0){ XPosition = XPosition + ((DistancePerHour / 60) * MinsFlying); }else if(Direction.compareTo("W") == 0){ XPosition = XPosition - ((DistancePerHour / 60) * MinsFlying); }else if(Direction.compareTo("N") == 0){ YPosition = YPosition + ((DistancePerHour / 60) * MinsFlying); }else if(Direction.compareTo("S") == 0){ YPosition = YPosition - ((DistancePerHour / 60) * MinsFlying); } } 1(a)(iv) VB.NET Function Move(Direction, MinsFlying) If Direction = "E" Then XPosition = XPosition + ((DistancePerHour / 60) * MinsFlying) ElseIf Direction = "W" Then XPosition = XPosition - ((DistancePerHour / 60) * MinsFlying) ElseIf Direction = "N" Then YPosition = YPosition + ((DistancePerHour / 60) * MinsFlying) ElseIf Direction = "S" Then YPosition = YPosition - ((DistancePerHour / 60) * MinsFlying) End If End Function Python def Move(self, Direction, MinsFlying): if Direction == "E": self.__XPosition = self.__XPosition + ((self.__DistancePerHour/60)*MinsFlying) elif Direction == "W": self.__XPosition = self.__XPosition - ((self.__DistancePerHour/60)*MinsFlying) elif Direction == "N": self.__YPosition = self.__YPosition + ((self.__DistancePerHour/60)*MinsFlying) elif Direction == "S": self.__YPosition = self.__YPosition - ((self.__DistancePerHour/60)*MinsFlying) 1(b) 1 mark each 3 • Cockatiel 71.0 instance of Bird created … • Macaw 56.0 instance of Bird created … • … both stored in variables/structures Example program code Java Bird FirstBird = new Bird(71.0, "Cockatiel"); Bird SecondBird = new Bird(56.0, "Macaw"); VB.NET Dim FirstBird As Bird = New Bird(71.0, "Cockatiel") Dim SecondBird As Bird = New Bird(56.0, "Macaw") Python FirstBird = Bird(71.0, "Cockatiel") SecondBird = Bird(56.0, "Macaw") 1(c)(i) 1 mark each 8 • Output species, X and Y position for both Bird objects in appropriate messages(s) • Prompt and input of bird choice, time and direction … • … validating all three inputs … • … looping until all three are valid • Calling Move() for chosen bird … • … with only input direction and input time as arguments • Outputting new position for the bird moved • Using get methods throughout where appropriate Example program code Java Integer Choice; Integer Time; String Direction; Scanner scanner = new Scanner(System.in); Choice = 0; while(Choice != 1 && Choice != 2){ System.out.println("Which bird do you want to move"); System.out.println("Enter 1 for " + FirstBird.GetSpecies() + " is currently at " + FirstBird.GetPosition()); System.out.println("Enter 2 for " + SecondBird.GetSpecies() + " is currently at " + SecondBird.GetPosition()); Choice = Integer.parseInt(scanner.nextLine()); } Time = -1; while(Time < 0 or Time > 500){ System.out.println("To the nearest minute how long as the bird been flying?"); Time = Integer.parseInt(scanner.nextLine()); } Boolean Valid = false; while(Valid == false){ 1(c)(i) Valid = true; System.out.println("Which direction has the bird been flying, North, South, East or West?"); Direction = scanner.nextLine().toUpperCase(); if(Direction.compareTo("NORTH") == 0 || Direction.compareTo("N") == 0){ if(Choice == 1){ FirstBird.Move("N",Time); }else{ SecondBird.Move("N", Time); } } else if(Direction.compareTo("SOUTH")== 0 || Direction.compareTo("S") == 0){ if(Choice == 1){ FirstBird.Move("S",Time); }else{ SecondBird.Move("S", Time); } } else if(Direction.compareTo("EAST")== 0 || Direction.compareTo("E") == 0){ if(Choice == 1){ FirstBird.Move("E",Time); }else{ SecondBird.Move("E", Time); } } else if(Direction.compareTo("WEST")== 0 || Direction.compareTo("W") == 0){ if(Choice == 1){ FirstBird.Move("W",Time); }else{ SecondBird.Move("W", Time); } }else{ Valid = false; } } System.out.println(FirstBird.GetSpecies() + " is currently at " + FirstBird.GetPosition()); 1(c)(i) System.out.println(SecondBird.GetSpecies() + " is currently at " + SecondBird.GetPosition()); VB.NET Dim Choice As Integer Dim Time As Integer Dim Direction As String Choice = 0 While Choice <> 1 And Choice <> 2 Console.WriteLine("Which bird do you want to move") Console.WriteLine("Enter 1 for " & FirstBird.GetSpecies() & " is currently at " & FirstBird.GetPosition()) Console.WriteLine("Enter 2 for " & SecondBird.GetSpecies() & " is currently at " & SecondBird.GetPosition()) Choice = Console.ReadLine End While Time = -1 While Time < 0 Or Time > 500 Console.WriteLine("To the nearest minute how long has the bird been flying ") Time = Console.ReadLine End While Dim Valid As Boolean = False While (Valid = False) Valid = True Console.WriteLine("Which direction has the bird been flying, North, South, East or West ") Direction = Console.ReadLine().ToUpper If Direction = "NORTH" Or Direction = "N" Then If Choice = 1 Then FirstBird.Move("N", Time) Else SecondBird.Move("N", Time) End If 1(c)(i) ElseIf Direction = "SOUTH" Or Direction = "S" Then If Choice = 1 Then FirstBird.Move("S", Time) Else SecondBird.Move("S", Time) End If ElseIf Direction = "EAST" Or Direction = "E" Then If Choice = 1 Then FirstBird.Move("E", Time) Else SecondBird.Move("E", Time) End If ElseIf Direction = "WEST" Or Direction = "W" Then If Choice = 1 Then FirstBird.Move("W", Time) Else SecondBird.Move("W", Time) End If Else Valid = False End If End While Console.WriteLine(FirstBird.GetSpecies() & " is currently at " & FirstBird.GetPosition()) Console.WriteLine(SecondBird.GetSpecies() & " is currently at " & SecondBird.GetPosition()) Python Choice = 0 while Choice != 1 and Choice != 2: print("Which bird do you want to move") print("Enter 1 for", FirstBird.GetSpecies(), "is currently at", FirstBird.GetPosition()) Choice = -1 print("Enter 2 for", SecondBird.GetSpecies(), "is currently at", SecondBird.GetPosition()) 1(c)(i) Choice = int(input()) Time = -1 while Time < 0 || Time > 500: Time = int(input("To the nearest minute how long has the bird been flying ")) Valid = False while Valid == False: Valid = True Direction = input("Which direction has the bird been flying, North, South, East or West ").upper() if Direction == "NORTH" or Direction == "N": if Choice == 1: FirstBird.Move("N",Time) else: SecondBird.Move("N",Time) elif Direction == "SOUTH" or Direction == "S": if Choice == 1: FirstBird.Move("S",Time) else: SecondBird.Move("S",Time) elif Direction == "EAST" or Direction == "E": if Choice == 1: FirstBird.Move("E",Time) else: SecondBird.Move("E",Time) elif Direction == "WEST" or Direction == "W": if Choice == 1: FirstBird.Move("W",Time) else: SecondBird.Move("W",Time) else: Valid = False print(FirstBird.GetSpecies(), "is currently at", FirstBird.GetPosition()) print(SecondBird.GetSpecies(), "is currently at", SecondBird.GetPosition()) 1(c)(ii) 1 mark for each: 3 • screenshot showing inputs for one test with correct output • screenshot showing inputs for a second test with correct output • screenshot showing inputs for a third and fourth test with correct output e.g. Test 1: Test 2: Test 3: Test 4: