Просмотр исходного кода

rotational-cipher: estimate and tidy

Frida Tveit 9 лет назад
Родитель
Сommit
7975530e96

+ 7
- 7
config.json Просмотреть файл

145
       ]
145
       ]
146
     },
146
     },
147
     {
147
     {
148
+      "slug": "rotational-cipher",
149
+      "difficulty": 4,
150
+      "topics": [
151
+
152
+      ]
153
+    },
154
+    {
148
       "slug": "kindergarten-garden",
155
       "slug": "kindergarten-garden",
149
       "difficulty": 4,
156
       "difficulty": 4,
150
       "topics": [
157
       "topics": [
539
       "topics": [
546
       "topics": [
540
 
547
 
541
       ]
548
       ]
542
-    },
543
-    {
544
-      "slug": "rotational-cipher",
545
-      "difficulty": 1,
546
-      "topics": [
547
-
548
-      ]
549
     }
549
     }
550
   ],
550
   ],
551
   "deprecated": [
551
   "deprecated": [

+ 5
- 8
exercises/rotational-cipher/src/example/java/RotationalCipher.java Просмотреть файл

1
+class RotationalCipher {
1
 
2
 
2
-public class RotationalCipher {
3
-
4
-    int shiftKey;
3
+    private int shiftKey;
5
 
4
 
6
     RotationalCipher(int shiftKey) {
5
     RotationalCipher(int shiftKey) {
7
         this.shiftKey = shiftKey;
6
         this.shiftKey = shiftKey;
8
     }
7
     }
9
 
8
 
10
-    public String rotate(String data) {
9
+    String rotate(String data) {
11
         StringBuilder dataStringBuilder = new StringBuilder();
10
         StringBuilder dataStringBuilder = new StringBuilder();
12
         for (char c : data.toCharArray()) {
11
         for (char c : data.toCharArray()) {
13
             if (Character.isUpperCase(c)) {
12
             if (Character.isUpperCase(c)) {
25
      * For Uppercase CaseStart = 'A' and CaseEnd = 'Z'
24
      * For Uppercase CaseStart = 'A' and CaseEnd = 'Z'
26
      * For Lowercase CaseStart = 'a' and CaseEnd = 'z'
25
      * For Lowercase CaseStart = 'a' and CaseEnd = 'z'
27
      */
26
      */
28
-    private char getReplacementCharacter(char characterToReplace,
29
-            char alphabetCaseStart, char alphabetCaseEnd) {
27
+    private char getReplacementCharacter(char characterToReplace, char alphabetCaseStart, char alphabetCaseEnd) {
30
         char replacementCharacter = (char) (characterToReplace + shiftKey);
28
         char replacementCharacter = (char) (characterToReplace + shiftKey);
31
         if (replacementCharacter > alphabetCaseEnd) {
29
         if (replacementCharacter > alphabetCaseEnd) {
32
-            replacementCharacter = (char) ((alphabetCaseStart - 1) +
33
-             (replacementCharacter % alphabetCaseEnd));
30
+            replacementCharacter = (char) ((alphabetCaseStart - 1) + (replacementCharacter % alphabetCaseEnd));
34
         }
31
         }
35
         return replacementCharacter;
32
         return replacementCharacter;
36
     }
33
     }

+ 3
- 6
exercises/rotational-cipher/src/test/java/RotationalCipherTest.java Просмотреть файл

1
-
2
 import org.junit.Assert;
1
 import org.junit.Assert;
3
-import org.junit.Before;
4
 import org.junit.Ignore;
2
 import org.junit.Ignore;
5
 import org.junit.Test;
3
 import org.junit.Test;
6
 
4
 
13
         rotationalCipher = new RotationalCipher(1);
11
         rotationalCipher = new RotationalCipher(1);
14
         Assert.assertEquals("b", rotationalCipher.rotate("a"));
12
         Assert.assertEquals("b", rotationalCipher.rotate("a"));
15
     }
13
     }
16
-    
14
+
17
     @Ignore("Remove to run test")
15
     @Ignore("Remove to run test")
18
     @Test
16
     @Test
19
     public void rotateSingleCharacterBy26() {
17
     public void rotateSingleCharacterBy26() {
75
     public void rotateAllLetters() {
73
     public void rotateAllLetters() {
76
         rotationalCipher = new RotationalCipher(13);
74
         rotationalCipher = new RotationalCipher(13);
77
         Assert.assertEquals("The quick brown fox jumps over the lazy dog.",
75
         Assert.assertEquals("The quick brown fox jumps over the lazy dog.",
78
-         rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt."));
79
-    } 
80
-
76
+                rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt."));
77
+    }
81
 }
78
 }