Das erste Projekt in Java, weshalb man etwas Nachsehen beim Source
haben darf, war ein Verschlüsselungsprogramm, welches die einfache
Cäsarverschlüsselung umsetzen sollte.
Das damalige römische Reich verwendete den nach Cäsar benannten
Algorithmus, welcher jeden Buchstaben des zu verschlüsselnden Text um
die Position des Schlüsselbuchstabens im Alphabet verschoben wird.
Nachdem die Cäsar-Klasse recht schnell fertig war, wollten ich das Programm noch um Mutltithreading erweitern.
Cäsar Klasse mit
Multithreading:
Code
- public class caesar {
- protected
- char[] alphabet;
- long start_millis;
- String s;
- char key;
- boolean mode;
- String[] strings = null;
- codingThread[] threads;
- int threadanzahl;
- int prioritaet;
- ThreadGroup tg;
- public caesar() {
- super();
- alphabet = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
- start_millis=0;
- }
- public String start(String st, char k,boolean m, int tanzahl,int prioritaet) {
- this.prioritaet = prioritaet;
- start_time();
- this.key=k;
- this.s=st;
- this.mode=m;
- this.threadanzahl=tanzahl;
- if ((this.threadanzahl >= this.s.length())){
- this.threadanzahl = 1;
- }
- strings = new String[threadanzahl+1];
- threads = new codingThread[threadanzahl+1];
- return allesaugeben();
- }
- public String encode(String s,int iThread) {
- String sneu = "";
- char[] schars;
- schars = s.toCharArray();
- int shl = indexOf(alphabet,key);
- if(shl!=-1) {
- for(int i = 0; i<=schars.length-1;i++) {
- if(!(threads[iThread].isInterrupted())) {
- int ckey = indexOf(alphabet,schars[i]);
- if(ckey!= -1) {
- if((shl+ckey) < alphabet.length) {
- sneu+=Character.toUpperCase(alphabet[(shl+ckey)]);
- } else {
- sneu+=Character.toUpperCase(alphabet[(shl+ckey)-alphabet.length]);
- }
- } else {
- sneu += schars[i];
- }
- } else {
- break;
- }
- }
- return sneu;
- } else {
- return "";
- }
- }
- public String decode(String s, int iThread) {
- String sneu = "";
- char[] schars;
- schars = s.toCharArray();
- int shl = indexOf(alphabet,key);
- if(shl!=-1) {
- for(int i = 0; i<=schars.length-1;i++) {
- int ckey = indexOf(alphabet,schars[i]);
- if(ckey!= -1) {
- if((ckey-shl)> 0) {
- sneu+= Character.toLowerCase(alphabet[(ckey-shl)]);
- } else if((ckey-shl < 0)) {
- sneu+=Character.toLowerCase(alphabet[alphabet.length+ckey-shl]);
- } else {
- sneu+=Character.toLowerCase(alphabet[0]);
- }
- }else {
- sneu += schars[i];
- }
- }
- return sneu;
- } else {
- return "";
- }
- }
- public int indexOf(char[] chr , char ch) {
- try {
- for(int i=0;i<=chr.length;i++) {
- if(Character.toUpperCase(ch)==chr[i]) {
- return i;
- }
- }
- return -1;
- } catch(ArrayIndexOutOfBoundsException e) {
- return -1;
- }
- }
- public void pause() {
- for(int i=1;i<= threads.length-1;i++) {
- threads[i].interrupt();
- }
- }
- public void resume() {
- tg.resume();
- }
- public String count_chars(String s) {
- double len = (double) s.length();
- String[] shr;
- shr = new String[2];
- if(len > 1000) {
- shr[0] = (Double.toString(len/1000));
- if(len > 1000000) {
- shr[0]= shr[0].substring(0,shr[0].length()-4)+"k";
- } else {
- shr[0]= shr[0].substring(0,shr[0].length()-2)+"k";
- }
- } else {
- shr[0]=Integer.toString((int)len);
- }
- return shr[0];
- }
- public void start_time() {
- start_millis = System.currentTimeMillis();
- }
- public String dif_time() {
- long dif = System.currentTimeMillis()-start_millis;
- double tmpsec =(double) dif/1000;
- return Double.toString(tmpsec)+"sec";
- }
- public String char_sec(String s) {
- return Integer.toString((int)((double)s.length()/(((double) System.currentTimeMillis()-start_millis)/1000)))+" Zeichen/sec";
- }
- class codingThread extends Thread {
- private
- String s;
- boolean mode;
- int lastIndex;
- public codingThread(boolean b, String s_code,int i) {
- this.mode=b;
- this.s = s_code;
- this.lastIndex=i;
- this.start();
- }
- @Override
- public void run() {
- if(mode) {
- strings[lastIndex]=encode(this.s,lastIndex);
- } else {
- strings[lastIndex]=decode(this.s,lastIndex);
- }
- }
- public void warten() {
- try {
- this.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public String allesaugeben(){
- int i = 0;
- String[] sub = null;
- String s_komplett = "";
- tg = new ThreadGroup("The Threads");
- sub = new String[threadanzahl+1];
- int l = (int) s.length()/threadanzahl;
- for(i=1;i<=(int) s.length()/l;i++ ) {
- try{
- if((i) ==(int) s.length()/l) {
- sub[i]=s.substring(l*(i-1), s.length());
- break;
- } else {
- sub[i]=s.substring(l*(i-1), (i)*l);
- }
- } catch (ArrayIndexOutOfBoundsException e) {
- }}
- for(i=1;i<=sub.length-1;i++) {
- threads[i] = new codingThread(mode,sub[i],i);
- }
- tg.enumerate(threads);
- tg.setMaxPriority(prioritaet);
- for(i=1;i<=strings.length-1;i++) {
- threads[i].warten();
- s_komplett+=strings[i];
- }
- return s_komplett;
- }
- }
Es fehlte nun nur noch eine GUI, welche kurz darauf fertig war.
Die Source:
Die Jar