Posts

Showing posts from 2012

Collection

                                          Set import java.util.HashSet; import java.util.Set; public class FindDuplicate {      public static void main(String[] args) {          Set<String> s = new HashSet<String>();          s.add("aman");          s.add("raja");          s.add("raja");          s.add("kumar");                   for (String a : args)              if (!s.add(a))                  System.out.println("Duplicate detected: " + a);                     System.out.println(s.size() + " distinct words: " + s);      }  } /* 3 distinct words: [kumar, aman, raja] */ -------------------------------------------------------------------------------------------------------------------------- import java.util.Collection; import java.util.LinkedHashSet; public class SetDuplicate {   public static void main(String[] args) {    Collection<Integer>noDups=new LinkedHashSet<Integer>(

toString() program in java

class A { static int i =5; /* public String toString() { return "i="+i; } void myMethod(){ System.out.println(" Aman "); }*/ public static void main(String[] args) { A obj = new A(); System. out .println( "obj.toString()----" +obj.toString()); System. out .println( Integer. toString ( i )); System. out .println(obj.hashCode()); /* System.out.println("obj.hashCode()----"+obj.hashCode()); A obj1 = new A(); System.out.println("obj.toString()----"+obj.toString()); System.out.println("obj1-----"+obj1); System.out.println("obj1.hashCode()-----"+obj1.hashCode()); System.out.println("equals method-----"+obj.equals(obj1)); A obj3= obj ; System.out.println(obj3.equals( obj )); System.out.println(obj.toString());*/ } }

Fibonacci Series

import java.io.* public class FibonacciSeries   {     public static void main(String[] args)throws IOException {       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));      System.out.println("Enter the number:");      int n1=Integer.parseInt(br.readLine());      int prev, next,sum,n;   prev=next=1;   for(n=1;n<=n1;n++)   {    System.out.println(prev);    sum=prev+next;    prev=next;    next=sum;   }  } }

Call By reference in java.

Call by Reference in java Many of the writer  said only call by value is possible in java not call by reference but i read call by reference: class CallByRef {   static int a , b ;   public CallByRef() {    a = 10;    b = 20;   } static void swap(CallByRef obj) {    int temp;   temp = CallByRef. a ; CallByRef.a=CallByRef.b; CallByRef.b=temp; System.out.println("In the swap() method obj has the value of and b are:" ); System. out .println( "a=" + CallByRef. a + "\t" + "b=" + CallByRef. b ); } public static void main(String[] args) { CallByRef obj1 =  new CaallByRef(); System. out .println( "Before swapping value of a and b in obj1" ); System. out .println( "a=" + CallByRef. a + "," + "b=" + CallByRef. b ); swap (obj1); System. out .println( "After swapping value of a and b in obj1" ); System. out .println( "a="