Tampilkan postingan dengan label java interview programs. Tampilkan semua postingan
Tampilkan postingan dengan label java interview programs. Tampilkan semua postingan

Selasa, 05 Juli 2016

Java Program to find shortest palindrome in string

  • We have a Letter or a word then we need add some letters to it and need to find out shortest palindrome 
  • For example we take "S":  S will be the shortest palindrome string.
  • If we take "xyz"zyxyz will be the shortest palindrome string
  • So we need to add some characters to the given string or character and find out what will be the shortest palindrome string by using simple java program.


Java example Program to find out shortest palindrome of given string


  1. package shortestpalindromeexample.java;
  2. import java.util.Scanner;
  3.  
  4. public class ShortestPalindromeDemo {
  5.  
  6. public static String shortestPalindrome(String str) {
  7.      
  8. int x=0;  
  9. int y=str.length()-1;
  10.      
  11.   while(y>=0){
  12.      if(str.charAt(x)==str.charAt(y)){
  13.           x++;
  14.          }
  15.             y--;
  16.   }
  17.  
  18. if(x==str.length())
  19. return str;
  20.  
  21. String suffix = str.substring(x);
  22. String prefix = new StringBuilder(suffix).reverse().toString();
  23. String mid = shortestPalindrome(str.substring(0, x));
  24.  
  25. return prefix+mid+suffix;
  26. }
  27.  
  28. public static void main(String[] args) {
  29.  
  30. Scanner in = new Scanner(System.in);
  31.  
  32. System.out.println("Enter a String to find out shortest palindrome");
  33.  
  34. String str=in.nextLine();
  35.  
  36. System.out.println("Shortest palindrome of "+str+" is "+shortestPalindrome(str));
  37.  
  38. }
  39.  
  40. }
Output:

  1. Enter a String to find out shortest palindrome
  2. java
  3. Shortest palindrome of java is avajava

find shortest palindrome in java program

Baca selengkapnya

Jumat, 10 Juni 2016

toString() method in java with example program

  • toString() method belongs to object class.
  • By default it will be called once  whenever we try to print object.
  • toString() method by default will print classname@hex representation of hashcode.
  • We can override this toString() method from object class.


  1. public String toString(){
  2.  
  3.    return null;
  4.        
  5. }

    Advantage of toString() method in java.

    • When ever we try to print object by default toString() method will be called and print classname@HEX_hashcode.
    • We can represent object in String format using toString() method.
    • if we want to represent string representation of object then we need to override toString() method in our class and return values of the object as a String.




     Java example program to print object without using toString() method.
    • Lets see an example program on printing object of a class.

    1. package tostringexamples;
    2. public class ToStringDemo {
    3.  
    4.     int a,b;
    5.  
    6. ToStringDemo(int x, int y){
    7.         a=x;
    8.         b=y;
    9.  }
    10.  
    11. public static void main(String[] args) {
    12.  
    13.  ToStringDemo obj= new ToStringDemo(1,2);
    14.         
    15.  ToStringDemo obj2= new ToStringDemo(3,4);
    16.         
    17.     System.out.println(obj);
    18.     System.out.println(obj2);
    19.  
    20.  }
    21.  
    22. }


    Output:


    1. tostringexamples.ToStringDemo@2a139a55
    2. tostringexamples.ToStringDemo@15db9742

    •  In the above program when we print object of ToStringDemo class it prints ToStringDemo@2a139a55.
    • It means when ever we print object of the class toString() method will be called and by default toString() method print classname@HEX_hashcode.
    • To test this now we will override the toString() method and prints object of the class and if it calls our method then we can understand that when ever we print object by default toString() method will be called.

     Java example program to print object by overriding toString() method.

    override tostring method in java example

    • In above example when we print object it executed overridden toString() method.
    • By overriding toString() method we can represent object in string form.

     

    toString() method used to convert data to String type. 

    •   By using toString method we can convert any wrapper object to string object
       
    Java example program to convert integer to string using toString() method.

    1. package tostringexamples;
    2. public class ToStringDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6.   Integer a=12;
    7.   Integer b=37;
    8.  
    9.   String str1=a.toString();
    10.   String str2 =b.toString();
    11.         
    12.   System.out.println(str1);
    13.   System.out.println(str2);
    14.  
    15.     }
    16. }

    Output:

    1. 12
    2. 37

    Java example program to convert Float to String using toString() method.

    1. package tostringexamples;
    2. public class ToStringDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6. Float  a=12.0f;
    7. Float b=33.4f;
    8.  
    9.   String str1=a.toString();
    10.   String str2 =b.toString();
    11.         
    12.   System.out.println(str1);
    13.   System.out.println(str2);
    14.  
    15.     }
    16. }

    Output:

    1. 12.0
    2. 33.4
    Baca selengkapnya