Thursday, September 14, 2006

Vedic Mathematics:

You really do not need to know multiplication beyond 5 table. We have many formulas in our vedas to solve all sort of mathematics problems starting from multiplication, division, algebra and the list goes on.. Here you go for simple example.

Suppose you need 8 x 7
8 is 2 below 10 and 7 is 3 below 10.
Think of it like this:


You subtract crosswise 8-3 or 7 - 2 to get 5,
the first figure of the answer.
And you multiply vertically: 2 x 3 to get 6,
the last figure of the answer.

The answer is 56. Thats all you do.

Wednesday, September 13, 2006

For each Construct in Java:

Java 1.5 introduces a new way of iterating over a collection of objects. The foreach loop is also known as the enhanced for loop. It is a new syntactical construct designed to simplify iteration. The foreach loop should make iteration more consistent, but only if and when everyone starts using it.
Effective use of the foreach loop depends on using Java 1.5's parameterized types, also known as generics. You can refer to generics section in this blog.
String[] moreNames = { "d", "e", "f" };
for (String name: moreNames)
System.out.println(name.charAt(0));

Generics in Collections

When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.

Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

Here is a simple example taken from the existing Collections tutorial:
// Removes 4-letter words from c. Elements must be strings
static void expurgate(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); )
if (((String) i.next()).length() == 4)
i.remove();
}
Here is the same example modified to use generics:
// Removes the 4-letter words from c
static void expurgate(Collection<> c) {
for (Iterator<> i = c.iterator(); i.hasNext(); )
if (i.next().length() == 4)
i.remove();


When you see the code , read it as “of Type”; the declaration above reads as “Collection of String c.” The code using generics is clearer and safer. We have eliminated an unsafe cast and a number of extra parentheses. More importantly, we have moved part of the specification of the method from a comment to its signature, so the compiler can verify at compile time that the type constraints are not violated at run time. Because the program compiles without warnings, we can state with certainty that it will not throw a ClassCastException at run time. The net effect of using generics, especially in large programs, is improved readability and robustness.