Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- /**
- * @author Kushtrim
- * @version 1.01
- */
- public class EncoderDecoder
- {
- public static void main(String args[])
- {
- EncoderDecoder ec = new EncoderDecoder();
- int key = new Integer(JOptionPane.showInputDialog("Jepni çelësin: ")).intValue();
- String fjalia = JOptionPane.showInputDialog("Rradhisni një fjali:");
- // textin e koduar e ruajme ne nje varg int
- int[] encodedText = ec.encode(fjalia,key);
- // menjehere po e kryejme dekodimin
- ec.decode(encodedText, key ) ;
- //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ë
- //marrim inputin nga perdoruesi
- //int[] txt = ec.merrInputin();
- //ec.decode(txt, key);
- }
- /**
- * @param fjalia Teksti që do të kodohet
- * @return fjaline te koduar si varg te int
- */
- public int[] encode (String fjalia, int key)
- {
- int[] code = new int[27];
- code[0] = key;
- for ( int i = 1; i != code.length; i++ )
- {
- code[i] = (int)(code[i - 1]*1.3 + 1);
- }
- int[] answer = new int[fjalia.length()];
- for ( int j = 0; j != fjalia.length(); j++ )
- {
- char c = fjalia.charAt(j);
- if ( c == ' ')
- {
- System.out.println(code[0]);
- }
- else if ( c >= 'a' && c <= 'z' )
- {
- int index = c - 'a' + 1;
- System.out.print(code[index] + " ");
- answer[j] =code[index];
- }
- else {
- System.out.println("Gabim: Karakter jokorrekt hyrës : " + c );
- System.exit(0);
- }
- }
- System.out.println();
- return answer;
- }
- /**
- * @param key - çelesi sipas se cilit krijohet tabela per dekodim
- * @param encodedText - texti i koduar si varg int
- */
- public void decode ( int[] encodedText, int key )
- {
- int[] code = new int[27];
- code[0] = key;
- for ( int i = 1; i != code.length; i++ )
- {
- code[i] = (int)(code[i - 1]*1.3 + 1);
- }
- for ( int i = 0 ; i!= encodedText.length; i++)
- {
- for (int j=0; j!=code.length ; j++)
- {
- if(encodedText[i] == code[j] )
- {
- if( j==0 )
- {
- System.out.print(" ");
- }
- else
- {
- char c= (char)((j +'a') -1);
- System.out.print(c +"");
- }
- }
- }
- }
- }
- /**
- * Merr tekstin e koduar ne forme te String nga perdoruesi
- * @return tekstin e koduar ne forme te vargut int
- */
- public int[] merrInputin()
- {
- String fjalia = JOptionPane.showInputDialog("Shkruaj numrat per t'i dekoduar");
- String[] s = fjalia.split(" ");
- int[] encodedText = new int[s.length];
- for ( int i = 0 ; i!= s.length; i++)
- {
- encodedText[i] = new Integer(s[i]).intValue();
- }
- return encodedText;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement