Showing posts with label core java. Show all posts
Showing posts with label core java. Show all posts

Tuesday, 1 January 2013

How to use method for calculating Fibonacci series?

public class MainClass {
   public static long fibonacci(long number) {
      if ((number == 0) || (number == 1))
         return number;
      else
         return fibonacci(number - 1) + fibonacci(number - 2);
   }
   public static void main(String[] args) {
      for (int counter = 0; counter <= 10; counter++){
         System.out.printf("Fibonacci of %d is: %d\n",
         counter, fibonacci(counter));
      }
   }
}
output:
 
 
Fibonacci of 0 is: 0
Fibonacci of 1 is: 1
Fibonacci of 2 is: 1
Fibonacci of 3 is: 2
Fibonacci of 4 is: 3
Fibonacci of 5 is: 5
Fibonacci of 6 is: 8
Fibonacci of 7 is: 13
Fibonacci of 8 is: 21
Fibonacci of 9 is: 34
Fibonacci of 10 is: 55

How to find the minimum and the maximum element in an array

import java.util.Arrays;
import java.util.Collections;

public class Main3 {
   public static void main(String[] args) {
      Integer[] numbers = { 8, 2, 7, 15, 4, 9, 5,99};
      int min = (int) Collections.min(Arrays.asList(numbers));
      int max = (int) Collections.max(Arrays.asList(numbers));
      System.out.println("Min number: " + min);
      System.out.println("Max number: " + max);
   }
}

// *.....How to reverse array........*//


import java.util.ArrayList;
import java.util.Collections;

public class Main {
   public static void main(String[] args) {
      ArrayList arrayList = new ArrayList();
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
      arrayList.add("D");
      arrayList.add("E");
      System.out.println("Before Reverse Order: " + arrayList);
      Collections.reverse(arrayList);
      System.out.println("After Reverse Order: " + arrayList);
   }
}

o/p:

Before Reverse :- [A, B, C, D, E]
After  Reverse :- [E, D, C, B, A]

Wednesday, 31 October 2012

What are different types of inner classes? Nested -level classes, Member classes, Local classes, Anonymous classes. Explain them




There are 4 types of inner classes - Member inner class, Local inner class, Static inner class and Anonymous inner class
1. Member inner class – A member of a class(enclosing class).
2. Local inner class – An inner class that is defined within a block.
3. Static inner class – Like static members, this class itself is static.
4. Anonymous inner class – A class without a name and implements exactly only one interface or exactly extends one abstract class.

The enclosing class can not have the accessibility to the inner class. To do so, the outer class must create an object of its nested class except for static inner class. The member classes can access all of its enclosing class’s members, because inner class is like a member of a class.
Anonymous inner class is used when an object creation, one time single method invocation to be done and releasing the object at once. This is particular in event driven programming.
The nested classes are implemented in handling AWT, Swing and Applet programming. An anonymous inner class is invoked when an action event is to be performed.

Wednesday, 8 August 2012

Write a prgram to how to print " X " In java ?????????


class Demo{
public static void main(String [] args)
{
 char prnt = 'X';
int i, j, s, nos = 0;  //nos controls the spacing.
//1st triangle
for (i = 9; i >= 1; i = i - 2) {
for (s = nos; s >= 1; s--) {  //Spacing control
System.out.print("  ");
}
for (j = 1; j <= i; j++) {
if (j == 1 || j == i) { //This hollows the triangle
System.out.print(prnt);
} else {
System.out.print("  ");
}
}
System.out.print("\n");
nos++;  // In the upper triangle the space increments by 1.
}
/* Since both the triangles have the same peak point, while printing the second triangle we'll ignore its peak point. Thus i starts from 3 and the nos from 3.*/
nos = 3;
for (i = 3; i <= 9; i = i + 2) {
for (s = nos; s >= 1; s--) {
System.out.print("  ");
}
for (j = 1; j <= i; j++) {
if (j == 1 || j == i) {
System.out.print( prnt);
} else {
System.out.print("  ");
}
}
System.out.print("\n");
nos--; //The spaces are in a decrementing order.
}

}
}
OUT PUT :


   X                            X 
     X                       X
        X                X 
           X         X
                 X
             X      X
        X               X
    X                       X
X                               X

Tuesday, 24 July 2012

examples of tricky and confusing java questions which asked in written test of java job interview ?(Right solution)

class A
{
public void m1(String str)
{
System.out.println("I am from String agrument method");
}

public void m1(Object obj)
{
System.out.println("I am from Object argument method");
}
public static void main(String args[])
{
A aobj=new A();
aobj.m1(null);
}
}


Solution : -
     "I am from String agrument method"

Why is java called "purely" object oriented language ?

Solution : -
               Java is NOT a purely Object Oriented Language because given following step below : -
             
      1. Not all pre-defined types in Java (primitives) are objects.
                   2. Static variables can be used without creating an instance of an object.
                   3. Overriding a static method / variable isn’t possible.
                   4. Operations on objects should only be done through methods
                       exposed by the object which   isn’t entirely true in Java .
                      Example : - “Foo” + “Bar”

Wednesday, 11 July 2012

/* Program to change an integer to words - NUMTOWD.C */


# include <stdio.h>
# include <conio.h>
void main()
{
long n, a[10], i, c = 0 ;
clrscr() ;
printf("Enter a number : ") ;
scanf("%ld", &n) ;
while(n > 0)
{
a[c] = n % 10 ;
n = n / 10 ;
c++ ;
}
printf("\n") ;
for(i = c - 1 ; i >= 0 ; i--)
{
switch(a[i])
{
case 0 :
printf("ZERO ") ;
break ;
case 1 :
printf("ONE ") ;
break ;
case 2 :
printf("TWO ") ;
break ;
case 3 :
printf("THREE ") ;
break ;
case 4 :
printf("FOUR ") ;
break ;
case 5 :
printf("FIVE ") ;
break ;
case 6 :
printf("SIX ") ;
break ;
case 7 :
printf("SEVEN ") ;
break ;
case 8 :
printf("EIGHT ") ;
break ;
B.Bhuvaneswaran A.45
case 9 :
printf("NINE ") ;
break ;
}
}
getch() ;
}
RUN 1 :
~~~~~~~
Enter a number : 225589
TWO TWO FIVE FIVE EIGHT NINE

What is Map Collection?

A map collection refers to a set of maps that are compiled and organized for a specific purpose, such as research, education, or preservatio...