Skip to content

Commit 09e7662

Browse files
authored
CaesarCipher by Coursera
1 parent 6a0fde9 commit 09e7662

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

java/CaesarCipher/CaesarCipher.ctxt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#BlueJ class context
2+
comment0.target=CaesarCipher
3+
comment1.params=input\ key
4+
comment1.target=java.lang.String\ encrypt(java.lang.String,\ int)
5+
comment2.params=
6+
comment2.target=void\ testCaesar()
7+
numComments=3

java/CaesarCipher/CaesarCipher.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import edu.duke.*;
2+
3+
public class CaesarCipher {
4+
public String encrypt(String input, int key) {
5+
//Make a StringBuilder with message (encrypted)
6+
StringBuilder encrypted = new StringBuilder(input);
7+
//Write down the alphabet
8+
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
9+
//Compute the shifted alphabet
10+
String shiftedAlphabet = alphabet.substring(key)+
11+
alphabet.substring(0,key);
12+
//Count from 0 to < length of encrypted, (call it i)
13+
for(int i = 0; i < encrypted.length(); i++) {
14+
//Look at the ith character of encrypted (call it currChar)
15+
char currChar = encrypted.charAt(i);
16+
//Find the index of currChar in the alphabet (call it idx)
17+
int idx = alphabet.indexOf(currChar);
18+
//If currChar is in the alphabet
19+
if(idx != -1){
20+
//Get the idxth character of shiftedAlphabet (newChar)
21+
char newChar = shiftedAlphabet.charAt(idx);
22+
//Replace the ith character of encrypted with newChar
23+
encrypted.setCharAt(i, newChar);
24+
}
25+
//Otherwise: do nothing
26+
}
27+
//Your answer is the String inside of encrypted
28+
return encrypted.toString();
29+
}
30+
public void testCaesar() {
31+
int key = 17;
32+
FileResource fr = new FileResource();
33+
String message = fr.asString();
34+
String encrypted = encrypt(message, key);
35+
System.out.println(encrypted);
36+
String decrypted = encrypt(encrypted, 26-key);
37+
System.out.println(decrypted);
38+
}
39+
}
40+

java/CaesarCipher/message1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FREE CAKE IN THE CONFERENCE ROOM!
2+

java/CaesarCipher/message2.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Dear Owen,
2+
No matter what you may have heard, there is no cake
3+
in the conference room. The cake is a lie. Please keep
4+
working on Coursera videos.
5+
6+
Thanks,
7+
Drew

java/CaesarCipher/package.bluej

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#BlueJ package file
2+
editor.fx.0.height=34
3+
editor.fx.0.width=199
4+
editor.fx.0.x=-32000
5+
editor.fx.0.y=-32000
6+
objectbench.height=90
7+
objectbench.width=758
8+
package.divider.horizontal=0.6
9+
package.divider.vertical=0.7983367983367984
10+
package.editor.height=377
11+
package.editor.width=652
12+
package.editor.x=-32000
13+
package.editor.y=-32000
14+
package.frame.height=600
15+
package.frame.width=800
16+
package.numDependencies=0
17+
package.numTargets=1
18+
package.showExtends=true
19+
package.showUses=true
20+
project.charset=UTF-8
21+
readme.height=58
22+
readme.name=@README
23+
readme.width=47
24+
readme.x=10
25+
readme.y=10
26+
target1.height=50
27+
target1.name=CaesarCipher
28+
target1.naviview.expanded=true
29+
target1.showInterface=false
30+
target1.type=ClassTarget
31+
target1.typeParameters=
32+
target1.width=110
33+
target1.x=70
34+
target1.y=10

0 commit comments

Comments
 (0)