6.2. File Format and Indentation

Template [TEMPLATE: 1 point]

Use the standard template Java source files. Replace the asterisks by appropriate text. Provide your name, the assignment number, the name of the file and the number of columns between tab stops that you have used.

Do not have a package line [PACKAGE-LINE: 1 point]

Do not start your file with
  package packname;
If you do, then I have to remove that line in order to run your program. (With that line in, I would have to be sure that your program is in a directory called packname, or even in a specific subdirectory of another specific directory if the package name has a dot in it. That makes my job more time consuming.)

Show tab stops, if used [TABS: 1 points]

If you use tabs, you must say, in the heading at the top of the file, where the tab stops are. For example, if your tab stops set every 4 spaces, say so.

Pay attention to this! If you specify the wrong spacing of tab stops, your program might be very poorly indented, and you will lose points for indentation.


No long lines [LONG-LINE: 1-2 points]

Do not write lines longer than about 80 characters. Break long lines.

Use blank lines [USE-BLANK-LINES: 1-2 points]

  • Put a blank line between the end of one method and the contract for the next method.
  • Put a blank line between a method's contract and the method definition.
  • If a method body contains paragraph comments, put a blank line before and after such a comment.

No excessive blank lines [EXTRA-EMPTY-SPACE: 1 point]

  • Do not leave excessive blank lines in method bodies or between methods.
  • Do not double-space code.
  • Do not double-space header files that do not contain contracts.
  • Do put blank lines between major paragraphs of code.

Indentation and braces [INDENT: 1-10 points*]

Every component of a structured statement (if-statemet, while-statement, do-statement, for-statement, switch-statement) must be surrounded by braces and indented either 2 or 3 spaces, as shown in the following example. A right-brace must be directly below the matching left-brace.
  if(condition)
  {
    statementstatement
  }
  else
  {
    statementstatement
  }

Exception. If the component is and empty set of braces {}, then it is not required to be on separate lines. For example, you are allowed to write

  for(…) {}
where the empty set of braces is the for-loop body.

*A program that has exceptionally poor indentation throughout can lose up to 40 points for lack of indentation.


Chained ifs [IF-MULTIPLE-CASES: 1 pt]

When several if-statements are used to simulate multiple cases, indent all of the cases the same amount, as in the following example.
  if(condition)
  {
    statements
  }
  else if(condition)
  {
    statements
  }
  else if(condition)
  {
    statements
  }
  else
  {
    statements
  }

No incorrect indentation for sequencing [INDENT-SEQUENCE: 1-10 points*]

Two statements that are performed one after another must be indented the same amount. For example,
  int x;
    int y;
is not correctly indented. Similarly,
  int badindentdemo(int n)
  {
    for(int i = 0; i < n; i++)
    {
      if(test(i))
      {
         return i;
      }
    }
      return -1;
  }
is not correctly indented. Since statement return −1 is performed just after the for-loop (assuming that the return statement inside the for-loop is not done), it should be indented the same amount as the word for.

*A program that has exceptionally poor indentation throughout can lose up to 40 points for lack of indentation.