Name: Answer Key Class:______ Date: COMPUTER

advertisement
Name:
Answer Key
Class:____________
Date:______________
COMPUTER PROGRAMMING UNIT TEST: CHAPTERS 1-4
Note: It matters whether:
a) it’s in upper or lower case;
b) whether there is a pair of parentheses;
c) whether there is a semicolon (;) at the end;
d) whether your answer includes if ( … ) or if the answer doesn’t ask for that;
e) In short, because you’re instructing a machine, not a human, any error in the language will cause your program not
to run as intended.
Understand those seemingly esoteric differences. They may mean the difference between being able to use the language
and not.
SHORT ANSWER. (20 Points Total – 2 Points Each)
1.
Write an expression to have an object turn 25 degrees in Greenfoot.
2.
Write an expression to have Greenfoot randomly return a number
2. Greenfoot.getRandomNumber(75)+1;
between 1 and 75. // There are 75 numbers from 1 to 75. So: getRandomNumber(75). But instead of starting from
0, we need to start from 1. So add 1 to the returned value.
Write a boolean expression for Greenfoot to check if the “up” arrow
3.
Greenfoot.isKeyDown (“up”)
has been pressed.
3.
1.
Greenfoot.playSound(“laugh.wav”);
4.
Write an expression method for Greenfoot to play the sound
“laugh.wav”.
5.
Write an expression that uses a getRandomNumber( ) call and
5. Greenfoot.getRandomNumber(50) – 15
returns an integer value between -15 and 34. // Again, first figure out how many numbers there are between -15
and 34 (remember the 0). Then adjust based on the starting value.
Write an expression that changes gunReloadTime = 20; to fire
6.
gunReloadTime = 10;
twice as fast.
6.
4.
turn(25);
7.
Write an expression that would place a Lobster in Crab World
200 cells from the left edge and 300 cells down from the top edge.
7.
8.
Write an expression that would increase the number of Crabs a Lobster 8.
crabsEaten = crabsEaten + 1;
has eaten each time a Lobster eats a Crab. // Or, crabsEaten += 1; or, crabsEaten++;
9.
Write a Boolean expression that would have a Lobster see if it has
found a Worm.
9.
10.
Write an expression which would stop a game being played in
Greenfoot.
10.
addObject( new Lobster(), 200, 300 );
canSee(Worm.class)
Greenfoot.stop( );
II.
MULTIPLE CHOICE. (30 POINTS TOTAL – 3 Points Each)
B 1. Which of the following methods is true 20% of the time?
A.
C.
Greenfoot.getRandomNumber(20) < 10
Greenfoot.getRandomNumber(50) < 20
B.
D.
Greenfoot.getRandomNumber(50) < 10
Greenfoot.getRandomNumber(80) < 20
C 2. In which of the following situations will the canMove method of Leaves-and-Wombats return true?
A.
B.
C.
D.
C 3. What numerical value must be entered when the wombat.setDirection( ) method
is called to make the wombat face up like in the picture at the right?
A.
D 4.
1
C. 2
When a worm acts, it does nothing.
When a Lobster sees a Crab, it eats it.
B.
D.
Greenfoot.playSound(“slurp.wav”);
Greenfoot.playSound.slurp.wav
B.
D.
C. if (numWorms > 5)
{ Greenfoot.Stop( ) };
D. if (numWorms < 5)
{ Greenfoot.Stop( ) };
addObject( new Worm( ), Greenfoot.getRandomNumber(550), Greenfoot.getRandomNumber(550) );
addObject( new Worm( ), Greenfoot.getRandomNumber( ), Greenfoot.getRandomNumber( )) );
addObject( new Worm( ), Greenfoot.getRandomNumber(550) );
addObject( new Worm( ), Greenfoot.getRandomNumber(550), in CrabWorld);
Which of the following turn( ) method calls will turn an object to the left 30?
A. turn(–30);
D 9.
Greenfoot.playSound(slurp.wav);
Greenfoot.playSound(“slurp”.wav);
Which of the following lines of code will add a Worm at a random place in Crab World in the Little Crab scenario?
A.
B.
C.
D.
A 8.
When a Crab sees a Worm, it eats it.
When a Lobster sees a Worm, it eats it.
A method called numWorms has been created to return the number of Worms in the Little Crab scenario. Which
statement will end the scenario if the value of numWorms is less than 5?
A. if (numWorms == 5) B. if (numWorms < = 5)
{ Greenfoot.Stop( ) };
{ Greenfoot.Stop( ) };
A 7.
D. 3
Which of the following method calls contains the proper syntax for playing the “slurp” sound?
A.
C.
D 6.
B.
Which of the following statements is false in regards to the Little Crab scenario?
A.
C.
A 5.
0
B. turn(left, 30);
C. turn(30);
D. turn– (30);
Which of the following will randomly turn an object up to 45 in either direction, right or left?
A.
C.
turn(Greenfoot.getRandomNumber(90 – 45);
turn(Greenfoot.getRandomNumber(90) – 45);
B.
D.
turn(Greenfoot.getRandomNumber(45);
turn(Greenfoot.getRandomNumber(45) – 90);
B 10. In which of the following situations will return “true” when the boolean foundLeaf ( ) method is invoked?
A.
B.
C.
D.
III.
WRITING CODE. (50 POINTS TOTAL)
1.
A new world called BackYard has been created for a game. There are 3 actors: Dog, Cat, and Mouse.
A.
Complete the code which will automatically populate the world BackYard with 3 Dogs, 1 Cat, and 6 Mice
public class BackYard extends World
{
public BackYard()
{
super(600, 600, 1);
populateWorld();
}
public void populateWorld()
{
addObject( new Cat(), 300, 300);
addObject( new Dog(), 100, 100);
addObject( new Dog(), 400, 400);
addObject( new Dog(), 500, 150);
addObject(
addObject(
addObject(
addObject(
addObject(
addObject(
new
new
new
new
new
new
Mouse(),
Mouse(),
Mouse(),
Mouse(),
Mouse(),
Mouse(),
Answers will vary because the
numbers can be whatever the
student decides.
50, 50);
75, 550);
200, 400);
300, 50);
350, 575);
550, 100);
}
}
B.
Complete the code for the following methods of the Mouse class. Add these to the Mouse’s act method.
1.
turnAtFence – the Mouse turns if he gets to the edge of the fenced in yard.
2.
randomRun – the Mouse randomly runs about the yard changing direction 10% of the time.
public class Mouse extends Animal
{
public void act()
{
move();
turnAtFence();
randomRun();
}
public void randomRun()
{
public void turnAtFence()
{
if( Greenfoot.getRandomNumber(100) < 10)
{
turn(5); // any reasonable # of deg.
}
}
}
if ( atWorldEdge() )
{
turn(20);
// or 5, or 17, etc.
}
}
C.
Complete the code for the following methods of the Cat class:
1.
checkKeyPress – the Cat turns to the right when the right arrow is pressed and turn to the left
when the left arrow is pressed.
2.
lookForMouse – the Cat eats a Mouse if it finds one, play the sound “yum.wav” when it eats one.
If the Cat eats all 6 mice, the sound “fanfare.wav” is played and the game is over.
3.
spinAround – for every 3 mice the Cat eats, he spins in a circle
public class Cat extends Animal
{
// should have declared & initialized variable here: int miceEaten=0;
public void act()
{
move();
public void checkKeyPress()
{
lookForMouse();
checkKeyPress();
spinAround();
if (Greenfoot.isKeyDown(“right”)
{ turn(5); }
if (Greenfoot.isKeyDown(“left”)
{ turn(-5); }
}
}
public void lookForMouse()
{ if ( canSee(Mouse.class) )
public void spinAround()
{
if (miceEaten = 3)
{
turn(360);
}
if (miceEaten == 6)
{
turn(-360);
}
{ eat(Mouse.class);
playSound(“yum.wav”);
miceEaten = miceEaten + 1;
if (miceEaten == 6)
{ playSound(“fanfare.wav”);
Greenfoot.stop();
}
}
}
}
D.
}
}
This scenario will be a two-player game. Complete the code for the following methods of the Dog class:
1.
checkKeyPress – the Dog turns to the right when the “d” is pressed and turn to the left when the
}
“a” is pressed.
2.
lookForCat – the Dog eats a Cat if it finds one, play the sound “chomp.wav” when it eats one.
If the Dog eats the Cat, the sound “gameover.wav” is played and the game is over.
public class Dog extends Animal
{
public void act()
{
move();
checkKeyPress();
lookForCat();
}
public void checkKeyPress()
{
public void lookForCat()
{
if (Greenfoot.isKeyDown(“a”)
{ turn(-5); }
if (Greenfoot.isKeyDown(“d”);
{ turn(5); }
}
if ( canSee(Cat.class) )
{
eat(Cat.class);
playSound(“chomp.wav”);
playSound(“gameover.wav”);
Greenfoot.stop();
}
}
}
Download