Monday, February 29, 2016

Terrible Chandu From Hacker Earth

Chandu is a bad student. Once his teacher asked him to print the reverse of a given string. He took three hours to solve it. The teacher got agitated at Chandu and asked you the same question. Can you solve it?
Input:
The first line contains an integer T, denoting the number of test cases.
Each test case contains a string S, comprising of only lower case letters.
Output:
For each test case, print the reverse of the string S.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TerribleChandu {

public static void main(String[] args) 
{
Scanner scn=new Scanner(System.in);
System.out.println("Type Test cases");
int n=scn.nextInt();
String s1="";
String [] str=new String[n];
List<String> list=new ArrayList();
for(int i=0;i<str.length;i++)
    {
list.add(scn.next());
}
for(String l:list) 
     {
System.out.println(reverse(l.toLowerCase()));
}
}
public static String reverse(String str)
{
String nstr="";
char c;
for(int i=str.length()-1;i>=0;i--)
     {
c=str.charAt(i);
nstr+=c;
}
return nstr;
}
}

Thursday, February 4, 2016

Immutable Class In Java

String class is immutable class in java . once we create the object of the immutable class we c'nt change that object.
When one object is shared with others and when the value of the object is changed so others also will be affected. To overcome this problem immutable is solution.
When we create the string class object we can't change that object if we will try so that time one new object will be created and the reference of current object will point to new object and than old object will be abandon and eligible for garbage collector.

We can Make our own immutable class .

final  class MakeImutableClass
{
  final String panCardNo;
  public MakeImutableClass(String panCardNo)
{
this.panCardNo=panCardNo;
}
public String getPanCardNo()
{
return panCardNo;
}

}

public class ImmutableClass
{
public static void main(String[] args)
{
MakeImutableClass mic=new MakeImutableClass("first");
MakeImutableClass mic1=new MakeImutableClass("second");


System.out.println("first pan no. is "+mic.getPanCardNo());
System.out.println("second pan no. is " +mic1.getPanCardNo());

}

}