Wednesday, 8 October 2014

Write a program to reverse a string

import java.util.*;
 class ReverseAString           //class name
{
   public static void main(String args[])  //main function
   {
      String original, reverse = "";    //string declaration
      Scanner in = new Scanner(System.in);                //to get input from- //-user

      System.out.println("Enter a string to reverse"); //Ask //string to reverse from user
original = in.nextLine();                            //getting input from user //using ‘in’ object

      int length = original.length();            //finding length of the //inputted string

      for ( int i = length - 1 ; i >= 0 ; i-- )  //using for loop //getting each character by using charAt(i) method
         reverse = reverse + original.charAt(i);  //reversed string is //stored in reverse string

      System.out.println("Reverse of entered string is: "+reverse);
//reversed sting is outputted to user
   }
}

Java Interview questions

·        Collections in java
·        Interfaces in java
·        Why static is used in public static void main()
Question
·        Difference between jre and jdk
Answer
·        JRE: Java Runtime Environment.
JRE basically the Java Virtual Machine where Java programs run on and  also includes browser plugins for Applet execution.
·        JDK: Java Development Kit

It is a Software Development Kit for Java, including JRE, and compilers and tools (eg. JavaDoc,, Java Debugger) to create and compile programs.

Sunday, 28 September 2014

Final Variable With Example


There is a final variable a, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

Example Program

class Super
{
                       final int a=100;
                       void show()
                       {
                        //a=200;error , can't be changed because final variable once assigned a value can never be changed.

                        System.out.println("Welcome to the final Keyword                       "+a);
                       }
}

class Demofinal
{
                       public static void main(String args[])
                       {
                        Super obj=new Super();
                        obj.show();
                       }
}


Output

D:\>javac Demofinal

D:\>java Demofinal

Welcome to the final Keyword    100

Wednesday, 24 September 2014

Constructor Overloading with Example

Constructor Overloading

Constructor overloading in java allows to have more than one constructor inside one Class. 

Example Program

class Demo
{
                       Demo()
                       {
                        System.out.println("Constructor without parameter");
                       }
                       Demo(int a)
                       {
                        System.out.println("Constructor with integer parameter "+a);
                       }
                       Demo(float b)
                       {
                        System.out.println("Constructor with float parameter "+b);
                       }
}
public class Democonover {
                       public static void main(String args[])
                       {
                        Demo obj1=new Demo();
                        Demo obj2=new Demo(4);
                        Demo obj3=new Demo(5.9f);
                       }
}


Output

D:\> javac Democonover.java
D:\>java Democonover
Constructor without parameter
Constructor with integer parameter 4
Constructor with float parameter 5.9


Explanation



Tuesday, 23 September 2014

Constructor program with parameter passing

 Constructor program with parameter passing

 Program

class Parmpass
{
          int i;
          Parmpass(int a)
          {
                   i=a;
                   System.out.println("Constructor with parameter "+i);
          }
}
class ParmpassMain
{
          public static void main(String args[])
          {
                   int b=10;
                   Parmpass obj= new Parmpass(b);
          }
}

Output   

D:\>java ParmpassMain
Constructor with parameter 10


Explanation 





Monday, 4 August 2014

Java program to find first 5 largest numbers in a list




import java.util.*;
class big
{
public static void  main(String args[])
{
int i;
System.out.println("Enter the size of the list (It should be greater than five) ");
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
int a[]=new int[n];
if(n>5)
{
System.out.println("Enter the elements ");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(sc.nextLine());
}
System.out.println("Entered elements are ");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

//sorting the elements
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Sorted list");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("First 5 largest elements are ");
for(i=n-1;i>=n-5;i--)
{
System.out.println(a[i]);
}

}
else
{
System.out.println("INVALID entry!!!!!");
System.out.println("Enter the size of the list greater than 5!!! ");
}

}

}



output



D:\java\notepad>javac big.java

D:\java\notepad>java big
Enter the size of the list (It should be greater than five)
1
INVALID entry!!!!!
Enter the size of the list greater than 5!!!

D:\java\notepad>javac big.java

D:\java\notepad>java big
Enter the size of the list (It should be greater than five)
6
Enter the elements
11
23
42
7
3
90
Entered elements are
11
23
42
7
3
90
Sorted list
3
7
11
23
42
90
First 5 largest elements are
90
42
23
11
7


D:\java\notepad>

Sunday, 3 August 2014

Java Program to find number of ones between 1 to 100

class ones
{
public void number()
{

int a[]=new int[20];
int j=0,count=0;;
for(int i=2;i<100;i++)
{
if(i%10==1)
count++;
if(i/10==1)
count++;

}
System.out.println("Number of ones between 1 to 100 is  "+count);

}
public static void main(String args[])
{
System.out.println("Welcome");
ones obj=new ones();
obj.number();

}
}