PURPOSE
The purpose of these rules are (1) to introduce you to a representative
set of coding standards that are typical in professional programming
and (2) to help you develop good style as a habit, something you will
find necessary to successfully complete large programs. There is no
doubt that you can write Java programs to solve the small problems
in this course without attention to style. But, you must keep in mind
that you are building professional skills, not merely finding the
answers to problems handed out as exercises.
FILE NAMES AND JAVA COMPILER
In this course each Java file name should clearly represent what it is
and each should have the ``.java'' suffix. Files names must match the
class name. For example, Project 1 should be Project1.java. A lab
project that determines the payroll of a small company could be
Payroll.java (or even something like Lab3.java).
INTRODUCTORY DOCUMENTATION
At the beginning of each Java class there should be a JavaDoc block
that contains:
The format to be used is as follows:
/**
* Program Name
* brief description of the program
*
* @author your name
*
* @recitation section number and recitation instructor's name
*
* @date date of completion
*
*/
The following is an example of an introductory comment block that
follows the format above:
/**
* Project 1 -- Video Calculator
* This program inputs the total number of minutes,
* the number of minutes for commercials, and the
* number of minutes for episodes. The program
* computes the total number of minutes for videos,
* the number of videos, and the number of seconds
* left over.
*
* @author Elwood Blues
*
* @recitation 03 (Andy Scharlott)
*
* @date September 14, 2005
*
*/
DECLARATIONS
final float BASE = 2.0; // divisor to obtain base 2 final char HYPHEN = '-'; // signals word continued on next line final int NAME_LENGTH = 20; // names can be 20 characters
float volts; // voltage in the circuit int amps; // amperage in the circuit char circuitName[NAME_LENGTH]; // name of the circuit
left = nextLeft; right = nextRight;use
left = nextLeft; // move to the left right = nextRight; // move to the right
/** * calculates and returns the charge for shipping cargo * between two planets. * * @param distance distance in miles between two planets * @param weight weight in pounds of item being shipped * @return A double representing the cost to ship the item in space */ public double findSpaceCost(long distance, float weight) { // implement here }
FORMATTING AND COMMENTING OF STATEMENTS
Alignment and indentation of lines of Java statements add much to the
readability of programs. The following guidelines are general so that
you may develop your own programming style. Use indentation and blank
lines as you see fit to increase the readability of your programs.
Use whitespace (blank lines and spaces on lines) liberally!
Use parentheses liberally!!
The body of a control structure, such as an if, a switch, a while, a do, or a for statement, should be indented. The following examples illustrate the only formatting style that is to be used in CS180. Each level of indenting is 4 spaces. (All of the below examples should have 4 spaces for indentation, but not all browsers display it correctly.)
if statement
if (expression)
{
statements
}
if-else statement
if (expression)
{
statements
}
else
{
statements
}
switch statement
switch (selector variable)
{
case case-1-value:
case 1 statements;
break;
case case-2-value:
case 2 statements;
break;
...
case case-n-value:
case n statements;
break;
default:
default statements;
break;
}
while statement
while (expression)
{
statements
}
do-while statement
do
{
statements
}
while (expression);
for statement
for (initialization; condition; increment)
{
statements
}
PROGRAM DESIGN