GWLabA04

advertisement
AP Computer Science GridWorld Case Study (GWCS)
GWLabA04
Changing Bug Behavior and Appearance
Lab Objectives
Continue to explore how small changes in a class can drastically alter object behavior.
Change the BoxBug class into a SpiralBall class.
Alter the appearance of the Bug object to a SpiralBall object.
Consider changes for different types of spiral behavior.
Lab Prerequisites
Completed ExpoJava, Chapter 05 and completed GWLabA03.
Understand fundamental decision control structures with single conditions.
Lab Sequence of Steps
#
Actions
Create
a
Workspace
and
two
projects
for GWLabA04
01
At this stage you should be able to create a project without requiring the
step-by-step guidance of the previous labs.
Create a Workspace, called GWLabA04, and save in GWLabA04 folder.
Create a project, called GWLabA04-1, and save in GWLabA04-1 folder.
Create a project, called GWLabA04-2, and save in GWLabA04-2 folder.
Make the GWLabA04-1 active.
Place the SpiralBallRunner.java file in the edit window, like Figure 01.
Figure 01
GWLabA04 Page 29 Updated 09-28-07
Comments
After creating a Workspace with multiple projects
and with attached JAR files several times, the
process will start to become automatic. We learn
from repetition. There also is no shame in looking
back at an earlier lab to pick up some forgotten
detail.
The SpiralBall1.java and SpiralBallRunner1.java
files, as well as The SpiralBall2.java and
SpiralBallRunner2.java files are essentially
renamed BoxBug files. The file names and the class
names do not match. It is one of the rules of Java
programming that a file must have the same
identifier (name) as the identifier of the class. This
is required to the degree of case sensitivity. If the
names are identical, but the file name is in capital
letters and the class name is in lower case letters, the
program will not compile, nor execute.
02
Observe the SpiralBall1 files
The SpiralBall1Runner.java file is ready for testing.
As happened with the previous lab exercise, you have a set of BoxBug
files that have been renamed SpiralBall1.
Load SpiralBall1.java in the edit window.
Figure 02 shows the SpiralBall1.java file before you make changes.
Change every occurrence of BoxBug to SpiralBall1.
It may seem like a silly exercise to be given files
where you have to rename identifiers. This is done
intentionally to give you practice with observing
where the key names in a program occur.
Figure 02
03a
Compile and Execute the SpiralBall1 Project
Compile the project and observe the display in Figure 04.
Figure 04
GWLabA04 Page 30 Updated 09-28-07
You have been this route before in the previous lab.
The program now compiles and a single bug
appears. You may be surprised. Is this not
supposed to be a program with a soccer ball?
After all the class is called SpiralBall1 and the
image file is called SoccerBall.gif.
03b
Compile and Execute the SpiralBall Program
Run the project and observe the display in Figure 05.
This is not very satisfactory.
First, the movement is the same old square and not a spiral.
Second, there appears to be a bug moving and not a soccer ball.
Figure 05
04a
By now you are probably not surprised that the
program behaves in a square pattern. It was
mentioned before that names have no meaning to
Java and changing the names of files, classes and
methods does not alter behavior.
This still does not explain the bug display. Have
patience, that item is next on the agenda.
Fix the Soccer Ball Gif Problem
Minimize your JCreator environment and navigate to folder:
C:\GridWorldStudents\GWCSLabsA\GWLabA04-1 folder.
You will see the files displayed by Figure 06.
The only gif you see is called SoccerBall.gif.
Change the file name to SpiralBall1.gif.
The GWLabA04-1 folder shows many files. There
are the java files with the code that is the actual
program. There are a variety of class files that are
the result of compiling java files. There are also a
variety of files with strange endings. The odd
looking files all have the project name. They are
used to organize the project.
Finally, there is one lonely gif file. It is called
SoccerBall.gif. The GridWorld program can run
without providing any image files. If none are
provided, the default display is a regular bug, like
the one you saw for the first lab.
Figure 06
This lab uses a SpiralBall1 class and first looks for
a SpiralBall.gif file. It is not available. The gif file
is intentionally misnamed to demonstrate this point.
Without the expected gif file, the program resorts to
its default bug display.
GWLabA04 Page 31 Updated 09-28-07
04b
Fix the Soccer Ball Gif Problem
Execute the program again and you will get display Figure 07.
Figure 07
04c
The SoccerBall.gif does display a ball image. It is
simply named incorrectly. After renaming the file
to SpiralBall1.gif, the bug is gone and you now see
a perfectly decent soccer ball.
Fix the Soccer Ball Gif Problem
All is well. Look at Figure 08.
There are now a nice bunch of flowers forming a square.
This means that the problem of the image is fixed.
However, there still is no spiral pattern.
Figure 08
Well the soccer ball problem is fixed. If you
understand this process correctly, you now know
that it is easy to use any type of image in your
program.
Make sure that the image has the same name as your
class. You also need to size the image 48 X 48 for a
proper fit. Other sizes work, but the gifs provided
by the GridWorld files are all 48 X 48 pixels.
The program can handle image files with the *.gif
format and the *.jpg format. Some other formats,
like *.bmp, are not accepted.
Be careful not to take an image file and casually
change the suffix to either gif of jpg. You need to
use a program, like Paint, Picture Manager or
similar program, to alter the image file format
properly. The 48X48 size is not a requirement, but
the images will look better at that size.
With the image file problem fixed, we are now left
with the problem of the square display, which must
be altered to display a spiral.
GWLabA04 Page 32 Updated 09-28-07
05
Make the Soccer Ball Move in a Spiral Pattern
Figure 09 shows what is expected of SpiralBall behavior.
What changes in the SpiralBall class will implement these changes?
Some class discussion will follow, but keep in mind you need to do this
part for a grade.
Figure 09
Figure 09 shows a spiral display. This does not
give you a solution. Many lab assignments started
with telling you everything step-by-step. Now there
will be some occasions where you need to think and
discuss how to solve a given problem. In future
problems you will do quite a bit more on your own.
First you need to learn a lot more computer science
concepts to handle such problems.
The secret in solving the spiral problem is to
understand how the provided code draws a square.
Remember that you were provided with the code of
the BoxBug class. It displays a square. Analyze
how the square is displayed and see what might be
necessary to create a spiral pattern.
6a
Make the Soccer Ball Move in a different Spiral Pattern
This lab assignment has another spiral pattern up its sleeve.
First, you will need to make the GWLabA04-2 project active.
The spiral in Figure 09 is lovely, but you want a tighter spiral.
Observe the spiral in Figure 10.
Discuss what changes are required to create this tight spiral pattern.
Can you alter the SpiralBall.java file to create the Figure 10 spiral?
Figure 10
GWLabA04 Page 33 Updated 09-28-07
Everything could have been placed in one project,
but this is more organized.
Let us assume that you finished the first spiral
problem. Perhaps you figured it out or perhaps a
class discussion provides the solution. It is also
possible that your kind teacher helped you.
The previous spiral has space between the paths.
This is actually simpler to do than the spiral shown
in Figure 10. There is no space at all. You already
have one spiral class under your belt. Can you also
figure out this more advanced version?
07
Complete GridWorld Lab 04
This concludes the guided GWLabA04 exercise.
You will now continue with the graded portion.
For 80 points you need to complete the SpiralBall.java file so that it
makes the spiral pattern of Figure 09.
For 100 points you need to alter the SpiralBall.java file so that it
makes the spiral pattern of Figure 10.
GWLabA04 Page 34 Updated 09-28-07
Download