March 19 2015

Legal Identifiers In Java

Legal Identifiers In Java

Names used for classes,methods,interfaces and variables are called Identifiers. An identifier is a sequence of one or more characters. It must be composed of only unicode characters,numbers,currency symbol($) and underscore symbol(_).

Following are the rules for legal identifiers

  • The first character must start with a letter or currency character($) or underscore(_). It cannot start with number

  • After first character,identifier can contain letters,numbers,currency symbol($) and underscore(_) in any combination

  • Idetifiers in Java are case sensitive,name and NAME are considered as two different identifiers

  • Java keywords cannot be used as identifier

Continue reading

March 6 2015

Java break outer loop

Java break outer loop

The placing of one loop inside the body of another loop is called nesting.The break statement is used to terminate the loop.

Example

for (int i = 1; i < 5; i++) {

for (int j = 1; j < 5; j++) {

if(condition){

break;

    }
  }
}

The break in the above code breaks the inner for loop not the outer for loop.

Following are the different ways to break the outer loop

Labeled break

Continue reading

March 5 2015

Constructor chaining in java

Constructor chaining in java

Constructors is a special method in Java which is used to initialize the instance variables of the class. A constructor has the same name of the class and does not have any return type(not even void).A constructor may or may not have parameters.

Constructors in Java can be overloaded.Two ore more constructors with difference in the parameters is called constructor overloading.

For complete article on Constructor Overloading click here

Constructor chaining

Calling one constructor from other constructor in the same class is Constructor chaining.It can be achieved using this().

this() should always be the first statement in constructor.

Continue reading

March 2 2015

Leap year program in java

Leap year program in java

A normal year has 365 days.
A leap year has 366 days (the extra day is the 29th of February)

example : 2012, 2016, 2020, 2024 …

Algorithm

1. Take the year from the user (year)
2. If the year is divisible by 400 it is a leap year
3. If the year is divisible by 4 and not divisible by 100 it is a leap year
4. If step 2 or step 3 conditions are not satisfied,It is a not leap year Continue reading

March 2 2015

Pascal triangle program in java

Pascal triangle program in java

Pascal’s triangle is a triangular array of the binomial coefficients.It is named after Blaise Pascal, a famous French Mathematician and Philosopher)

As show in figure,the triangle will start with 1 at the top and placing the numbers below it in the traiangular format.Except the edges(1’s), each number is the sum of two numbers above it.

Algorithm

pascal triangle program in java

1.Take ‘number of rows’ from the user(n)
2.Iterate from 0 to n(i)
3.Add spaces for every iteration from n-1 spaces to 0 spaces(j)
4.Iterate from 0 to i(k)
5. Print the values using (number * (i – k) / (k + 1)) Continue reading

February 24 2015

Automorphic number java program

Automorphic number java program

In mathematics an automorphic number is a number whose square ends in the same digits as the number itself.

For example,

52 = 2562 = 36 ,252=625 , 762 = 5776

The sequence of automorphic numbers begins 1, 5, 6, 25, 76, 376, 625, 9376…

Algorithm

  • Take the input from the user (number)
  • Calculate number of digits in the number(digits)
  • Calculate square of the number(numberSquare)
  • Calculate the reminder of number divided by the 10 to power of digits(lastDigits)
  • If number is equal to last digits of the number,It is automorphic
  • Else it is not automorphic

Continue reading

February 12 2015

Multilevel inheritance in java

Multilevel inheritance in javamultilevel inheritance in java

Deriving new classes from existing classes so that new classes acquires the features of existing class is called inheritance.Multilevel inheritance in java is a mechanism where one class inherits features from a derived class.Multilevel inheritance can go up to any number of level.

As you can see in the below example, Class B is a subclass of Class A and Class C is sub class of Class B.Class B inherits the features of Class A and Class C inherits the features of both Class B and Class A.

Continue reading