CIS 3020 Part 3

From In The Wings
Revision as of 11:13, 21 February 2007 by Jka (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Syntactic Elements

class SpecialTurtle extends Turtle {
  public void drawTriangle( double sideLength ) {
    final double DEGREES_IN_CIRCLE = 360.0;
    double turnAngle = DEGREES_IN_CIRCLE / 3;
    this.tailDown();
    this.move(sideLength);
    this.turnLeft(turnAngle);
    this.move(sideLength);
    this.turnLeft(turnAngle);
    this.move(sideLength);
    return;
  } // end drawTriangle
} // end class SpecialTurtle
  • Keywords (a.k.a. Reserved Words):
    • Examples: class, public, static, void, new, extends, double, this, return, const
    • Have a predefined meaning
    • Can only be used in specific locations within your program
  • class - used to define a class
  • public - allows everone to access
  • static - belonds to the class as a whole, not to an instance of the class
  • void - a "return value" of nothing
  • new - instantiate an instance (object) of a class
  • extends - define a specialized version of a class
  • double - defines a type - real number
  • this - refers to the current object
  • return - stop processing and return the specifed value, if one is given:
    • return;
    • return( expr );
  • final - used to define a constant:
    • final double DEGREES_IN_CIRCL 360.0;

Identifiers

  • Programmer selected symbolic names for data, operations, and classes
  • Contain letters, numbers, underscore
  • Start with letter or underscore
  • Class names: Demo, Island, Turtle
    • Start with capital letter (stylistic convention)
    • Each subsequent word also is capitalized
  • Method names: main, drawTriangle
    • start with lower case letter
    • Each subsequent word is capitalized
  • Data names
    • Constants - DEGREES_IN_CIRCLE
      • values that cannot change
      • All upper-case characters with underscores between words
    • Variables - sideLength, turnAngle, tina
      • Start with lower case
      • Each subsequent word is capitalized

Operators

  • Predefined by the language
    • [] - array index operator
    • = - gets (the value of), assignment operator
    • . - dot operator - send the message on the right to the object on the left. Not the same as decimal point in number.
    • + - addition operator
    • - - subtraction operator
    • * - multiplication operator
    • / - division operator
    •  % - modulus operator - remainder from division