Uploaded by Mr R Zapata

Greenfoot test blank

advertisement
Name:
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.
1.
___________
2.
Write an expression to have Greenfoot randomly return a number
between 1 and 75.
Write a boolean expression for Greenfoot to check if the “up” arrow
has been pressed.
2.
________________________
3.
________________________
4.
Write an expression method for Greenfoot to play the sound
“laugh.wav”.
4. _______________________________
5.
Write an expression that uses a getRandomNumber( ) call and
returns an integer value between -15 and 34.
5. _____________________________
6.
Write an expression that changes gunReloadTime = 20; to fire
twice as fast.
6.
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.
has eaten each time a Lobster eats a Crab.
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.
II.
MULTIPLE CHOICE. (30 POINTS TOTAL – 3 Points Each)
3.
___________
______
_________________________
___________________________
___________
_________
1.
Which of the following methods is true 20% of the time?
A.
C.
2.
Greenfoot.getRandomNumber(20) < 10
Greenfoot.getRandomNumber(50) < 20
0
B.
1
C. 2
When a worm acts, it does nothing.
When a Lobster sees a Crab, it eats it.
When a Crab sees a Worm, it eats it.
When a Lobster sees a Worm, it eats it.
Greenfoot.playSound(“slurp.wav”);
Greenfoot.playSound.slurp.wav
B.
D.
Greenfoot.playSound(slurp.wav);
Greenfoot.playSound(“slurp”.wav);
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?
C. if (numWorms > 5)
{ Greenfoot.Stop( ) };
D. if (numWorms < 5)
{ Greenfoot.Stop( ) };
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.
_ 8.
D. 3
B.
D.
A. if (numWorms == 5) B. if (numWorms < = 5)
{ Greenfoot.Stop( ) };
{ Greenfoot.Stop( ) };
_ 7.
D.
Which of the following method calls contains the proper syntax for playing the “slurp” sound?
A.
C.
_6.
C.
Which of the following statements is false in regards to the Little Crab scenario?
A.
C.
_5.
B.
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.
_4.
Greenfoot.getRandomNumber(50) < 10
Greenfoot.getRandomNumber(80) < 20
In which of the following situations will the canMove method of Leaves-and-Wombats return true?
A.
3.
B.
D.
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);
B. turn(left, 30);
C. turn(30);
D. turn– (30);
9.
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);
_ 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);
}
}
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()
{
}
}
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();
}
}
public void lookForMouse()
{
public void spinAround()
}
D.
{
}
} for the following methods of the Dog class:
This scenario will be a two-player game. Complete the code
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()
{
}
Download