Need help writing this method in Java. Can\'t use any methods in Java library cl
ID: 675506 • Letter: N
Question
Need help writing this method in Java. Can't use any methods in Java library class Arrays.
Q1:
Write a method caesarEncrypt which takes as input a String originalMessage and an int shift and returns the String created by applying a shift of size shift to the original message. Note that your method should work with all non-negative integers. That is, you can assume that the input will never be smaller than zero. In the case that shift is greater than 25, you should cycle over the alphabet an additional time. For example the letter a shifted by 30 is the same as the letter a shifted by 4, which would be e.
Q2:
Write a method caesarDecrypt which takes as input a String encoded and an int shift and returns the String created by applying a reverse shift of shift to the original message. caesarEncrypt() is the inverse of caesarDecrypt(). This means that for any String s, caesarDecrypt(caesarEncrypt(s)) is the same as s
Hint: Can you use the method encrypt() that you wrote in the previous question to help for this part?
Explanation / Answer
string caesarEncrypt(string s,int shift){
char[] arr = s.toCharArray();
string encrypted = "";
for(int i = 0; i < s.length(); i++){
encrypted += (char)arr[i] % 26;
}
return encrypted;
}
string caesarDecrypt(string s,int shift){
return caesarEncrypt(s,shift*-1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.