Advertisement
Kushtrim

EncoderDecoder

Apr 30th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import javax.swing.*;
  2. /**
  3.  * @author Kushtrim
  4.  * @version 1.01
  5.  */
  6. public class EncoderDecoder
  7.  
  8. {
  9.     public static void main(String args[])
  10.     {
  11.         EncoderDecoder ec = new EncoderDecoder();
  12.  
  13.         int key = new Integer(JOptionPane.showInputDialog("Jepni çelësin: ")).intValue();
  14.         String fjalia = JOptionPane.showInputDialog("Rradhisni një fjali:");
  15.  
  16.         // textin e koduar e ruajme ne nje varg int
  17.         int[] encodedText  = ec.encode(fjalia,key);
  18.         // menjehere po e kryejme dekodimin
  19.         ec.decode(encodedText, key ) ;
  20.  
  21.         //decode mund ta perdorim edhe ndamas prej encode, p.sh. nje tekst e kodojme me heret , e ruajme diku , dhe tash pas nje kohe duam ta deshifrojmë
  22.         //marrim inputin nga perdoruesi
  23.         //int[] txt = ec.merrInputin();
  24.         //ec.decode(txt, key);
  25.  
  26.     }
  27.  
  28.     /**
  29.      * @param fjalia Teksti që do të kodohet
  30.      * @return fjaline te koduar si varg te int
  31.      */
  32.     public int[] encode (String fjalia, int key)
  33.     {
  34.         int[] code = new int[27];
  35.         code[0] = key;
  36.  
  37.         for ( int i = 1; i != code.length; i++ )
  38.         {
  39.             code[i] = (int)(code[i - 1]*1.3 + 1);
  40.         }
  41.  
  42.         int[] answer = new int[fjalia.length()];
  43.  
  44.         for ( int j = 0; j != fjalia.length(); j++ )
  45.         {
  46.             char c = fjalia.charAt(j);
  47.             if ( c == ' ')
  48.             {
  49.                 System.out.println(code[0]);
  50.             }
  51.             else if ( c >= 'a' && c <= 'z' )
  52.             {
  53.                 int index = c - 'a' + 1;
  54.                 System.out.print(code[index] + " ");
  55.                 answer[j] =code[index];
  56.             }
  57.             else {
  58.                 System.out.println("Gabim: Karakter jokorrekt hyrës : " + c );
  59.                 System.exit(0);
  60.             }
  61.         }
  62.         System.out.println();
  63.  
  64.         return answer;
  65.     }
  66.  
  67.     /**
  68.      * @param key - çelesi sipas se cilit krijohet tabela per dekodim
  69.      * @param encodedText -  texti i koduar si varg int
  70.      */
  71.     public void decode ( int[] encodedText, int key )
  72.     {
  73.         int[] code = new int[27];
  74.         code[0] = key;
  75.         for ( int i = 1; i != code.length; i++ )
  76.         {
  77.             code[i] = (int)(code[i - 1]*1.3 + 1);
  78.         }
  79.  
  80.         for ( int i = 0 ; i!= encodedText.length; i++)
  81.         {
  82.             for (int j=0; j!=code.length ; j++)
  83.             {
  84.                 if(encodedText[i] == code[j] )
  85.                 {
  86.                     if( j==0 )
  87.                     {
  88.                         System.out.print(" ");
  89.                     }
  90.                     else
  91.                     {
  92.                         char c= (char)((j +'a') -1);
  93.                         System.out.print(c +"");
  94.                     }
  95.  
  96.                 }
  97.             }
  98.  
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Merr tekstin e koduar ne forme te String nga perdoruesi
  104.      * @return  tekstin e koduar ne forme te vargut int
  105.      */
  106.     public int[] merrInputin()
  107.     {
  108.         String fjalia = JOptionPane.showInputDialog("Shkruaj numrat per t'i dekoduar");
  109.  
  110.         String[] s = fjalia.split(" ");
  111.         int[] encodedText = new int[s.length];
  112.  
  113.         for ( int i = 0 ; i!= s.length; i++)
  114.         {
  115.             encodedText[i] = new Integer(s[i]).intValue();
  116.         }
  117.  
  118.         return encodedText;
  119.     }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement