ICE 4 Worksheet SOLUTIONS

advertisement
BIT115 – LECTURE 4 – ICE SOLUTIONS – PARTS 1, 2, 3
Making Decisions: If, If/Else Statements
Note: Please keep the programs that you create today, in case you have a dispute about your grades for
the ICEs at the end of the quarter. When you're working with a partner, each person should save their
own, individual copy
Part 1: Boolean Expressions
Evaluate the following Boolean expressions for a Robot. Assume the robot is on intersection (1,
5) facing north. There is a Wall immediately in front of it. In each case your answer will be
either true or false. Highlight or circle the correct answer.
a. this.getAvenue() > 0 TRUE or FALSE
b. this.getAvenue() <= 5 TRUE or FALSE
c. this.getStreet() != 1 TRUE or FALSE
d. !(this.getStreet() == 1) TRUE or FALSE
e. this.frontIsClear() TRUE or FALSE
f. !this.frontIsClear() TRUE or FALSE
g. !!this.frontIsClear() TRUE or FALSE
h. this.frontIsClear() == false TRUE or FALSE
Page 1
Part 2: New Methods
1. Write a pair of methods, as follows:
a) carryExactlyEight ensures a robot is carrying exactly eight things in its
backpack. Assume the robot is on an intersection with at least eight
things that can be picked up.
Students may miss that the robot can start with more than eight things.
public void carryExactlyEight()
{ // put down extra things, if any
while(this.countThingsInBackpack() > 8)
{ this.putThing();
}
// pick up things if we didn’t start with 8
while(this.countThingsInBackpack() < 8)
{ this.pickThing();
}
}
b) Generalize carryExactlyEight to carryExactly. The new method will take a
parameter specifying how many Things the robot should carry.
public void carryExactly(int num)
{ // put down extra things, if any
while(this.countThingsInBackpack() > num)
{ this.putThing();
}
// pick up things if we didn’t start with num
while(this.countThingsInBackpack() < num)
{ this.pickThing();
}
}
Page 2
2. Write a new robot method, faceNorth. A robot that executes faceNorth will turn
so that getDirection returns Direction.NORTH.
a) Write faceNorth so that the robot turns left until it faces north. Use several
if statements.
public void faceNorth()
{ if (this.getDirection() == Direction.EAST)
{ this.turnLeft();
}
if (this.getDirection() == Direction.SOUTH)
{ this.turnLeft();
this.turnLeft();
}
if (this.getDirection() == Direction.WEST)
{ this.turnLeft();
this.turnLeft();
this.turnLeft();
}
}
b) Write faceNorth so that the robot turns left until it faces north. Use a while
statement.
public void faceNorth()
{ while (this.getDirection() != Direction.NORTH)
{ this.turnLeft();
}
}
Page 3
//
//
//
//
//
ICE_04_If_Else_SOLUTION
#####################################
BIT115 - Introduction to Programming
In Class Exercise, Lecture 4
#####################################
import becker.robots.*;
import java.util.Random; // to use the "Random" class
class RobotThatFollowsThePipe extends Robot
{
public RobotThatFollowsThePipe(City c, int st, int ave, Direction dir, int num)
{
super(c, st, ave, dir, num);
}
// The command to follow the pipe
public void FollowThePipe()
{
this.goToNextTurn();
this.goToNextTurn();
this.goToNextTurn();
this.goToNextTurn();
this.goToNextTurn();
this.goToNextTurn();
}
// The command to turn Right
public void turnRight()
{
this.turnLeft();
this.turnLeft();
this.turnLeft();
}
// The command to move the robot to the next turn
public void goToNextTurn()
{
this.move();
this.move();
this.move();
this.move();
// Check we can pick something up. If we can, then turn left, if not turn right
if( this.canPickThing() )
{
this.turnLeft();
}
else
{
this.turnRight();
}
// Make sure front is clear. If not, (like in the last straight section) then turn left once.
if( !this.frontIsClear() )
{
this.turnLeft();
}
}
}
Page 4
public class ICE_04_If_Else_SOLUTION extends Object
{
public static void hallwayN(City c, int st, int ave, int streetsNorth)
{
for(int i = 0; i < streetsNorth; i++)
{
new Wall(c, st - i, ave, Direction.WEST);
new Wall(c, st - i, ave, Direction.EAST);
}
}
public static void hallwayE(City c, int st, int ave, int streetsEast)
{
for(int i = 0; i < streetsEast; i++)
{
new Wall(c, st, ave+i, Direction.NORTH);
new Wall(c, st, ave+i, Direction.SOUTH);
}
}
public static
{
hallwayE(c,
new Wall(c,
new Wall(c,
new Wall(c,
void setupCity(City c)
4,
4,
4,
4,
2,
1,
1,
1,
3);
Direction.NORTH);
Direction.WEST);
Direction.SOUTH);
hallwayN(c, 3, 5, 3);
new Wall(c, 4, 5, Direction.EAST);
new Wall(c, 4, 5, Direction.SOUTH);
new Thing(c, 4, 5);
hallwayE(c, 0, 6, 3);
new Wall(c, 0, 5, Direction.NORTH);
new Wall(c, 0, 5, Direction.WEST);
hallwayN(c, 3, 9, 3);
new Wall(c, 0, 9, Direction.EAST);
new Wall(c, 0, 9, Direction.NORTH);
hallwayE(c, 4, 10, 8);
new Wall(c, 4, 9, Direction.WEST);
new Wall(c, 4, 9, Direction.SOUTH);
new Thing(c, 4, 9);
new Wall(c, 4, 17, Direction.NORTH);
new Wall(c, 4, 17, Direction.EAST);
new Wall(c, 4, 17, Direction.SOUTH);
}
public static void main(String[] args)
{
City toronto = new City(6, 20);
RobotThatFollowsThePipe Jo = new RobotThatFollowsThePipe(toronto, 4, 1, Direction.EAST, 0);
setupCity(toronto);
Jo.FollowThePipe();
}
}
Page 5
Download