computer science
Main Ideas: Constructors, Methods, Classes, Conditionals, Loops
Study the Acrobat class code:
public class Acrobat extends Athlete
{
public Acrobat(int x, int y) //header
{
super(3, x, Display.NORTH, y); //body
}
public void flip()
{
turnLeft();
turnLeft();
move();
}
}
How many constructors does the Acrobat class define?
2
What part of the Acrobat constructor definition do you look at to determine how to use the constructor?
Acrobat (this)
When using the Acrobat constructor, how many arguments/parameters can you put in?
2
When using the Acrobat constructor, what type of arguments/parameters must you put in?
Number of beepers y cord
5. What do the arguments in the Acrobat constructor represent?
The coordinate where the y coordinate is and the number of beepers
6. What if the body for the constructor were written like this:
super(x, y, Display.NORTH, 1);
What would the parameters represent in this case?
The x and y coordinates
7. What if the body for the constructor were written like this:
super(x, 4, y, 1);
What would the parameters represent in this case?
The x coordinate and the way the robot starts facing.
8. Write a constructor for a Sprinter class (that extends Athlete) to start at a user defined coordinate, face east, and possess 3 beepers. Remember, Athlete only has a default constructor (0-argument) and a 4- argument constructor.
Public sprinter(int x, int y) {
super(x,y,Display.EAST,3);
}
9. Write a default constructor for a Dog class that faces west and has 10 bones (beepers) in its possession. Dogs always begin at (1, 3).
Public dog(){
super(1,3,Display.WEST,10);}
10. Write a 2-argument constructor for a Dog class that can face any directions and be on any x-coordinate. It will always begin at a y- coordinate of 1 and it will carry 5 beepers.
Public dog(int x,int dir){
super(x,1,dir,5);
}
11. How many times does each loop iterate?
a) for (int x = 0; x < 5; x++) ___5____
b) for (int n = 4; n < 11; n++) 6___
c) for (int y = 5; y <= 10; y++) ___6____
d) for (int b = 10; b >3; b--) ___7____
e) for (int j = 10; j > 0; j++) ____inf___
f) for (int n = 3; n < 3; n++) ____0___
g) for (int k = 1; k < 20; k++) ___0____
12. Using a for loop, write the appropriate code for an Athlete bob to pick up exactly 8 beepers.
for(int i = 0; i<8; i++)
bob.pickBeeper();
13. Using a while loop, write the appropriate code for a Robot sara to move until she reaches a wall.
While (sara.frontisclear()){
sara.move();
}
14. Using a loop, write the appropriate code for a Racer noah to face west.
for(int i = 0; i<2; i++)
noah.turnright();
// didnt know that was a function
15. Write a definite loop that will have oscar the Athlete put breadcrumbs down on each step as he travels a straight path from (1, 1) to street (1, 10). oscar should also stop on (1, 10).
for(int i = 0; i<9; i++){
oscar.move();
oscar.putBreadcrumbs();}
16. Use an if-else if statement to make karel (a Robot) turn left if her front is blocked by a wall, move if she is not on top of a beeper, or turn around if she is on top of a beeper. Karel should do only one or none of the options.
If (!karel.frontIsClear()){
karel.turnLeft();
}
If else (!karel.nextToBeeper()){
karel.move();
}
If else (karel.nextToBeeper()){
karel.turnAround();/ turn around 2x
}
17. Using a loop, write the code for a Robot albert to walk in a straight line, dropping one beeper at each space until he gets to a beeper. Keep count of how many beepers are dropped. Print out how many total beepers were dropped after the Robot albert stops at the last beeper. Assume the Robot albert has an infinite number of beepers.
Int count = 0
While (!albert.nexttobeeper()){
albert.putbeeper();
albert.move();
count++
}
sop(“albert has” +count+);
18. Write an instance method, called followWall, for the Athlete class that includes an indefinite loop to follow a wall of unknown length. The picture below shows the starting and ending situation.
Public void followWall(){
while(left is not clear!()){
move();}
19. Rewrite the followWall method as a class (static) method for a driver class that takes in an Athlete object parameter.
public static void followWall(temp){
while(!temp.leftisclear()){
temp.move();}
20. Given an Athlete object named olivia, write the line of code to call the instance method followWall()and write the line of code to call the class method followWall to make olivia follow a wall.
Instance method call:
olivia.followWall();
Class method call:
followWall(olivia);