Improving coding style into classes

**Class and Interface declarations should be organized in the following manner: ** 1. Class/Interface documentation. 2. class or interface statement. 3. Class (static) variables in the order public, protected, package (no access modifier), private. 4. Instance variables in the order public, protected, package (no access modifier), private. 5. Constructors. 6. Methods (no specific order). Reduce complexity by making the location of each class element predictable. Imported classes should always be listed explicitly. [Read More]

Improving coding style into functions or methods

Method modifiers should be given in the following order: static abstract synchronized final native The modifier (if present) must be the first modifier. public static double square(double a); // NOT: static public double square(double a); is one of public, protected or private while includes volatile and transient. The most important lesson here is to keep the access modifier as the first modifier. Of the possible modifiers, this is by far the most important, and it must stand out in the method declaration. [Read More]

Java specific naming convention

JFC (Java Swing) variables should be suffixed by the element type. widthScale, nameTextField, leftScrollbar, mainPanel, fileToggle, minLabel, printerDialog Enhances readability since the name gives the user an immediate clue of the type of the variable and thereby the available resources of the object. Array specifiers must be attached to the type not the variable. int[] a = new int[20]; // NOT: int a[] = new int[20] The arrayness is a feature of the base type, not the variable. [Read More]

Specific cases of naming enhancing naming style

The term find can be used in methods where something is looked up. vertex.findNearestVertex(); matrix.findSmallestElement(); node.findShortestPath(Node destinationNode); Give the reader the immediate clue that this is a simple look up method with a minimum of computations involved. Consistent use of the term enhances readability. The term initialize can be used where an object or a concept is established. printer.initializeFontSet(); The American initializeshould be preferred over the English initialise. Abbreviation init must be avoided. [Read More]

Java and CPP - the differences and similarities

This list of similarities and differences is based heavily on The Java Language Environment, A White Paper by James Gosling and Henry McGilton http://java.sun.com/doc/language_environment/ and the soon-to-be published book, Thinking in Java by Bruce Eckel, http://www.EckelObjects.com/. At least these were the correct URLs at one point in time. Be aware, however, that the web is a dynamic environment and the URLs may change in the future. Java does not support typedefs, defines, or a preprocessor. [Read More]

Types of constructors

1. Void constructors or default constructors This has no parameters and is must in case of dynamic allocation of objects. 2. Default parameter constructor A default parameter is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used. [Read More]

The stack and the heap

The memory a program uses is typically divided into four different areas: The code area, where the compiled program sits in memory. The globals area, where global variables are stored. The heap, where dynamically allocated variables are allocated from. The stack, where parameters and local variables are allocated from. There isn’t really much to say about the first two areas. The heap and the stack are where most of the interesting stuff takes place, and those are the two that will be the focus of this section. [Read More]

Word Length Frequency

// word\_len\_histo.cpp : reads words and lists distribution // of word lengths. // Fred Swartz, 2002-09-01 // This would be nice to turn into an OO program, where // a class represented a distribution of values. // Some elements which are globals here would turn into // private member elements in the class (eg, valueCount). //--- includes #include <iostream> #include <iomanip> #include <cctype> using namespace std; //--- prototypes void countValue(int cnt); float getAverage(); //--- constants const int BINS = 21; // how many numbers can be counted //--- globals int valueCount\[BINS\]; // bins used for counting each number int totalChars = 0; // total number of characters //=========================================================== main int main() { char c; // input character int wordLen = 0; // 0 if not in word, else word length //--- Initialize counts to zero for (int i=0; i valueCount\[i\] = 0; } //--- Read chars in loop and decide if in a word or not. [Read More]

Taking input as string 1 - " C-String to Int "

Converting C-Strings to Integer If you want to convert a C-string (zero-terminated array of chars) of digits, you can call one of the library functions to do this (good idea), or write something like the following (good exercise). Character codes for digits Every character is represented by a pattern of bits. These patterns can be thought of as integers. If your system uses ASCII (or any of the newer standards), the integer value of the code for ‘0’ is 48, ‘1’ is 49, etc. [Read More]

Using stl sort

Never write your own sort! Use the the sort in the Standard Template Library (STL). The STL has sorts that are efficient and well tested. Basic syntax for calling sort When calling the STL sort, you need to pass two parameters: the address of the first element to sort, and the address of one past the last element to sort. The address is used for iterating across array elements. For other data structures (eg, a vector) you will have to do something a little different, but for arrays we can simply express the beginning and ending points with the array name and the addition of an integer. [Read More]