Skip to content

Instantly share code, notes, and snippets.

@LordNairu
Created February 13, 2014 17:32
Show Gist options
  • Save LordNairu/8979904 to your computer and use it in GitHub Desktop.
Save LordNairu/8979904 to your computer and use it in GitHub Desktop.
package practical2p3.uk.ac.qub;
public class CaesarCypher {
public CaesarCypher(){
}
public CaesarCypher (String stringToEncode, int codeKey) {
this.stringToEncode = stringToEncode;
this.codeKey = codeKey;
}
private String stringToEncode;
private int codeKey;
/**
* @return the stringToEncode
*/
public String getstringToEncode() {
return stringToEncode;
}
/**
* @param stringToEncode the stringToEncode to set
*/
public void setstringToEncode(String stringToEncode) {
this.stringToEncode = stringToEncode;
}
/**
* @return the codeKey
*/
public int getCodeKey() {
return codeKey;
}
/**
* @param codeKey the codeKey to set
*/
public void setCodeKey(int codeKey) {
this.codeKey = codeKey;
}
public String encode (String stringToEncode, int codeKey) {
String work;
char code;
code = 'a';
work ="";
for (int counter = 0; counter < stringToEncode.length(); counter++) {
code = stringToEncode.charAt(counter);
for (int loop = 0; loop < codeKey; loop++) {
if (code == 'Z') {
code = 'A';
} else if (code == 'z') {
code = 'a';
}
if (Character.isAlphabetic(code)) {
code++;
}
}
String codedMessage = Character.toString(code);
System.out.print(codedMessage);
work = codedMessage;
}
System.out.println(work);
return work;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment