Monday, 30 May 2016

1. Write code in Java to Convert numbers to words

public class NumbersAndWords
{
  public void convertToWord(int number,String word)
  {
 StringBuffer convertedWord = new StringBuffer();
String one[] = { " ", " one", " two", " three", " four", " five",
" six", " seven", " eight", " Nine", " ten", " eleven",
" twelve", " thirteen", " fourteen", "fifteen", " sixteen",
" seventeen", " eighteen", " nineteen" };

String ten[] = { " ", " ", " twenty", " thirty", " forty", " fifty",
" sixty", "seventy", " eighty", " ninety" };

if (number > 19)
{
convertedWord.append(ten[number / 10] + " " + one[number % 10]);
} else
{
convertedWord.append(one[number]);
}
    if(number > 0)
    convertedWord.append(word);
    System.out.println(convertedWord.toString());
  }

  public static void main(String[] args)
  {
    int number=1223456;
 
   if(number>0)
   {
      NumbersAndWords a = new NumbersAndWords();
      a.convertToWord(((number/100000)%100)," lakh");
      a.convertToWord(((number/1000)%100)," thousand");
      a.convertToWord(((number/100)%10)," hundred");
      a.convertToWord((number%100)," ");
    }
  }
}

Friday, 26 February 2016

Exception Handling in Java

Exceptional handling is one of the best useful features provided by Java.
Exception can be any error that may occur during the execution of the program.
Example: When user tries to withdraw amount more than his account balance, an error occurs.
In Such situations instead of ending the program abnormally, Exception handling is used to throw the valid error messages to the end user.
The reasons for an exception can be mainly categorized into three categories:
1.       User(client) error
2.       Developer error
3.       Hardware error.
Exceptions are mainly divided into three categories
1.       Checked Exceptions
2.       Un checked Exceptions
3.       Error
Below is the hierarchy of exceptions. Throwable is the root class for all exceptions.

Checked Exceptions:  
v  Checked Exceptions are the exceptions which are checked by compiler. I.e. our program will not compile if do not handle these checked Exceptions.
v  All checked Exceptions are inherited from Exception class
v  Checked exceptions can be handled either by using try-catch block. Or by using throws keyword
v  Checked Exceptions are also called as Compile time Exceptions
Example:
In the below example user is trying to read a file called ‘chckedException.txt’ in D drive. There is a chance giving wrong location for the file.

