CodeExample(1).
java
/**
* This program is an example of code formatting and is not designed to
* produce any meaningful results.
* @author: John Deal
*/
import [Link];
public class CodeExample
/**
* Returns joke based on jokeNumber.
**/
public String getMusicJoke(int jokeNumber)
final String NO_JOKE = "No Joke";
final String SINGER_JOKE = "How do you tell if there is a singer "
+ "at the door? They never have the right key and don't know " + "when to come in!";
final String GUITARIST_JOKE = "How do you get a guitarist to be " + "quiet? Put sheet music in front
of them!";
String joke;
// Determine joke
switch (jokeNumber)
{
case SINGER_JOKE_NUMBER:
joke = SINGER_JOKE;
break;
case GUITARIST_JOKE_NUMBER:
joke = GUITARIST_JOKE;
break;
default:
joke = NO_JOKE;
break; // Not necessary but good practice.
} // End switch (jokeNumber).
return joke;
} // end getMusicJoke
/**
* Program entry point.
**/
public static void main(String[] args)
final int MIN_COUNTER = 0;
final int MAX_COUNTER = 10;
String className = "CodeExample";
int counter;
float value;
className = "CodeExample";
CodeExample codeExample = new CodeExample();
// Print class name with incrementing counter appended.
for (counter = MIN_COUNTER; counter < MAX_COUNTER; counter++)
[Link]("Name: " + className + "_" + counter);
[Link]("-----------------"); // Output divide.
// Print class name with decrementing counter appended.
while (counter > MIN_COUNTER)
[Link]("Name: " + className + "_" + counter);
--counter;
// Display result
if (counter < MIN_COUNTER)
[Link]("Dropped below minimum counter.");
} else
[Link]("Did not drop below minimum counter.");
// Display counter state
switch (counter)
{
case MIN_COUNTER:
[Link]("At MIN_COUNTER.");
break;
case MAX_COUNTER:
[Link]("At MAX_COUNTER.");
break;
default:
[Link]("Counter is: " + counter);
break;
} // End switch (counter).
[Link]("Now for a couple of jokes...");
[Link]([Link](SINGER_JOKE_NUMBER));
[Link]([Link](GUITARIST_JOKE_NUMBER));
} // end main()
// Joke numbers.
private final static int SINGER_JOKE_NUMBER = 1;
private final static int GUITARIST_JOKE_NUMBER = 2;
} // end CodeExample
[Link]
/* Commenting: Each class must have a cursory header comment
describing what behaviors the class provides
You can use either a C-Style comment like this comment,
or Java style // comment at the top of the file
for the class header comment,
but all other comments should be Java style // comments
*/
// class FormattedStyle provides examples of Java Style
//
// Source file names must start with a capital letter,
// be letters and numbers only,
// and match the name of the public class with the main() method to be executed.
// Class names will follow the camel naming convention
// Class names will start with a capital letter
public class FormattedStyle {
// Program method main() should be at the top of a class after any constructor(s).
public static void main(String[] args) {
} // end main()
// The right brace of each class and method should be followed by a
// end comment such as // end main()
// each method containing logic (i.e. not getters or setters)
// must have a cursory header comment describing what the function provides
// foo() provides some code examples
public void foo(int value) {
// Indentation level must be consistently either 3 or 4 spaces
value++;
// Variable names will follow the camel naming convention
int camelCase;
// method-local variable names will start with a lower-case letter
float shouldBeLowercase;
// each major control structure must have a cursory comment
// describing the how control structure logic operates.
if (value > 10) {
// Indentation level must be consistently either 3 or 4 spaces
// each and there must not be any TABs in the source code
value = value - 10;
// Lines should not wrap around to the 1st column.
// For example
[Link]("Enter weight (in pounds) followed by height " +
"(in inches) separated by a blank space:");
} // end foo()
// Line lengths should be kept within 80 characters
// (width of a normal terminal window)
// unless doing so is not possible or reduces code clarity.
// It is sometimes necessary to set automated formatters to 78 character
// line width in order to achieve an 80 character line width.
// Use Meaningful Variable Names: Variables and named constants should have
// meaningful names
// which are descriptive of their contents for example: accountBalance
public void guess(int maximumValueToGuess) {
// Only declare one variable per statement
int i;
int j;
// Braces alignment: Braces for control structures be vertically aligned
i = 1;
j = 2;
while (i <= maximumValueToGuess - 1)
i++;
// Braces alignment: Braces for control structures can be K&R aligned
while (i <= MAX_GUESS - 1) {
i++;
// Use Braces: Major logic statements (if, switch, for, while, do)
// MUST have an associated code block (i.e. set of braces)
// even if there is only one or no other statements associated with the logic
// statement.
// This is also required for case statements of more than 1 associated
// statement.
if (i < j) {
i = j;
// One space before and after operators
while (i == j) {
i++;
// No space after left parens, braces, and brackets and no space before right
// parens, braces, and brackets
while (i == j) {
i++;
// Use parens to improve understandability of logic
while (i <= (MAX_GUESS - 1)) {
i++;
// Methods should only have one return statement
// (or no return in case of void method) when practical
public boolean isEven(int x) {
return (x % 2) == 0;
public final int PRINT_QUEUE = 3;
public void printQueue(int queueCommand) {
switch (queueCommand) {
// Do not use Magic Numbers
// Use a final or enum to make the code more understandable
case PRINT_QUEUE:
[Link]("Print Queue: ");
}
// Method names start with a lower-case letter and use the camel naming
// convention
public double getPaymentDollars() {
return [Link];
public double incNumStyleIssues(double num) {
[Link] += num;
return [Link];
} // end incNumStyleIssues()
private double paymentDollars;
// Final constant names will be all capital letters
// with “_” separating the logical words (ex. NAME_INDEX)
private final int MAX_GUESS = 100;
// class instance variable names will start with a lower-case letter
private double excutionTimeMilliSec;
private static int numStyleIssues = 0;
// class static variable names will start with a lower-case letter.
private static int studentCount = 19;
// Use private to make fields accessible to the class only,
// all non-final instance variables should be private to support information hiding.
private int privateAccessInt = 123;
// The right brace of each class should be followed by a
// end comment such as // end main()
} // end class FormattedStyle
// All references to instance data should use this.
// Helps to distinguish instance data from local data:
class Year {
// return the Year
public int getYear() {
return [Link];
// set the Year
public void setYear(int year) {
[Link] = year;
// Year defined as an int as all years are numeric integers
private int year;
} // end Year
[Link]
import [Link];
/* HelloJava
* * Provides an example of Java Style
* Class header comment is above the class declaration using C-style notation
*/
public class JavaStyleExamples
// main() should be at the top of a class after any constructor(s).
// Method header comments use // notation
public static void main(String[] args)
JavaStyleExamples helloJava = new JavaStyleExamples();
[Link]();
} // end main
// run()
//
// A non-static member function which can access instance data
public void run()
// Let the user know if today is a Tuesday
if (isATuesday())
[Link]("Today is a Tuesday");
// Final constant names will be all capital letters
// One space before and after operators
// Using DOZEN instead of 12 avoids a magic number
final int DOZEN = 12;
} // end run
// isATuesday()
//
// Returns true if today is a Tuesday
// methods are camelCase
private boolean isATuesday()
// Methods should only have one return statement
// (or no return in case of void method) when practical
return [Link]("Tue");
private long millis = [Link]();
private Date date = new Date(millis);
private String dateStr = [Link]();
} // end HelloJava
/* Car
* * Provides example of static and non-static methds
*/
class Car
// convertMpgToKpl(double mpg)
//
// Converts Miles per Gallon to Kilometers per Liter
// Is static because one might want to know what 35mpg converts to
// even if nobody has ever built a Car
public static double convertMpgToKpl(double mpg)
{
return 0.425144 * mpg;
// setMileage(double mpg)
//
// Sets the efficiency of one particular Car
// Can't be static since it's inconceivable to call the method
// before any Car has been constructed
public void setMileage(double mpg)
[Link] = mpg;
private double mpg; // Efficiency of the Car
[Link]
// Class names will follow the camel naming convention
// Class names will start with a capital letter
class un_formatted_Style
public void foo(int value) {
value++;
// Variable names will follow the camel naming convention
int not_camel_case;
// method-local variable names will start with a lower-case letter
float ShouldBeLowercase;
if (value > 10) {
// Indentation level must be consistently either 3 or 4 spaces
// each and there must not be any TABs in the source code
value = value - 10;
// Lines should not wrap around to the 1st column.
// For example
[Link]("Enter weight (in pounds) followed by height " +
"(in inches) separated by a blank space:");
// Line lengths should be kept within 80 characters (width of a normal terminal window)
// unless doing so is not possible or reduces code clarity. It is sometimes necessary
// to set automated formatters to 78 character line width in order to
// achieve an 80 character line width.
// Program method main() should be at the top of a class after any constructor(s).
public static void main(String [] args) {
// Use Meaningful Variable Names: Variables and named constants should have meaningful names
// which are descriptive of their contents for example: accountBalance
public void guess(int mx) {
// Only declare one variable per statement
int i, j;
// Braces alignment: Braces for control structures can either be
// vertically aligned or K&R aligned
i = 1;
j = 2;
while( i<=mx-1 ) { i++; }
// Use Braces: Major logic statements (if, switch, for, while, do)
// MUST have an associated code block (i.e. set of braces)
// even if there is only one or no other statements associated with the logic statement.
// This is also required for case statements of more than 1 associated statement.
if (i < j)
i = j;
// One space before and after operators
while (i==j) {
i++;
// No space after left parens, braces, and brackets and no space before right parens, braces, and
brackets
while ( i == j ) {
i++;
}
// Use parens to improve understandability of logic
while(i <= MAXGUESS - 1) {
i++;
// Methods should only have one return statement
// (or no return in case of void method) when practical
public boolean isEven(int x) {
if (x % 2 == 0) {
return true;
else {
return false;
public void printQueue(int queueCommand) {
switch (queueCommand) {
// Do not use Magic Numbers
// e.g. Instead of
case 2:
[Link]("Print Queue: ");
// Method names start with a lower-case letter and use the camel naming convention
public double Get_paymentDollars() {
// To clearly distinguish between local and instance data,
// refer to all instance data with the prefix this
return paymentDollars;
// When should a method be static?
// Ask yourself "does it make sense to call this method, even if no Object has been constructed yet?"
// If so, it should definitely be static, otherwise it should not be static.
static public double incnumStyleIssues(double num) {
numStyleIssues += num;
return numStyleIssues;
private double paymentDollars;
// Final constant names will be all capital letters
// with “_” separating the logical words (ex. NAME_INDEX).
private final int MAXGUESS = 100;
// class instance variable names will start with a lower-case letter
private double ExcutionTimeMilliSec;
private static int numStyleIssues = 0;
// class static variable names will start with a lower-case letter.
private static int StudentCount = 19;
// Do not use default visibility
int packageAccessInt = 123;
// All references to instance data should use this.
// Helps to distinguish instance data from local data:
class Year {
// return the Year
public int getYear() {
return theYear;
// set the Year
public void setYear(int year) {
theYear = year;
// Year defined as an int as all years are numeric integers
private int theYear;
} // end Year