GWLabA03

advertisement
AP Computer Science GridWorld Case Study (GWCS)
GWLabA03
Investigate the BoxBug Behavior and Creating an OctagonBug
Lab Objectives
Learn how to create a workspace with multiple projects.
Observe the behavior of the BoxBug object.
Learn the difference between an unbounded world and a bounded world.
Remove and add BoxBug objects, with different parameters, during program execution.
Alter the BoxBug class to become an OctagonBug class which follows an octagon display pattern.
Lab Prerequisites
Completed ExpoJava, Chapter 04, started Chapter 05 and GWLabA02
Understand fundamental decision control structures with single conditions.
Lab Sequence of Steps
#
Actions
Create
a
Workspace
and
Projects
for GWLabA03
01a
Click File, New, Project and Blank Workspace
Type GWLabA03 in the Name window.
Browse to C:\GridWorldStudents\GWLabsA\GWLabA03.
This is shown in Figure 01.
Click Finish.
Figure 01
Comments
You may have noticed that JCreator creates a
Workspace automatically, even when you only
make one project.
There are times when it is convenient to work with
multiple projects. The projects may be similar, as is
the case with GWLabA03 or there may be multiple
solutions tested for the same problem.
You do need to keep in mind that you can only
compile a project. It is not possible to compile a
Workspace. Compiling and executing a specific
project requires that you make the desired project
active. This is shown later in the lab.
GWLabA03 Page 19 Updated 09-28-07
01b
Create a Workspace and Projects for GWLabA03
Click Project, New, Project and Empty Project.
Type GWLabA03-1 in the Name window and browse to:
C:\GridWorldStudents\GWLabsA\GWLabA03\GWLabA03-1.
This is shown in Figure 02.
Click Next.
The project manager does not care how files are
organized, but it makes sense to be neat and place
the files for each project in its own folder.
In this case the Workspace is placed in GWLab03
and each one of the projects becomes a subfolder,
called GWLabA03-1 and GWLabA03-2.
Figure 02
01c
Create a Workspace and Projects for GWLabA03
Click Next, Required Libraries, check the box to attach the JAR file.
Click Finish twice.
You should be at Figure 03.
The special GridWorldJarFile is not attached to a
Workspace. It is attached to each project. The
process of indicating a required library and checking
the box of the JAR file needs to be done each time a
new project is created.
Right now the JAR file is attached to the
GWLabA03-1 project. You will need to attach the
JAR file again for the GWLabA03-2 project.
Figure 03
GWLabA03 Page 20 Updated 09-28-07
01d
Create a Workspace and Projects for GWLabA03
Click Project, New, Project and Empty Project.
Type GWLabA03-2 in the Name window and browse to:
C:\GridWorldStudents\GWLabsA\GWLabA03\GWLabA03-2.
This is shown in Figure 04.
Click Next.
In GWLabA03 you will work with two different
Bug classes. The GWLaA03-1 folder contains files
for the BoxBug class. This class has a bug that
moves in a square or "box" style.
The GWLabA03-2 folder contains files for the
OctagonBug class. This class has a bug that needs
to move in an octagon pattern.
Figure 04
01e
Create a Workspace and Projects for GWLabA03
Click Next, Required Libraries, check the box to attach the JAR file.
Click Finish twice.
You should be at Figure 05.
There are now two projects, GWLabA03-1 and GWLabA03-2
in the GWLabA03 Workspace.
Figure 05
Add the required library to the second project and
you now have two projects that are ready to compile
and execute.
Do keep in mind that setting up a Workspace and
projects ever so carefully does not insure that
compiling and execution will follow. The code in
the files added to the project must be correct.
You also need to realize that there are two types of
correctness. First the sentence structure or syntax of
the program statements must be correct. If the
syntax is wrong you will get a list of compile errors
and it is not possible to execute the program.
Second, after the project compiles, you only know
that the syntax of the files is correct. This does not
mean that the program will execute correctly. You
need to observe the execution carefully to check for
logic errors.
GWLabA03 Page 21 Updated 09-28-07
02a
Compile and Execute the GWLabA03-1 Project
In the File View window, right-click GWLab03-1.
Click Sets as Active Project.
Compile and Execute the project to get to the display in Figure 04.
At this stage there is little difference between the Bug objects
of the earlier labs and the current BoxBug objects.
Figure 06
When there are multiple projects in one Workspace,
one of the projects is always active. When the
second project, GWLabA03-2, was created it
automatically became active.
Now we want to return to the first project,
GWLabA03-1, and observe its behavior. We make
the first project active with Project and Set Active
Project.
You can also Right-click on the project name in the
File View window and then select Sets As Active
Project. Either way accomplishes the same switch
to the newly desired project.
Keep in mind that it is irrelevant what file is
displayed in the edit window. You can double-click
on any file to make it display in the edit window.
This feature allows you to check the program code
and make corrections and changes. It does not
control what is compiled and executed. This is
different from the File Compile/Execute approach,
which you learned earlier.
02b
Compile and Execute the GWLabA03-1 Project
Click Run and your will see two square paths, shown in Figure 07.
Figure 07
You will observe that two lovely bugs are both
running around in a "box" pattern. Observant
students of mathematics may argue that these bugs
are making a two-dimensional square pattern and
not a three-dimensional box pattern.
Members of the CollegeBoard Test Development
Committee selected the name BoxBug. The
relevance to computer science concepts stays the
same, even if it was called AardvarkBug.
GWLabA03 Page 22 Updated 09-28-07
03a
Run an Altered Execution Path
Close the GridWorld window and execute the project again.
Right-click on the blue bug and click the removeSelfFromGrid method.
Right-click on the red bug and click on the moveTo method. Enter (9,4).
Figure 06 shows that the blue bug is gone and the red bug is at (9,4).
The changes you make at this stage allows you to
observe that the bug will self-destruct if it is not
capable of moving properly. You are intentionally
moving the bug to such a location that it is not
possible to fit the square in the grid space.
Figure 08
03b
Run an Altered Execution Path
Click Run and watch the execution again.
The bug will remove itself when it runs into the wall shown in Figure 09.
Figure 09
The moment the bug runs into the wall, it removes
itself from the GridWorld. It is possible that you
will see or may have seen the behavior of the
BoxBug provided with the original files of the
GridWorld Case Study. In the original bug files
there is protection, which prevents bug self
destructing when there is no path available.
The files in this GWCS teaching unit have
intentionally been altered in various ways to observe
certain behavior. The protection against selfdestruction, provided in the original files has been
removed to observe this behavior and also to make a
point about bounded and unbounded GridWorlds.
GWLabA03 Page 23 Updated 09-28-07
4a
Execute with an Unbounded GridWorld
Close the current GridWorld window and repeat the previous execution.
When the red bug is by itself at (9,4), do not Run yet.
Click on World and Set grid to get to Figure 10.
With the selection of an unbounded matrix our bug
should not be running into any walls and selfdestruct.
Figure 10
4b
All the GridWorld executions have been performed
in the default 10 X 10 matrix. It is possible to
specify a different size matrix. It is also possible to
use an unbounded matrix.
Execute with an Unbounded GridWorld
Select the UnboundedGrid and Run the GridWorld again.
This time there is plenty of room as you see in Figure 11.
Figure 11
GWLabA03 Page 24 Updated 09-28-07
Our bug is much smaller, but we saved its life in the
new, and unbounded environment.
You can try different execution and you observe that
the bug no longer self-destructs.
05a
Switch to the Octagon Bug Class
An important aspect of computer programming is the ability to alter
existing code. Code may require change because it is faulty or the
program code is changed to reflect new ideas and improvements.
Many lab assignments with the GridWord Case Study provide you with
step-by-step instructions to help you investigate features of the complex
GridWorld total program.
The second half of this lab assignment is the first occurrence where you
are expected to make changes in existing code.
During the first half of the lab assignment you observed the behavior of
the BoxBug class. Taking this code, you are now expected to make the
appropriate changes so that the bug does not follow a square pattern, but
rather an octagon pattern.
Making ever more complex changes to the GridWorld code will become a
common experience in the future lab assignments.
05b
Prepare the OctagonBug Files
The time has come to create some new bug behavior.
Our goal is to make a bug move in an octagon pattern.
This exercise will not create a new class from scratch.
We will use the BoxBug class to create the new OctagonBug class.
In the File View window, right-click GWLab03-2.
Click Sets as Active Project.
Place the OctagonBugRunner.java file in the edit window.
This is the previous BoxBugRunner.java file with a new name.
Alter the file and change every occurrence of BoxBug to OctagonBug.
Delete some lines and some details to get Figure 12.
Save the file.
Figure 12
GWLabA03 Page 25 Updated 09-28-07
The BoxBug class was ready to go. You created the
proper project and then compiled and executed.
This is not exactly true for the OctagonBug class.
The files in your GWLabA03-2 folder are named
OctagonBug but that is all. Everything else is
exactly the same as the files of the BoxBug classes.
Your first job is to change all the former BoxBug
names to OctagonBug. Additionally, you need to
alter the manner in which the bug is tested.
The bug is meant to run an octagon pattern. We
wish to see this pattern on a large bounded grid.
This requires that we have a bug starting at a precise
location to make this pattern fit properly.
5c
Prepare the OctagonBug Files
Place the OctagonBug.java file in the edit window.
This is the previous BoxBug.java file with a new name.
The previous step changed the BoxBugRunner file.
Now you need to make changes to the BoxBug file
and you will be ready to observe the behavior of this
new bug.
Alter the file and change every occurrence of BoxBug to OctagonBug.
Save the file when you get to Figure 13.
Figure 13
6
Compile and Execute the OctagonBug Class
Compile and Execute as you have done previously to get Figure 14.
Well it is great that you executed an OctagonBug class, there is no
evidence of any octagon pattern in the output display.
There is the square pattern behavior you saw with the BoxBug class.
Figure 14
This little demonstration brings about a very
important lesson about computer programming.
Computers do not care about names and they do not
understand names.
Computers only understand a sequence of
instructions. You can call a program DrawMouse
and then create instructions, which actually draw a
tiger or an elephant.
It is absolutely impossible for the program to
understand what you want except for the set of
instructions. Program names, class names and
variable names are all selected for the convenience
of the programmer. The computer does not care.
In this case the only changes we have made to the
previous BoxBug files is a substitution of every
occurrence of BoxBug with OctagonBug. This will
not alter the behavior.
GWLabA03 Page 26 Updated 09-28-07
7a
Alter the OctagonBug.java File to Display Octagon Behavior
The goal of the OctagonBug is to display an octagon like Figure 15.
One thing is clear. Changing the file names, class names and the
constructor names from BoxBug to Octagon does not change the
display.
Figure 15
GWLabA03 Page 27 Updated 09-28-07
The drawing shows precisely what the OctagonBug
needs to perform. It needs to march around making
eight turns and leave behind a path of flowers in a
lovely octagon pattern.
7b
Alter the OctagonBug.java File to Display Octagon Behavior
The OctagonBugRunner.java file is a file that runs and tests the
OctagonBug class. It does not control the behavior. The OctagonBug
class is shown in Figure 16. As it stands right now it displays squares.
Examine the class and determine how the square display is accomplished.
What needs to be done and changed to create an octagon display?
Figure 16
8
Lab Assignment
If you complete this assignment and show that you have created two
separate projects inside a work space and you can switch to an unbounded
grid, then you have earned 80 points.
For 100 points change the Octagon.java file so that it will make an
octagon path of flowers, like you see in Figure 15.
GWLabA03 Page 28 Updated 09-28-07
The code of the previous BoxBug class is not that
complicated at this stage. You need to make
changes in the provided file so that the behavior is
altered.
Download