Document

OCR Computer Science for A Level
Teaching and Learning Resources
Chapter 6: Types of programming language
ANSWERS TO QUESTIONS IN THE STUDENT’S BOOK
Page 87
Questions
1. Using an implementation of LMC, write and run the program provided.
2. Amend the program so it adds together three numbers.
Answer
INP
STA
INP
ADD
STA
INP
ADD
OUT
Num1 DAT
Num1
Num1
Num1
Num1
Question
3. Write a program that takes in two numbers and subtracts the second from the first.
Answer
INP
STA
INP
STA
LDA
SUB
OUT
Num1 DAT
Num2 DAT
Num1
Num2
Num1
Num2
NB: If students use the following code, they will find it incorrectly subtracts the first
number from the second:
INP
STA Num1
INP
SUB Num1
OUT
Num1 DAT
OCR Computer Science for A Level Chapter 6
© Hodder & Stoughton Limited 2015
OCR Computer Science for A Level
Teaching and Learning Resources
Page 89
Question
1. Write an LMC program that outputs the larger of two numbers.
Answer
Num2Bigger
outputAndEnd
Num1
Num2
INP
STA
INP
STA
SUB
BRP
LDA
BRA
LDA
OUT
HLT
DAT
DAT
Num1
Num2
Num1
Num2Bigger
Num1
outputAndEnd
Num2
Question
2. BRZ branches when 0 is stored in the accumulator. Write an LMC program that takes in two
numbers and outputs 1 if they are the same and 0 if they are different.
Answer
Same
outputAndEnd
Num1
Num2
One
Zero
INP
STA
INP
STA
SUB
BRZ
LDA
BRA
LDA
OUT
HLT
DAT
DAT
DAT
DAT
Num1
Num2
Num1
Same
Zero
outputAndEnd
One
1
0
OCR Computer Science for A Level Chapter 6
© Hodder & Stoughton Limited 2015
OCR Computer Science for A Level
Teaching and Learning Resources
Page 90
Question
1. Describe what the code provided does. (If you are unsure, try running it.)
Answer
The program asks for a number then counts up from 1 to that number.
So entering 5 results in:
1
2
3
4
5
Question
2. Rewrite the code so the program does exactly the same but this time only using BRP and not
BRZ or BRA.
Answer
Loop
End
Count
Times
One
INP
STA
LDA
SUB
BRP
LDA
ADD
STA
OUT
LDA
BRP
HLT
DAT
DAT
DAT
times
count
times
end #jump to end if accumulator is positive.
count
one
count
times
times #Bit of a hack to ensure…
loop #always branches positive
0
1
OCR Computer Science for A Level Chapter 6
© Hodder & Stoughton Limited 2015
OCR Computer Science for A Level
Teaching and Learning Resources
Page 92
Questions
1. In an object-oriented language of your choice, find out how to write a class, recreate the
monster class here and create the objects monsterOne and monsterTwo.
2. Add the method greet to the monster class, which should make the monster introduce
themselves. Test this method works.
Answer
Below is a solution in Java.
class TestClass
{
public static void main (String[] args) throws java.lang.Exception
{
Monster monsterOne = new Monster(true, 7, "Josh");
Monster monsterTwo = new Monster(false, 14, "Steve");
monsterOne.sleep();
monsterTwo.greet();
}
}
class Monster
{
private boolean poisonous;
private int strength;
private String name;
//Below is the contructor
public Monster(boolean givenPoisonous, int givenStrength, String givenName)
{
poisonous=givenPoisonous;
strength=givenStrength;
name=givenName;
}
public void eat()
{
System.out.println(name+" eats a hero. Delicious!");
}
public void sleep()
{
System.out.println("Snore, Snore, Snore");
}
}
public void greet()
{
System.out.println("Hello my name is "+name);
}
OCR Computer Science for A Level Chapter 6
© Hodder & Stoughton Limited 2015
OCR Computer Science for A Level
Teaching and Learning Resources
Page 94
Question
1. In an object-oriented language of your choice, find out how to use inheritance and create a
Vampire class.
Answer
Again, below is a Java example. Note the issues with the drink blood method accessing a
private attribute of its super class. To get round this in the given code, the following
method is added to the Monster class:
public String getName()
{
return name;
}
The Vampire class is as follows:
class Vampire extends Monster
{
private boolean hasCastle;
public Vampire(boolean givenHasCastle, int givenStrength, String givenName)
{
//The line below is the same as super.new(..
//in the pseudocode
super(false,givenStrength,givenName);
hasCastle=givenHasCastle;
}
public void sleep()
{
System.out.println("The vampire sleeps silently");
}
public void drinkBlood()
{
/*NB here comes a challenge. Name has been previously denoted
as private. This means it can't be accessed by subclasses.
There are two options:
1) Make name 'protected' instead of private. This
does mean it can be accessed by other classes in the package.
2) Add an accessor method to Monster.
In this case we have used option (2).
*/
System.out.println(super.getName()+" drinks the hero's blood.");
}
}
OCR Computer Science for A Level Chapter 6
© Hodder & Stoughton Limited 2015
OCR Computer Science for A Level
Teaching and Learning Resources
Questions
2. Create a goblin class. Goblins like to collect gold so ensure they have a goldCoins attribute,
storing how many they have, and a method for them to tell the program how many they have.
3. Goblins are noisy eaters – override the eat method to reflect this.
Answer
class Goblin extends Monster
{
private int goldCoins;
public Goblin(boolean givenPoisonous, int givenStrength, String givenName, int
givenGoldCoins)
{
}
super(givenPoisonous, givenStrength, givenName);
goldCoins=givenGoldCoins;
public void announceGoldCoins()
{
System.out.println("I have "+goldCoins+" gold coins.");
}
public void eat()
{
System.out.println("Chomp, splutter, munch");
}
}
Page 95
Question
Extend the code from the previous questions to create a monster zoo and send your monsters to
sleep.
Answer
Changing the previous main method:
public static void main (String[] args)
{
Monster monsterOne = new Monster(true, 7, "Josh");
Monster monsterTwo = new Monster(false, 14, "Steve");
Goblin goblinOne = new Goblin(false, 2, "Frank", 103);
Vampire vampireOne = new Vampire(true, 12, "Dracula");
Vampire vampireTwo = new Vampire(false, 10, "Norman");
vampireTwo};
Monster[] zoo = {monsterOne,monsterTwo, goblinOne, vampireOne,
for(int i=0; i<zoo.length;i++)
{
zoo[i].sleep();
}
}
OCR Computer Science for A Level Chapter 6
© Hodder & Stoughton Limited 2015
OCR Computer Science for A Level
Teaching and Learning Resources
Page 96
Question
Using the Monster class you made earlier, use encapsulation to ensure the strength can only be
set to a value between one and twenty.
Answer
Strength must be set as private.
The following setter class is added to the Monster class:
public void setStrength(int entry)
{
if(entry>20) strength=20;
else if(entry<1) strength=1;
else strength=entry;
}
To ensure an invalid strength isn’t given through the constructor, the constructor is
amended to:
public Monster(boolean givenPoisonous, int givenStrength, String givenName)
{
poisonous=givenPoisonous;
setStrength(givenStrength);
name=givenName;
}
OCR Computer Science for A Level Chapter 6
© Hodder & Stoughton Limited 2015