Java Tutorial
(Skydive)1,
by Mike
Roam.
Revised Dec.2'98, printed 12/8/98. Doc
1 of 4
(Doc 1) (Doc
2) (Doc
3) (Doc
4)
- Java & Spotcheck editor
- Create Plane class
- Fields:
altitude, heading,
speedVertical, locNorth, etc.
- Methods: move(),
openTheDoor()
- Create Chute class
- Fields:
altitude, heading,
speedVertical, locNorth, etc.
- Methods: move(),
openTheChute(), cutaway()
- Create Diver class
- Fields:
altitude, heading
- Methods: move(),
openTheChute()
- sub-classing (extends)
- Create "Flyer" class
- Fields:
float altitude,
float heading,
float speedVertical,
float
speedHorizontal,
float locNorth,
float locEast.
- Methods:
move()
- Modify Plane, Chute, and Diver classes so they
"extends Flyer," and
remove the fields and methods that are duplicated by Flyer.
- Add unique (not part of Flyer) members to Plane
- Fields:
boolean
isTheDoorOpen, int
howManyEngines, int
maxNumOfDivers.
- Methods:
openTheDoor(),
closeTheDoor()
- Add unique members to Class Diver:
- Fields:
boolean
isStanding
- Methods:
openTheMainChute(),
openTheReserveChute().
- Add unique members to Chute
- Fields:
float howFarOpen
- Methods:
pullThePin(),
repackTheChute()
- Comments
- Add the following comments to Class Flyer:
- (at top of class definition:)
/* Note: all distances are
in meters. All times are in seconds. */
- (before "heading" field:)
/* North==0, East==90,
South==180, and West==270. */
- Objects HAVING objects inside
- Modify the Diver so that it has some more fields:
mainChute and
reserveChute. (Guess
what type they are... "Chute"!)
- Modify the Plane by giving it some fields that can hold
Divers: passenger1 and
passenger2. (Guess what
type they are... "Diver"!)
- writing code inside methods:
- Here are some sample commands. Don't type them,
just see what they look like:
- A basic print (output) statement:
System.out.println("wahoo");
- A basic assignment ("gets") statement:
isWeird =
true; /* notice the
single "=" sign which means "gets" */
- An "if/then" statement:
if ((X=='r') &&
(Z==3)) {
System.out.println("Let's jump " + Z + " times.")
} /* Notice the
double equals "==" for comparisons. */
- Let's make the
"openTheDoor()" method
really do something. Go to the Plane class and go into the
"openTheDoor()" method. Put in two statements:
isTheDoorOpen = true;
/* setting the door open
*/
System.out.println("The plane
just opened its door")
/* (Someday we might
have multiple planes and then we'll have to change this method
so that it also says which plane opened its door.)
*/
- Create a similar program for "closeTheDoor()"
- Fill in the "move()"
method in the Flyer class.
- Since the
"speedHorizontal"
field says how fast this Flyer moves every second, and since
"locNorth" and
"locEast" keep track
of where we are, then we can figure out where we'll be after
we move.
- Example: we're moving due North at 55 meters
per second. Our current location is 800 meters North of
the drop zone, so after we move we'll be 855
meters North of the drop zone.
- Here's the logic in English: if we're pointed North,
then increase our north location. (The speed says how far
to move.) Else If we're pointed East then increase our
East location. Else If we're pointed South,
decrease our North location. Etc. (Else if we're
pointed West, decrease the East location,
Else
System.out.println("I don't know how to go direction " +
heading + "!")
- Print out a messagesaying that we moved.
System.out.println("I
moved!")
- No matter which direction we're flying compared to the
compass, up is still up, so just add the speedVertical to
our altitude.
- Sending messages to objects
- The Diver's
"openMainChute()" method
will actually tell the Diver's mainChute to open itself. Put
the following code into the openMainChute() method:
- mainChute.pullThePin();
/* This means, "Hey mainChute, pull your own pin!" */
- System.out.println("I've
just told mainChute to pull its
pin!");
- Similar code should go into the Diver's
"openReserveChute()"
method.
- Setting initial values for fields
- All of the fields we've created should have
starting values, so that we never get clobbered by undefined
variables. Click on the little circle after each field and type
"=" and some appropriate value. Example:
float altitude = 0;
- Let's have all of the Flyer fields start with 0 (zero).
Zero altitude, zero speed, etc.
- The Diver's "Chute" field is not a float or int variable,
so it can't start off as zero. Instead, the line that used to
look like "Chute
mainChute;" will now look like
"Chute mainChute = new
Chute();".
- Creating a dropzone (main Applet with
init() method)
- Note: the current plan is to have the dropzone own
the Planes and Divers.
- At the very top of the program, fill in the "import ..."
line and create a few more as follows:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
- Create another class,
"DropZone", which is
"public" (click first
° before "class") and
"extends Applet" and has
the following fields and methods:
- Simple Fields.
float temperature, float
windSpeed, int windHeading
- Fancy Fields:
plane
ourPlane;
Diver
Ken;
Diver Barbie
- Make sure that all fields have
starting values. For example:
plane ourPlane = new
Plane();
- Methods: public
void init()
- the init() method can set the Planes and
Divers to their starting positions.
ourPlane.altitude=3000;
/* meters, == 9000 feet */
ourPlane.passenger1
= Ken;
Ken.altitude=3000;
- Set up some divers and the plane and their fields
(heading, velocity, etc.)
- Note: Eventually, the dropZone will boss the planes and
divers around, and will put buttons on the screen for the human
to press.
- Functions with parameters, For Loops
- Let's give the diver a
shout() method that has
control about how many times to shout. Declare it like this:
void shout( int
howMany)
- Inside the shout() method, we'll have a "for" loop that
repeats as many times as we want:
for (int x = 0; x<5; ++x)
{/* just kidding-use our
variable, not 5 */
/* the following code
will repeat while x is less than 5
*/
System.out.println("Wahoo!");
}
- Now you can (and should) add code to the dropZone's
init() method telling a
Diver to shout.
Ken.shout(7);
/* This will tell Ken to shout
7 times. */
- Functions with different signatures
- Let's give the diver another shout() method that
takes an extra parameter:
void shout( int howMany, String
theYell)
- This method won't shout "Wahoo!"&emdash;instead, it will
shout theYell, using
System.out.println(theYell);
- Put another shout command into dropZone's init()
method:
Ken.shout(4,"Cowabunga!");
/* Ken will shout
"Cowabunga" 4 times */
- Divers with a name.
Since many Divers might be shouting at the same time, it would be
nice if they said who they were when shouting. ("Super Ken says
Yahoo!") instead of ("Yahoo!")
- Add another field to class Diver to hold a name:
String myName
- In DropZone's init() method, give names to some of your
Divers:
Ken.myName = "Super
Ken";
- Customize the shout() methods so that they include the
Diver's name:
- Simple way is just to change the println method
inside the loop:
System.out.println(myName +
" says " + theYell);
/* "Super Ken says
wow, Super Ken says wow, Super Ken says wow"
*/
- Sophisticated way ("Super Ken says wow wow wow"):
System.out.print(myName + "
says ");
for (int i = 0; i < howMany; ++i) {
System.out.print(theYell + ", ");
}
System.out.println();
Next Lesson...