Thursday 16 April 2015

http://jayasatyatech.com/febonicseries




public class FebonicSeries {

public static void main(String[] args) {
int x=1;
int y=1;
int z=0;

for(int k=0;k<=6;k++)
{

x=y+z;
y=z;
z=x;
System.out.println(""+x);

}
}

}

Monday 24 November 2014

Java Programs For Freshers

1)Bubble sort
A)
import java.util.Scanner;
 
class BubbleSort {
  public static void main(String []args) {
    int n, c, d, swap;
    Scanner in = new Scanner(System.in);
 
    System.out.println("Input number of integers to sort");
    n = in.nextInt();
 
    int array[] = new int[n];
 
    System.out.println("Enter " + n + " integers");
 
    for (c = 0; c < n; c++) 
      array[c] = in.nextInt();
 
    for (c = 0; c < ( n - 1 ); c++) {
      for (d = 0; d < n - c - 1; d++) {
        if (array[d] > array[d+1]) /* For descending order use < */
        {
          swap       = array[d];
          array[d]   = array[d+1];
          array[d+1] = swap;
        }
      }
    }
 
    System.out.println("Sorted list of numbers");
 
    for (c = 0; c < n; c++) 
      System.out.println(array[c]);
  }
}
 
o/p: 

Friday 7 November 2014

All Java Programs

Q)First java program.
A)
public class ClassEx {

    public static void main(String[] args) {
   
    System.out.println("First program");   
    }
}


class: class keyword is used to declare a class in java.
public: public   keyword is an access modifier which represents visibility, it means it is visible to all.
static: static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
void:void is the return type of the method, it means it doesn't return any value.
main :main represents startup of the program.
String[] args: String[] args is used for command line argument.
System.out.println() :System.out.println() is used print statement.
System:java.long.System is a class
out:
out is a object of system class
println():
println() is a mthod of print stream class
Q)Can i change the order of public static void main(String[] args) to static  public void main(String[] args)  ?
A)Yes  you can change  ,JVM won't throws any exceptions.





OOP'S Concepts(Note: Interviewer is asking more Questions on oop's. I will explain later in detail )

1) Abstraction & Encapsulation
2) Polymorphism
3) Inheritance
4) Abstract Class
5) Interface

Q) What is Abstraction?
A) Hiding the implementation and providing the service is called Abstraction.

Q) What is the Encapsulation?
A) Binding the data along with its related functionalities is called Encapsulation.

Ex:
class Account {
private double balance;

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}
}
Advantages:
1) Security
2) Easy Enhancement
3) Improve Maintainability

Q) What is Polymorphism ?
A) This has been derived from Greek word. 'Poly' means Many, Morphism means 'Forms'. Totally one name with multiple forms is called Polymorphism.

Types of Polymorphism.
  • Static Binding   or  Compile Time  Polymorphism or Early Binding.
Ex: Method overloading
  • Dynamic Binding  or Run time Polymorphism  or Late Binding.
Ex : Method overriding.

Q) What is Method over loading .
A) Defining multiple Methods with same name, with different arguments in a class is nothing but over loading.

Ex:

public class MethodOverLoading {

public static void main(String[] args) {

MethodOverLoading overLoading= new MethodOverLoading();

overLoading.yogi(10);
overLoading.yogi(10,20);
overLoading.yogi(10,20f);


}
public void yogi(int i)
{
System.out.println("org1"+i);
}
public void yogi(int i, int j)
{
System.out.println("org2"+i+"org2"+j);
}
public void yogi(int j, double k)
{
System.out.println("org3"+j+"org4"+k);
}
public void yogi(int j, double k, float f)
{
System.out.println("org5"+j+"org6"+k);
}

}

Q) What is the Method overriding?
A) Defining a method in Sub Class with the same signature of the Super Class  or  defining a method in the Super Class with the same signature of the Sub Class is called a Method Overriding

Ex:

public class OverRidingEx {

public static void main(String[] args) {

Z z1= new Z();
z1.one();
X x1= new X();
x1.one();
//Z zz1= new x(); //compilation error
X xx1= new Z();
xx1.one();

}

}

class X
{
public void one()
{
System.out.println("super class");
 }
}
 
class Z extends X
{
public void one()
{
System.out.println("sub class");

}

}

out-put
-------
sub class
super class
sub class





Q) What is  Inheritance?

A) Inheritance is the concept of getting the properties of one class to another class or one object to another object is  called Inheritance.

Q) Why multiple Inheritance doesn't support java.
A)

public class InheritanceEx {

public static void main(String[] args) {

}

}

class A extends B,C
{
public void M1()
{
System.out.println("class A");}
}

class B
{
public void M1()
{
System.out.println("class B");}
}

class C
{
public void M1()
{
System.out.println("class C");}
}

Explanation:
1. Multiple Inheritance is not supported in java just because of the Violation of super() keyword.
When Ever you called the method/constructor of super class at that time JVM got confused,it cant decide which method you are calling
Q) What is Abstract class?
A) Partially implemented and Partially unimplemented class is Abstract class. That means Abstract class contains, abstract methods as well as implemented(concrete) methods.

Ex:
abstract class AbstractClassEx1
{
    AbstractClassEx1(){
       
    }
public abstract void m1();
public void m2()
{
System.out.println("yogi");   
}
}

Q) What is Interface?
A) Fully unimplemented structure called Interface. That means Interface contains only Abstract methods and  variables.

Ex:
public interface InterFaceEx {
    int j=20;
    public static final int i=10;
    public void m1();
    public void m2();
    public void m3();
   
}
Q) What is the class?
A) Fully implemented structure is called a class 

public class ClassEx {

    public static void main(String[] args) {
   
        ClassEx ex= new ClassEx();
        ex.m1();
    }

    public void m1()
    {
    System.out.println("methos imp");   
    }
}
Q) What is Object?
A)  Instance of a class is called object.











Saturday 24 May 2014