20 October 2013

Difference between subSequence and substring in Java (CharSequence and String)

Hi all,

Lets see the two method in Java - subSequence and substring.

When we consider a string str = "Hello World.!!" and call these two method with same parameters they returns the same value.

class StringSeqSub
{
 public static void main(String args[])
 {
  String str = "Hello World.!!";
  System.out.println(str.substring(0,5));
  System.out.println(str.subSequence(0,5));  
 } 
}

When we execute the about code, we get the output
Output - 
Hello
Hello

Now question comes out if both return the same value why java developers provided two method if one method can fulfill the demand.??
Let see the signature of both methods.
public CharSequence subSequence(int beginIndex, int endIndex)
public String substring(int beginIndex, int endIndex)
now we can find return type is different in both cases.
class StringSeqSub
{
    public static void main(String args[])
    {
        String str = "Hello World.!!";
        // String s1 = str.subSequence(0, 5);    // this line will throw error
        CharSequence s1 = str.subSequence(0, 5);
        String s2 = str.substring(0, 5);
        System.out.println(s1);
        System.out.println(s2);
    } 
}
and again the output will be same
Output - 
Hello
Hello

Now lets see the basic difference between CharSequence and String class..
1. 'String'  is a class while on the other side 'CharSequence'  is interface.
2.  'String'  implements the 'CharSequence'  interface. CharSequence is implemented by String, but
     also CharBuffer, Segment, StringBuffer, StringBuilder.

3. String[] and a CharSequence[] is essentially the same. But CharSequence is the abstraction, and
    String is the implementation.

Now as in point 2 I told you 'String' implements the interface 'CharSequence' so we can hold the object of string in reference variable of CharSequence.
     CharSequence s1 = str.subSequence(0, 5);
     // String s1 = str.subSequence(0, 5);  // this line will throw error
     String s2 = str.substring(0, 5);
     CharSequence s3 = str.substring(0, 5); // this line will not throw error

for more details on this refer these link link1 and link2.

Happy knowledge sharing.. :)

Possible arrangement of characters in String

Hi All,

Let see how can we find all the possible arrangement of characters in String.

First we need to understand the basic concept how can we find all the arrangement. Consider if we have string "ABC" then how can we find the all possible arrangement.

We will trace the string backward from right to left (from 'C' to 'A')

1. get the Last char 'C'

2. get the second last char 'B' and try to place it before and after C. You will get the possible combination - "CB" and "BC"

3. Now take the 'A' and try find all the possible combination from "CB" and "BC". and You will get all the possible combination - ACB, CAB, CBA   and ABC,  BAC,  BCA.

now lets convert this concept in algorithm using Java Language.

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

public class perm
{
        static List<String> words = new ArrayList<String>();
        
 public static void main(String args[])
 {
  String str="XYZ";
                
  char c[] = str.toCharArray();
  
  for(int i=c.length-1; i > -1; i--)
                {
                    getPossibleCombination(c[i]);
                }
                                
                for(int i=0; i < words.size(); i ++)
                {
                    System.out.println(words.get(i));
                }                
 }

        public static void getPossibleCombination(char c)
        {
            String result;
            if(words.isEmpty())
            {
                words.add(String.valueOf(c));
            }
            else
            {     
                List<String> ll = new ArrayList<String>();
                for(String str : words)
                {
                    for(int i=0; i <= str.length(); i++)
                    {
                        result = charInsert(str, c, i);
                        if(ll.indexOf(result)==-1)
                        {
                            ll.add(result);   
                        }                        
                    }
                }
                words = ll;
                ll = null;
            }
        }
        
    public static String charInsert(String str, char c, int j)
    {
        String begin = str.substring(0, j);
        String end = str.substring(j);
        return begin + c + end;
    }
}

Thats it.. when you will run this program we will get the output
Output :
XYZ
YXZ
YZX
XZY
ZXY
ZYX

Happy Knowledge sharing.. :)

17 October 2013

Design a Logo using SVG (Scalable Vector Graphics)

Hi  All,

Let see how can we create Java Logo using SVG.  If you are aware with basic of SVG else you can get basic SVG understand from this link.