File file=new File ("D: // chckedException.txt”);
 FileReader fr = new FileReader (file);

To handle such situations, java throws a compile time exception called FileNotFoundException which should be handled by the developer while developing the code.
Developer can use either try catch block or can choose to skip the exception handling by just throwing it using throws keyword.

Solution 1:
public void readFile()
{
Try
{
File file=new File ("D: // chckedException.txt”);
 FileReader fr = new FileReader (file);
}
Catch(FileNotFoundException ex)
{
System.out.println(“File does not exist at the given location”);
}
}
Solution 2
public void readFile() throws Exception
{
File file=new File ("D: // chckedException.txt”);
 FileReader fr = new FileReader (file);

}



Thursday, 14 January 2016

Core Java Interview questions

Hello guys, in this post I will try to cover as many interview questions as possible.These are most commonly asked interview questions that can be useful for freshers as well as experienced people.

In this post I will try to concentrate maximum on core Java questions only. 

1. What is the difference between String, StringBuffer and StringBuilder?

String Object is immutable. I.e. if we create a String object, we cannot change it at the same memory location. 
If we change the string object, it creates a new memory location and copies the content into that new location.

Example 

String s1 = new String ("Java interview")

Suppose that Address of s1 is 200 

if we say s1 = s1 +" questions”

Then it will create a new memory location (say at 300) and copies the content to new location.


I.e. address of s1 will change to 300. Please click on the image below to maximize it.

StringBuffer :

StringBuffer is mutable. I.e. if we create a string buffer object, then we can modify it at the same location.

StringBuffer sb = new String Buffer (“Java interview”);

Suppose that address of sb is 200

If we say
Sb.sppend(“questions”)

Then its value will be changed to Java Interview Questions and address will be same 200.



  String Builder :

  1.  String Builder is Non-synchronized version of String buffer. i.e String Buffer is synchronized where as String builder is non-synchronized. 
  2. StringBuilder is much faster than StringBuffer as it doesnt have to deal with synchronization methods and locks
  3. so if you are not going to use threading in java then using StringBuilder will improve the efficiency of the program.
  4. StringBuffer is old class in java where as StringBuilder was introduced from java 1.5 version.

1.     Explain about finalize method in JAVA.

v  Finalize method is a protected method declared in Object class
v  It is by default available to all classes defined in Java.
v  This method is called by the garbage collected before destroying an object.
v  We can override this method in our class. When we override, it is our responsibility to call the finalize method of the super class.
v  Below is the syntax of finalize method.


Protected void finalize() throws Throwable;
 

Wednesday, 13 May 2015

Inheritance in Javascript

in Javascript inheritance can be achieved by using below two methods
1.Prototype chaining
2.Constructor stealing

1.prototype chaining

base=function()
{
this.name="base";
}

derived=function
{
this.marks="20";

this.prototype=new base();//creating inheritance using prototype
}

with above approach we face some problems while usig collectios like arrays.

Example


base=function()

{
this.name="base";
this.languages=["tel","eng","hindi"];
}

derived=function
{
this.marks="20";

this.prototype=new base();//creating inheritance using prototype
}

var  d1=new derived();
var d2=new derived();
d1.push("Kannada");

alert(d1.laguages) //prints tel,eng,hindi,kannada

alert(d2.languages) //also prints tel,eng,hindi,kannada

this is the problem with prototype chaiing when we use arrays, and try tomodify them,it will be reflected in all instances of the class.as when we modify an array, it points to a different location/reference


Constructor stealing


This is the second way of implementing inheritance in javascript where we invoke constructor of the parent class directly in the child class to perform inheritance.


base=function()
{
this.name="base";
}

derived=function
{
this.marks="20";

this.prototype=new base();//creating inheritance using prototype
}

with above approach we face some problems while usig collectios like arrays.

Example


base=function()

{
this.name="base";
this.languages=["tel","eng","hindi"];
}

derived=function
{
this.marks="20";

base.call(this);
}

var  d1=new derived();
var d2=new derived();
d1.push("Kannada");

alert(d1.laguages) //prints tel,eng,hindi,kannada

alert(d2.languages) // prints tel,eng,hindi 

so to avoid problems usig prototype chaiing, we can prefer constructor stealing.



Struts2 and Spring Integration

As we all now, struts2 can be integrated with other frameworks also. In this article,I will try to expalin how to integrate struts2 with spring framework.

To perform the integration, follow the below steps.

1.download the struts2-spring-plugin-x.y.z.jar file and add to class path file
2.In the web.config file configure the ContextLoaderListener listner as shown below.




<listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
 </listener>



The main purpose of this file is to load the applicatioContext.xml file. This file should be at the same level of web.xml file.

Friday, 24 April 2015

Spring Tutorials


Spring is a light weight framework used to develop complex java applications.
Advantages of spring are
1.It is very light weight when compared to EJBs
2.It can be easily integrated with other frameworks
3.No need of any web server while developing applications. As it provides some inbuilt containers.
4.Provides built in support for connection pooling and transaction management.

Spring provides below two containers

1.BeanFatory container.(Bean factory container is also called as IOC container or Core container)
2.ApplicationContextContainer

BeanFactory container :

1.BeanFactory container is also called as IOC Container or core container
2.It is implementation of BeanFactory class
2.It supports lazy loading. i.e. objects are injected only when they are invoked.
4.By default it considers all beans as singleton
5.

Wednesday, 15 October 2014

What is JAX-P-

Hello and welcome to JAX-P article,

in this article,we will discuss details about JAX-P. JAX-P stands for XML processing.

As we disscussed in 'What is XML' article, XML is also a document which contains data.

earlier there were no options in JAVA to read XML files, users has to use java I/O to XML document.

later JAVA has provided to this API to process XML documents.

XML can be read either from starting or in random order ,JAVA provides two types of parsers to process XML files.

SAX-PARSER :

SAX parser reads the document in sequential order.
it is event based parser.
there are different types of events called document start,element start,element end..

it can read only one element at any time.
it uses less memory
it is fast compared with DOM parser.

SAXParserFactory saxparserFactory=
SAXParserFactory.newInstance();

 SAXPARSER parser=saxparserFactory.newSAXParser();

using parser, we can parse the XML documents. it provides some set of methods to perform the read operations

SAX parser can only read the documents.

Search This Blog