.Tutorial # 5.

Tracing Code



- Read all the code before you start

- Start at the main method and follow through the code in the method, line by line, until you reach the closing bracket. Once the program reaches the closing bracket, it is done processing and ends.

- While you are following through the code you may encounter a method call, where you will have to 'jump' to a different line of the code, where you will have to continue going from line to line until that method has completed, and you continue where you originally left the main method

- Constructors - The code within the constructor is ONLY run when a new object is created. Constructors are methods named the same as the class, and can be overloaded.

- Multiple Classes - Within a single program there can be many classes, and objects instantiated which each can have its own methods and variables. One object can call another objects method, and visa versa. Throughout all of these method calls, they all always return back to where they were called, unless the program crashes (or errors).

- Spacing - To show spacing use the underscore ( |_| (below the letters) unless it can safely be assumed. For blank lines use the ( [ ). Also make sure that there is a difference between your upper and lower case.



Float



Floats can be declared as follows:

float floater;

// no 'f' needed only for 0 number to represent float
floater = 0;

// notice the f after the
floater = 3.3f;   

// the number after the 'E' is the exponent, in this case its a -3
floater = 0.3E-03f;


Pseudocode



Pseudocode has no defined language, but your own language, of the logic of the program. It can help explain what you are trying to do, and can be used with any program, where the programming language is irrelevant.

// Summary: count to ten and print total

// Let total = 0
// Let number = 0
// while number is less than 11, do the following
    // total = total + number
    // number = number + 1
// end while
// print total

You can then use that pseudocode as your temporary comments as you figure out the particular syntax to work out your program, in this case Java.



toString() Method



    toString() is the default method called if you just refer to an object. If you recieve an address back, like object@12345, printed to the screen, most likely there has not been a toString() method defined for that class. The general purpose of the toString() method is to graphically represent the current state of the object. So every class should have a toString() method, excluding the main class.



Style - Comments


    - Comments help other programmers understand and use your code.

    - All comments should go BEFORE the block of code to which they refer.

    - Do not reveal implementation details in method and class comments.

    - Class comments should be short, and need not mention available methods.

    - Method comments must explain to a user what the method does.

    - The purpose of all parameters must be made clear.

    - State pre and post conditions necessary for the use of the method.

    - Exceptional cases should only be discussed in the precondition.

    - All instance variables need a comment.

    - Reveal implementation details in internal comments.

    - Comment loops.

    - Comment related statements.



Style - Naming


    - Class names must be nouns or noun phrases, and be capitalized.

    - Method names must be verbs or verb phrases.

    - Method and instance variables names must start with a lowercase letter.

    - Names made up of multiple words must have each interior word capitalized.

    - Constant (final) names must be in all capitals, and words must be separated by underscores.

   Examples:
      class PolarCoordinates // class
      String currentLimit // variables
      int largestSoFar
      void show() // methods
      int calculateCurrentLimit()
      final int MIN_VALUE // constants



Method Naming



    Get and Set Methods: Methods to get and set an attribute of a class, for example an attribute maxSize, should be prefaced with "set" and "get." Examples: getMaxSize, setMaxSize.

    Boolean Methods: A method that tests a boolean condition V about an object should be named isV. Example: isSorted.

Names should be descriptive, and you should avoid abbreviating.



Style - Whitespace



    - Indent four spaces (or a tab) within each block (A block is a section of code between braces (squiggly lines)).

    - Place a blank line before each comment, after every { and before every }. (except when closing braces appear right after each other, if the block contains only one statement, or the else keyword.)

    - If you have a chunk of code without comments, break it up into semantically-related parts with blank lines between them.

    - Put three blank lines between methods.

    - Opening braces are placed on the same line as the keyword with which they are associated, with a preceding space.

   - Closing braces ( } ) go on their own line, and are "outdented" to match the keyword that began the block, except that "} else {" and "} else if ( ... ) {" are allowed.



sitemap