CODING STYLE


SPRING 2024

All program code that is submitted must follow the coding style guidelines described in this web site. If you have any questions, ask your instructor or teaching assistant. 


NAMES OF CONSTANTS, VARIABLES, METHODS & CLASSES

Java constant names should contain all uppercase letters. Constant names consisting of multiple words should separate the words using underscore characters. 

Good: TAX_RATE, MAX_USERS_ALLOWED
Bad: taxRate, TAXRate, TaxRate, MaxUsersallowed 

Java variable and method names should begin with a lowercase letter. Variable and method names consisting of multiple words should contain capital letters for the first letter of all words except the first and should not contain any underscore characters. 

Good: temperatureGaugeValue
Bad: TEMPERATURE_GAUGE_VALUE, temperaturegaugevalue, Temperature_Gaugevalue

Java class names should begin with a capital letter; class names consisting of multiple words should contain capital letters for the first letter of each word (for example: DigitalThermometer) and should not contain any underscore characters. 

Good: DigitalThermometer
Bad: digitalThermometer, DIGITAL_THERMOMETER

Constant, variable, method and class names should be chosen so as to be self-documenting (e.g. the name suggests the data that is stored inside or the purpose of the class).  Any names that are not self-explanatory must be documented explicitly.

INDENTATION

Each subunit of code should be indented for readability purposes. Generally, a subunit of code is marked by a set of curly brackets in Java. Statements that do not require brackets but imply a nesting structure should be indented as well. Each level of nesting should be indented four spaces. For example, the following if statement illustrates acceptable nesting:

if (netIncome > 15000.0)
    rebate = 0.01 * netIncome;
else
    rebate = 150.0;

Lines of code should not extend beyond 80 characters in the event that your code needs to be printed out. If a statement takes up more than one line, the second and subsequent lines should be indented two spaces past the beginning of the first line of the statement. For example:

System.out.println("A taxpayer with a net income of $"
  + netIncome + " can expect a rebate check of $"
  + rebate + ".");

DOCUMENTATION

Every class should have a beginning comment that contains the specification for the class, as described in Chapter 1 of your textbook. Your specification will include general information about the class in addition to specific pre- and post-conditions for each method of the class and any exceptions that may be thrown. The specification in your program code should be in the javadoc format, as described near the end of your textbook. 

Documentation should be used to explain any non-obvious code. Every statement does not need to be documented, but sections of code that have a specific purpose should be documented as a unit. Any code that uses variables that are not self-documenting should be explained in a short comment.  Comments may be placed on lines by themselves or appended to lines of code, but should not interfere with the reading of the code itself. Generally, comments for code that is indented should be indented as well. 

Click here for sample program using javadoc format documentation.

USER INPUT

In this class, you will not be able to use SavitchIn or any predefined textbook input class. Instead, you should use the standard built-in Java classes to read in data from the keyboard. Here's a quick primer. To open an input stream for reading from the keyboard, you can use the following code:


InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(inStream);
To open an input stream for reading from a text file, you can use the following code (if file name is given on the command line, replace fileName with args[0]):

FileInputStream fis = new FileInputStream(fileName); 
InputStreamReader inStream = new InputStreamReader(fis);
BufferedReader stdin = new BufferedReader(inStream);

Once the stream is open, you can read one line at a time as follows:

String data = stdin.readLine();

You can extract each data element of the string (separated by white space) using the following:

String nextDataValue;
StringTokenizer tokenizer = new StringTokenizer(data);
while (tokenizer.hasMoreTokens())
{
	nextDataValue = tokenizer.nextToken();
	// then do something with nextDataValue
}

Since each data value is a string, you may need to convert it to use it. For example, if the data value is to be used as an integer, you can do the following:

int number;
number = Integer.parseInt(nextDataValue);

Refer to Appendix A (p728-739) of the text book for more information on reading from text files.



Course Info | Schedule | Sections | Announcements | Homework | Exams | Help/FAQ | Grades | HOME