没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
59页
GridWorld案例提供了一个图形化环境用于可视化对象在二维网格中的交互。在这个案例中,你将设计和制造各种Actor的对象,将它们添加到一个网格中,并且根据一定的规则决定Actor的行为。此外我们还增加了三个扩展任务,ImageReader、N-Puzzle(华容道)和MazeBug(迷宫)。ImageReader实验要求参训学生利用本实验软装置,实现一个利用二进制流读取Bitmap图像,并且能够进行简单地处理和保存的软件;N-Puzzle实验要求参训学生利用广度优先搜索算法和启发式搜索算法完成重拼图游戏;MazeBug实验要参训学生采用深度优先搜索算法就可以走出迷宫。此项目要求参训学生在短时间内自学新的程序设计语言和相关工具,可以训练参训学生的快速学习的能力。通过此项目实训能让参训学生掌握在Linux环境下进行代码编写、程序调试和测试的基本知识,并且掌握使用JDK进行Java程序设计,用Ant进行程序编译,部署和测试,以及掌握单元测试的基本知识,能使用JUnit编写测试用例。同时扩展任务可以锻炼参训学生图像处理能力和学习、理解、应用深度优先搜索算法,广度优先搜索算法的能力。
资源推荐
资源详情
资源评论
© 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)
and www.collegeboard.com/apstudents (for AP students and parents).
GridWorld
AP
®
Computer Science
Case Study
Solutions Manual
__________________________________________________________________
The AP
®
Program wishes to acknowledge and to thank
Judith Hromcik of Arlington High School in Arlington, Texas.
Part 1
© 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)
and www.collegeboard.com/apstudents (for AP students and parents).
2
Part 1 Answers:
Do You Know?
Set 1
1. Does the bug always move to a new location? Explain.
No. A bug will only move to the location in front of it if the cell exists and is empty or if there is a flower in the
cell.
2. In which direction does the bug move?
A bug attempts to move forward.
3. What does the bug do if it does not move?
When a bug cannot move, it turns 45 degrees to the right.
4. What does a bug leave behind when it moves?
A bug leaves a flower in its old cell when it moves to a new cell. The flower is the same color as the bug.
5. What happens when the bug is at an edge of the grid? (Consider whether the bug is facing the edge as well as
whether the bug is facing some other direction when answering this question.)
If a bug is facing the grid edge and it is told to act, it will turn 45 degrees to the right. When told to act again, it
will turn another 45 degrees to the right.
If a bug is facing the grid edge and it is told to move, it will remove itself from the grid and a flower will
replace the bug in that location.
6. What happens when a bug has a rock in the location immediately in front of it?
The bug turns 45 degrees to the right.
7. Does a flower move?
No.
8. What behavior does a flower have?
A flower “wilts” over time. The color of a flower gets darker until it turns a dark gray.
9. Does a rock move or have any other behavior?
No. A rock stays in its location and does not appear to have any other behaviors when the step or run button is
used.
10. Can more than one actor (bug, flower, rock) be in the same location in the grid at the same time?
No. A location in the grid can contain only one actor at a time.
Part 1
© 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)
and www.collegeboard.com/apstudents (for AP students and parents).
3
Exercises (Page 8)
1. Test the
setDirection method with the following inputs and complete the table, giving the compass
direction each input represents.
Degrees Compass Direction
0 North
45 NorthEast
90 East
135 SouthEast
180 South
225 SouthWest
270 West
315 NorthWest
360 North
2. Move a bug to a different location using the moveTo method. In which directions can you move it? How far
can you move it? What happens if you try to move the bug outside the grid?
A bug can be moved to any valid location in the grid using the moveTo method. When moving the bug using
the moveTo method, the bug will not change its original direction. The setDirection method or turn
method must be used to change a bug’s direction.
Attempting to move a bug to a location outside of the grid (an invalid location for the grid) will cause an
IllegalArgumentException to be thrown.
3. Change the color of a bug, a flower, and a rock. Which method did you use?
The setColor method.
4. Move a rock on top of a bug and then move the rock again. What happened to the bug?
When a rock is moved “on top” of a bug, the bug disappears. When the rock is moved to another location, the
bug is no longer there, or anywhere else in the grid. When a new actor moves into a grid location occupied by
another actor, the old actor is removed from the grid.
Part 2
© 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)
and www.collegeboard.com/apstudents (for AP students and parents).
4
Do You Know?
Set 2
1. What is the role of the instance variable sideLength?
The sideLength instance variable defines the number of steps a BoxBug moves on each side of its box.
2. What is the role of the instance variable steps?
The steps instance variable keeps track of how many steps a BoxBug has moved on the current side of its
box.
3. Why is the turn method called twice when steps becomes equal to sideLength?
When a BoxBug travels sideLength steps, it has to turn 90 degrees to travel along the next side of its
box. The turn method only executes a 45 degree turn; therefore it takes two turn method calls to turn 90
degrees.
4. Why can the move method be called in the BoxBug class when there is no move method in the
BoxBug code?
The BoxBug class extends the Bug class, and the Bug class has a public move method. Since the
BoxBug class is a subclass of the Bug class, it inherits the move method from the Bug class.
5. After a BoxBug is constructed, will the size of its square pattern always be the same? Why or why not?
Yes. When a BoxBug is constructed, the side length is determined and cannot be changed by client code.
6. Can the path a BoxBug travels ever change? Why or why not?
Yes. If another Actor, like a Rock or Bug, is in front of a
BoxBug when it tries to move, the BoxBug
will turn and start a new box path.
7. When will the value of steps be zero?
Initially, the value of steps is set to zero when a BoxBug is constructed. After that, the value of steps
will be set to zero when steps is equal to sideLength—meaning the BoxBug has completed one side
of its box path, or when the BoxBug cannot move and turns instead to start a new box path.
Part 2
© 2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals)
and www.collegeboard.com/apstudents (for AP students and parents).
5
Part 2 Exercises
1. Write a class CircleBug that is identical to BoxBug, except that in the act method the turn
method is called once instead of twice. How is its behavior different from a BoxBug?
import info.gridworld.actor.Bug;
public class CircleBug extends Bug
{
private int steps;
private int sideLength;
public CircleBug(int n)
{
sideLength = n;
}
public void act()
{
if (steps < sideLength && canMove())
{
move();
steps++;
}
else
{
turn();
steps = 0;
}
}
}
The path of a CircleBug is an octagon, instead of a square.
剩余58页未读,继续阅读
清风抚菊
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页