Caesar cipher is implemented

This commit is contained in:
2021-07-25 15:57:53 -04:00
parent aaa2031274
commit 4f8b440c59
4 changed files with 293 additions and 47 deletions

View File

@@ -0,0 +1,121 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Caesar.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-25-21
//This is the declaration of the Caesar class
package mattrixwv.CipherStreamJava;
public class Caesar{
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
public static final String version = "1.0"; //The current version number for the library
private int shift; //The amount that you need to shift each letter
//Sets shift and makes sure it is within the propper bounds
private void setShift(int shiftAmount){
//If you shift more than 26 you will just be wrapping back around again
shift = shiftAmount % 26;
}
//Sets the input string
private void setInputString(String inputString){
this.inputString = inputString;
}
//Encodes the inputString and stores the result in outputString
private String encode(){
for(int cnt = 0;cnt < inputString.length();++cnt){
char temp = inputString.charAt(cnt); //A temperary holder for the current working character
//If it is an upper case letter shift it and wrap if necessary
if(Character.isUpperCase(temp)){
temp += shift;
//Wrap around if the letter is now out of bounds
if(temp < 'A'){
temp += 26;
}
else if(temp > 'Z'){
temp -= 26;
}
}
//If it is a lower case letter shift it and wrap if necessary
else if(Character.isLowerCase(temp)){
temp += shift;
//Wrap around if the letter is now out of bounds
if(temp < 'a'){
temp += 26;
}
else if(temp > 'z'){
temp -= 26;
}
}
//If it is whitespace, number, or punctuation just let it pass through
//Add it to the output string
outputString += temp;
}
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode(){
for(int cnt = 0;cnt < inputString.length();++cnt){
char temp = inputString.charAt(cnt); //A temperary holder for the current working character
//If it is an upper case letter shift it and wrap if necessary
if(Character.isUpperCase(temp)){
temp -= shift;
if(temp < 'A'){
temp += 26;
}
else if(temp > 'Z'){
temp -= 26;
}
}
//If it is a lower case letter shift it and wrap if necessary
else if(Character.isLowerCase(temp)){
temp -= shift;
if(temp < 'a'){
temp += 26;
}
else if(temp > 'z'){
temp -= 26;
}
}
//If it is whitespace, number, or punctuation just let it pass through
//Add it to the output string
outputString += temp;
}
return outputString;
}
//Constructor
public Caesar(){
reset();
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Returns shift
public int getShift(){
return shift;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Sets the shift and inputString and encodes the message
public String encode(int shiftAmount, String inputString){
reset();
setShift(shiftAmount);
setInputString(inputString);
return encode();
}
//Sets the shift and inputString and decodes the message
public String decode(int shiftAmount, String inputString){
reset();
setShift(shiftAmount);
setInputString(inputString);
return decode();
}
//Makes sure all of the variables are empty
public void reset(){
inputString = outputString = "";
shift = 0;
}
}