To design a Logo you must know the basic path tag and curve etc. Open notepad and paste the following content and save the file and "JavaLogo.svg" and open it in any browser to see the Java Logo you just designed . :)

<svg version="1.1" id="Layer_1" xmlns="https://www.w3.org/2000/svg" x="0px" y="0px"
      width="300px" height="550px" viewBox="0 0 300 550">
    <path style="fill:#5382A1;" 
             d="M102.681,291.324
                c0,0-14.178,8.245,10.09,11.035
                c29.4,3.354,44.426,2.873,76.825-3.259  
                c0,0,8.518,5.341,20.414,9.967
                C137.38,340.195,45.634,307.264,102.681,291.324"/>
             
    <path style="fill:#5382A1;" 
             d="M93.806,250.704
                c0,0-15.902,11.771,8.384,14.283
                c31.406,3.24,56.208,3.505,99.125-4.759 
                c0,0,5.936,6.018,15.27,9.309
                C128.771,295.215,30.962,271.562,93.806,250.704"/>
                
    <path style="fill:#E76F00;" 
             d="M168.625,181.799
                c17.896,20.604-4.702,39.145-4.702,39.145
                s45.441-23.458,24.572-52.833 
                c-19.491-27.394-34.438-41.005,46.479-87.934
                C234.974,80.177,107.961,111.899,168.625,181.799"/>
                
    <path style="fill:#5382A1;" 
             d="M264.684,321.369
                c0,0,10.492,8.645-11.555,15.333
                c-41.923,12.7-174.488,16.535-211.314,0.506 
                c-13.238-5.759,11.587-13.751,19.396-15.428
                c8.144-1.766,12.798-1.437,12.798-1.437
                c-14.722-10.371-95.157,20.364-40.857,29.166
                C181.236,373.524,303.095,338.695,264.684,321.369"/>
                
    <path style="fill:#5382A1;" 
             d="M109.499,208.617
                c0,0-67.431,16.016-23.879,21.832
                c18.389,2.462,55.047,1.905,89.193-0.956 
                c27.906-2.354,55.927-7.359,55.927-7.359
                s-9.84,4.214-16.959,9.075
                c-68.475,18.009-200.756,9.631-162.674-8.79
                C83.313,206.851,109.499,208.617,109.499,208.617"/>
                
    <path style="fill:#5382A1;" 
             d="M230.462,276.231
                c69.608-36.171,37.424-70.931,14.96-66.248
                c-5.506,1.146-7.961,2.139-7.961,2.139 
                s2.044-3.202,5.948-4.588
                c44.441-15.624,78.619,46.081-14.346,70.52
                C229.063,278.055,230.14,277.092,230.462,276.231"/>
                
    <path style="fill:#E76F00;" 
             d="M188.495,4.399c0,0,38.55,38.563-36.563,97.862
                c-60.233,47.568-13.735,74.69-0.025,105.678  
                c-35.159-31.722-60.961-59.647-43.651-85.637
                C133.663,84.151,204.049,65.654,188.495,4.399"/>
                
    <path style="fill:#5382A1;" 
             d="M116.339,374.246
                c66.815,4.277,169.417-2.373,171.847-33.988
                c0,0-4.671,11.985-55.219,21.503  
                c-57.028,10.732-127.364,9.479-169.081,2.601
                C63.887,364.361,72.426,371.43,116.339,374.246"/>
                
    <path style="fill:#E76F00;" 
             d="M105.389,495.048
                c-6.303,5.467-12.96,8.536-18.934,8.536
                c-8.527,0-13.134-5.113-13.134-13.314 
                c0-8.871,4.936-15.357,24.739-15.357
                h7.328
                V495.048 
                M122.781,514.671
                v-60.742
                c0-15.517-8.85-25.756-30.188-25.756 
                c-12.457,0-23.369,3.076-32.238,6.999l2.56,10.752
                c6.983-2.563,16.022-4.949,24.894-4.949
                c12.292,0,17.58,4.949,17.58,15.181
                v7.677
                h-6.135
                c-29.865,0-43.337,11.593-43.337,28.994
                c0,15.017,8.878,23.553,25.594,23.553
                c10.745,0,18.766-4.436,26.264-10.928
                l1.361,9.22
                H122.781z"/>
                
    <path style="fill:#E76F00;" 
             d="M180.825,514.671
                h-21.692
                l-26.106-84.96
                h18.943
                l16.199,52.2l3.601,15.699 
                c8.195-22.698,13.991-45.726,16.89-67.899
                h18.427
                C202.15,457.688,193.266,488.396,180.825,514.671"/>
                
    <path style="fill:#E76F00;" 
             d="M264.038,495.048
                c-6.315,5.467-12.984,8.536-18.958,8.536
                c-8.512,0-13.131-5.113-13.131-13.314 
                c0-8.871,4.948-15.357,24.749-15.357h7.34
                V495.048 
                M281.428,514.671
                v-60.742
                c0-15.517-8.872-25.756-30.185-25.756
                c-12.466,0-23.382,3.076-32.247,6.999l2.556,10.752
                c6.986-2.563,16.042-4.949,24.907-4.949
                c12.283,0,17.579,4.949,17.579,15.181
                v7.677
                h-6.145
                c-29.874,0-43.34,11.593-43.34,28.994
                c0,15.017,8.871,23.553,25.584,23.553
                c10.751,0,18.769-4.436,26.28-10.928
                l1.366,9.22
                H281.428z"/>
                
    <path style="fill:#E76F00;" 
             d="M36.847,529.099
                c-4.958,7.239-12.966,12.966-21.733,16.206
                l-8.587-10.105 
                c6.673-3.424,12.396-8.954,15.055-14.105
                c2.3-4.581,3.252-10.485,3.252-24.604
                v-96.995
                h18.478
                v95.666 
                C43.311,514.038,41.802,521.663,36.847,529.099"/>
