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)

 

  1. Java & Spotcheck editor
    1. Create Plane class
      1. Fields: altitude, heading, speedVertical, locNorth, etc.
      2. Methods: move(), openTheDoor()
    2. Create Chute class
      1. Fields: altitude, heading, speedVertical, locNorth, etc.
      2. Methods: move(), openTheChute(), cutaway()
    3. Create Diver class
      1. Fields: altitude, heading
      2. Methods: move(), openTheChute()
  2. sub-classing (extends)
    1. Create "Flyer" class
      1. Fields: float altitude, float heading, float speedVertical, float speedHorizontal, float locNorth, float locEast.
      2. Methods: move()
    2. Modify Plane, Chute, and Diver classes so they "extends Flyer," and remove the fields and methods that are duplicated by Flyer.
    3. Add unique (not part of Flyer) members to Plane
      1. Fields: boolean isTheDoorOpen, int howManyEngines, int maxNumOfDivers.
      2. Methods: openTheDoor(), closeTheDoor()
    4. Add unique members to Class Diver:
      1. Fields: boolean isStanding
      2. Methods: openTheMainChute(), openTheReserveChute().
    5. Add unique members to Chute
      1. Fields: float howFarOpen
      2. Methods: pullThePin(), repackTheChute()
  3. Comments
    1. Add the following comments to Class Flyer:
      1. (at top of class definition:)
        /* Note: all distances are in meters. All times are in seconds. */
      2. (before "heading" field:)
        /* North==0, East==90, South==180, and West==270. */
  4. Objects HAVING objects inside
    1. Modify the Diver so that it has some more fields: mainChute and reserveChute. (Guess what type they are... "Chute"!)
    2. Modify the Plane by giving it some fields that can hold Divers: passenger1 and passenger2. (Guess what type they are... "Diver"!)
  5. writing code inside methods:
    1. Here are some sample commands. Don't type them, just see what they look like:
      1. A basic print (output) statement:
        System.out.println("wahoo");
      2. A basic assignment ("gets") statement:
        isWeird = true; /* notice the single "=" sign which means "gets" */
      3. An "if/then" statement:
        if ((X=='r') && (Z==3)) {
        System.out.println("Let's jump " + Z + " times.")
        }
        /* Notice the double equals "==" for comparisons. */
    2. 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.) */
    3. Create a similar program for "closeTheDoor()"
    4. Fill in the "move()" method in the Flyer class.
      1. 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.
        1. 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.
        2. 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 + "!")
      2. Print out a messagesaying that we moved. System.out.println("I moved!")
      3. No matter which direction we're flying compared to the compass, up is still up, so just add the speedVertical to our altitude.
  6. Sending messages to objects
    1. The Diver's "openMainChute()" method will actually tell the Diver's mainChute to open itself. Put the following code into the openMainChute() method:
      1. mainChute.pullThePin(); /* This means, "Hey mainChute, pull your own pin!" */
      2. System.out.println("I've just told mainChute to pull its pin!");
    2. Similar code should go into the Diver's "openReserveChute()" method.
  7. Setting initial values for fields
    1. 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;
    2. Let's have all of the Flyer fields start with 0 (zero). Zero altitude, zero speed, etc.
    3. 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();".
  8. Creating a dropzone (main Applet with init() method)
    1. Note: the current plan is to have the dropzone own the Planes and Divers.
    2. 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.*;

    3. Create another class, "DropZone", which is "public" (click first ° before "class") and "extends Applet" and has the following fields and methods:
      1. Simple Fields. float temperature, float windSpeed, int windHeading
      2. Fancy Fields: plane ourPlane; Diver Ken; Diver Barbie
        1. Make sure that all fields have starting values. For example: plane ourPlane = new Plane();
      3. Methods: public void init()
        1. 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;
        2. Set up some divers and the plane and their fields (heading, velocity, etc.)
    4. Note: Eventually, the dropZone will boss the planes and divers around, and will put buttons on the screen for the human to press.
  9. Functions with parameters, For Loops
    1. 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)
    2. 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!");
      }
    3. 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. */
  10. Functions with different signatures
    1. Let's give the diver another shout() method that takes an extra parameter:
      void shout( int howMany, String theYell)
    2. This method won't shout "Wahoo!"&emdash;instead, it will shout theYell, using System.out.println(theYell);
    3. Put another shout command into dropZone's init() method:
      Ken.shout(4,"Cowabunga!"); /* Ken will shout "Cowabunga" 4 times */
  11. 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!")
    1. Add another field to class Diver to hold a name: String myName
    2. In DropZone's init() method, give names to some of your Divers:
      Ken.myName = "Super Ken";
    3. Customize the shout() methods so that they include the Diver's name:
      1. 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" */
      2. 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...