Tuesday, September 21, 2010

Enums in java

In prior releases, the standard way to represent an enumerated type was the int Enum pattern: // int Enum Pattern - has severe problems! public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2; public static final int SEASON_FALL = 3; But these are not type safe, and clearly naming them is bit of a problem. So in Java 5, they introduced Enums. Eg. enum Season { WINTER, SPRING, SUMMER, FALL } public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,...

Saturday, September 18, 2010

Modifying Java Variables (w.r.t c and c++)

Modifying Simple Variable The only mechanism for changing the value of a simple Java variable is an assignment statement. Java assignment syntax is identical to C assignment syntax. As in C, an assignment replaces the value of a variable named on the left- hand side of the equals sign by the value of the expression on the right- hand side of the equals sign. Modifying Object Variable  Java object variables can be changed in two ways. Like simple variables, you can make assignments to object variables. When this is done the object referenced...

Friday, September 17, 2010

Constructor in java 1

ConstructorsWhen you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors are similar to methods, but with some important differences. Constructor name is class name. A constructors must have the same name as the class its in. Default constructor. If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor...

Common naming convention : Coding Style

Variable names must be in mixed case starting with lower case.  Common practice in the Java development community and also the naming convention for variables used by Sun for the Java core packages. Makes variables easy to distinguish from types, and effectively resolves potential naming collision as in the declaration eg. int state; Names representing constants (final variables) must be all uppercase using underscore to separate words. MAX_ITERATIONS, COLOR_RED Common practice in the Java development community and also the naming convention...

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...

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. For the other modifiers, the...

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. Plural...

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. It is not known why Sun allows both forms. Java source files...

Thursday, September 16, 2010

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. Without a preprocessor, there are no provisions...

Wednesday, September 1, 2010

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. 3 Private constructors 4. Parametric constructor It is good practice to try not to overload the constructors. It is...

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. The heap The heap (also known...

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...

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. This knowledge is commonly used when converting character digits...

Dynamic Allocation Issues

Memory leaksA program that allocates memory, but doesn't free it, is said to have a memory leak. For a small amount of data this usually isn't a big problem. However, a program that runs for a long time repeatedly allocating memory without freeing it will eventually crash, often crashing the entire system. Dangling pointersDangling or stale pointers are another source of problems. These are pointers to memory that has been deallocated. There's no problem in principle with leaving old pointers lying around, as long as they're never used. It's...

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 sortWhen 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...

Enum in c++

The problem: representing series of valuesIt is very common to have a series of values that need to be represented. For example, to simulate a traffic light requires representing three values (red, yellow, and green), but there is no built-in C++ color datatype. Use integer values to represent colors, for example red as 0, yellow as 1, and green as 2. There is nothing "green" about the value 2, and it could just as easily be represented by some other number. However, it is common to start a series at zero and continue up by ones. The...

Beginning with vi

Unsetting with vi Eg. to put off ai, use :set n...