</svg>

And the output will be this Java Logo  - 


To understand the basic of SVG follow the link SVG and Tags.

Happy Knowledge sharing.. :)


15 October 2013

URL Coder/Encoder

Hi All,

Let see how can we use coding / encoding url.

  • Paste the encoded text and click decode button to DECODE.
  • Paste the decoded text and click encode button to ENCODE

URL Decoder/Encoder



Lets see how can we write our own URL encoder decoder.
  • Open notepad.
  • Paste the following contents.

<html>
<head>
   <title>MyTacTics.blogspot.in</title>
   <script type="text/javascript">
      function encode() {
         var obj = document.getElementById('dencoder');
         var unencoded = obj.value;
         obj.value = encodeURIComponent(unencoded).replace(/'/g,"%27").replace(/"/g,"%22");   
      }
      function decode() {
         var obj = document.getElementById('dencoder');
         var encoded = obj.value;
         obj.value = decodeURIComponent(encoded.replace(/\+/g,  " "));
      }
   </script>
</head>
<body>
   <form onsubmit="return false">
   <h1>
   URL Decoder/Encoder</h1>
   <textarea cols="80" id="dencoder" rows="20" style="resize: none;"></textarea>
   <div>
      <input onclick="decode()" type="button" value="Decode" />
      <input onclick="encode()" type="button" value="Encode" />
   </div>
   </form>
</body>


  • Save the file as URL.html, and its done.

Happy Knowledge sharing.. :)

14 October 2013

Full/Half Circle, Rectangle in SVG file.

Hi Friends,

Today let see how can we create the a logo or any icon using the SVG (Scalable Vector Graphics).

Initially we need to understand the basic on svg file.  Whats the basic and how can we start to create our first svg file.
1.  Open notepad and write the following lines -


<svg version="1.1" height="100px" width="100px"  xmlns="https://www.w3.org/2000/svg" >
      <circle cx="50px" cy="50px" r="40px" style="fill:green" />
</svg>

2.  Save the file as FirstSVG.svg.

3.  Open the file in any browser it will show you a circle with filled green color.


Here the first and last line is mandatory and in between we write the code according to what we want. here its basic SVG file.

we have few standard tags for different-different design which we can use to draw icon or logo, like - Line, Circle, eclipse, rectangle, polygon or any other path.

