The following standards are to be followed in writing Java programs. They are important enough to be part of the grading criteria for all programs. Any constructs not covered in this standard are to be implemented in a consistent and readable fashion.
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.
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, a class named Robot
should be a file named Robot.java
. A lab assignment that determines the payroll of a small company could be in a file named Payroll.java
(or even something like Lab3.java
).
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, email
* @version date of completition
*
*/
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 Scuggins, escuggins@purdue.edu
* @version June 17, 2013
*
*/
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 }
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. 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 CS18000. 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 you make minor changes to these formatting conventions, be sure you do so consistently.
if (expression) { statements }
if (expression) { statements } else { statements }
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 (expression) { statements }
do { statements } while (expression);
for (initialization; condition; increment) { statements }