Line- 
<svg version="1.1" height="100px" width="200px"  xmlns="https://www.w3.org/2000/svg" >
      <line x1="0" y1="0" x2="200" y2="100"
          style="stroke: #E28965;stroke-width:2" />
</svg>


Circle- 
<svg version="1.1" height="100px" width="100px"  xmlns="https://www.w3.org/2000/svg" >
      <circle cx="50px" cy="50px" r="40px" style="fill:#89BDFF; stroke:#E28965;
      stroke-width:5;" />
</svg>



Rectangle- 
<svg version="1.1" height="100px" width="200px"  xmlns="https://www.w3.org/2000/svg" >
      <rect x="10px" y="10px" height="80px" width="180px" style="fill:#89BDFF;
      stroke:#E28965; stroke-width:5;" />
</svg>



Ellipse- 
<svg version="1.1" height="100px" width="200px"  xmlns="https://www.w3.org/2000/svg" >
      <ellipse cx="100px" cy="50px" rx="90px" ry="40" style="fill:#89BDFF;
      stroke:#E28965; stroke-width:5;" />
</svg>



Polygon- 
<svg version="1.1" height="200px" width="200px"  xmlns="https://www.w3.org/2000/svg" >
      <polygon points="100,10 40,180 190,60 10,60 160,180" style="fill:#89BDFF;
      stroke:#E28965;stroke-width:5;fill-rule:evenodd;" />
</svg>



Half-Circle(Clipping)- 
<svg version="1.1" height="200px" width="200px"  xmlns="https://www.w3.org/2000/svg" >
      <clipPath id="cut-off-bottom">
          <rect x="0" y="0" width="200" height="100" />
      </clipPath>

      <circle cx="100" cy="100" r="100" clip-path="url(#cut-off-bottom)" />
</svg>



Path- 
<svg version="1.1" height="200px" width="200px"  xmlns="https://www.w3.org/2000/svg" >
      <path d="M10,10 L100,190 L190,100 Z" style="fill:#89BDFF;
      stroke:#E28965;stroke-width:5;" />
</svg>

Lets understand the tag <path> and its parameter d = "... "

M10,10  -> we want to start from 10,10
L100,190 -> draw a line to the point 100,190
L190,100 -> draw a line to the point 190,100
Z -> go from current point to start point mean (10,10)

Curv (<Path> tag)- 
<svg version="1.1" height="200px" width="220px"  xmlns="https://www.w3.org/2000/svg" >
      <path style="fill:#89BDFF;stroke:#E28965;stroke-width:2;" d="M10,60
 c0,0,50,-50,100,-13
 c0,0,50,40,100,13
 c0,0,-50,50,-100,13
 c0,0,-50,-40,-100,-13
 " />
</svg>


To Understand the tag <path>  in more details refer this page 

Happy Knowledge Sharing.. :)


1 October 2013

Disable Right Click in webpage using jQuery

Hi Friends,

Let see how can we disable the "Right Click" in a web page using the jQuery.  Sometimes we need this snippet to prevent the user to perform few operations. But remember this is OK for normal user but for expert it would not work.  They know lot of way to show the context menu.

Well whatever lets see how can we disable the right click..
<html>
<head>
  <title>Right Click Disabled</title>
  <script src="https://code.jquery.com/jquery-1.9.1.js" type="text/javascript">
  </script>
  <script type="text/javascript">
    $(function () {
      // to disable the right click on whole web page
       $(document).bind("contextmenu",function(e){
         e.preventDefault();
         alert("Right Click is not allowed");
      });
     
     // to disable the right click on all the images use the below code
     
  /*
      $('img').bind("contextmenu",function(e){
         e.preventDefault();
         alert("Right Click is not allowed on images");
      });      
      */
    });
  </script>
</head>
<body style="height:400px; border:1px solid black;">
    This is the body content. 
    <img src="head.png" />
</body>
</html>
Without prompting any alert just to disable the right click you have to remove the alert box
$(function () {
      // to disable the right click on whole web page
       $(document).bind("contextmenu",function(e){
         e.preventDefault();
         alert("Right Click is not allowed"); 
      });
  });
Happy knowledge sharing.. :)