Repository: TheAlgorithms/Java
Pull request #5690 (2024-10-10 02:44:53)

Add cipher class, cipher tests, enhance docs in 'AtbashCipher.java'
- [✅] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [✅] This pull request is all my own work -- I have not plagiarized it.
- [✅] All filenames are in PascalCase.
- [✅] All functions and variable names follow Java naming conventions.
- [✅] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [✅] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5690?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `77.77778%` with `4 lines` in your changes missing coverage. Please review.
> Project coverage is 59.73%. Comparing base [(`6535d47`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6535d4755d8bed3cd410571568d0200f01bd806a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`8d62a3f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/8d62a3f5e900a528309a1980e85a1f182eb1b3ef?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5690?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...n/java/com/thealgorithms/ciphers/AtbashCipher.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5690?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fciphers%2FAtbashCipher.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9jaXBoZXJzL0F0YmFzaENpcGhlci5qYXZh) | 77.77% | [2 Missing and 2 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5690?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##             master    #5690   +/-   ##
=========================================
  Coverage     59.72%   59.73%           
- Complexity     3854     3865   +11     
=========================================
  Files           560      561    +1     
  Lines         16011    16029   +18     
  Branches       3072     3075    +3     
=========================================
+ Hits           9563     9575   +12     
- Misses         6045     6048    +3     
- Partials        403      406    +3     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5690?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

siriak: Please update with the latest master branch
Krounosity: > Looks good, thanks!

Thank you!
Krounosity: > Please update with the latest master branch

Done, do check

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 7ebc176b..e022ddc2 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -50,6 +50,7 @@
             * [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java)
             * [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java)
             * [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java)
+            * [AtbashCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AtbashCipher.java)
             * [Autokey](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Autokey.java)
             * [Blowfish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Blowfish.java)
             * [Caesar](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Caesar.java)
@@ -663,6 +664,7 @@
               * [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
             * [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java)
             * [AffineCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AffineCipherTest.java)
+            * [AtbashTest](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AtbashTest.java)
             * [AutokeyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java)
             * [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
             * [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
diff --git a/src/main/java/com/thealgorithms/ciphers/AtbashCipher.java b/src/main/java/com/thealgorithms/ciphers/AtbashCipher.java
new file mode 100644
index 00000000..c3b67314
--- /dev/null
+++ b/src/main/java/com/thealgorithms/ciphers/AtbashCipher.java
@@ -0,0 +1,71 @@
+package com.thealgorithms.ciphers;
+
+/**
+ * The Atbash cipher is a simple substitution cipher that replaces each letter
+ * in the alphabet with its reverse.
+ * For example, 'A' becomes 'Z', 'B' becomes 'Y', and so on. It works
+ * identically for both uppercase and lowercase letters.
+ * It's a symmetric cipher, meaning applying it twice returns the original text.
+ * Hence, the encrypting and the decrypting functions are identical
+ * @author https://github.com/Krounosity
+ * Learn more: https://en.wikipedia.org/wiki/Atbash
+ */
+
+public class AtbashCipher {
+
+    private String toConvert;
+
+    // Default constructor.
+    AtbashCipher() {
+    }
+
+    // String setting constructor.
+    AtbashCipher(String str) {
+        toConvert = str;
+    }
+
+    // String getter method.
+    public String getString() {
+        return toConvert;
+    }
+
+    // String setter method.
+    public void setString(String str) {
+        toConvert = str;
+    }
+
+    // Checking whether the current character is capital.
+    private boolean isCapital(char ch) {
+        return ch >= 'A' && ch <= 'Z';
+    }
+
+    // Checking whether the current character is smallcased.
+    private boolean isSmall(char ch) {
+        return ch >= 'a' && ch <= 'z';
+    }
+
+    // Converting text to atbash cipher code or vice versa.
+    public String convert() {
+
+        // Using StringBuilder to store new string.
+        StringBuilder convertedString = new StringBuilder();
+
+        // Iterating for each character.
+        for (char ch : toConvert.toCharArray()) {
+
+            // If the character is smallcased.
+            if (isSmall(ch)) {
+                convertedString.append((char) ('z' - (ch - 'a')));
+            }
+            // If the character is capital cased.
+            else if (isCapital(ch)) {
+                convertedString.append((char) ('Z' - (ch - 'A')));
+            }
+            // Non-alphabetical character.
+            else {
+                convertedString.append(ch);
+            }
+        }
+        return convertedString.toString();
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/ciphers/AtbashTest.java b/src/test/java/com/thealgorithms/ciphers/AtbashTest.java
new file mode 100644
index 00000000..26812cf2
--- /dev/null
+++ b/src/test/java/com/thealgorithms/ciphers/AtbashTest.java
@@ -0,0 +1,28 @@
+package com.thealgorithms.ciphers;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class AtbashTest {
+
+    @Test
+    public void atbashEncrypt() {
+        AtbashCipher normalToEncrypt = new AtbashCipher("Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!");
+        String expectedText = "Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!";
+
+        normalToEncrypt.setString(normalToEncrypt.convert());
+
+        assertEquals(expectedText, normalToEncrypt.getString());
+    }
+
+    @Test
+    public void atbashDecrypt() {
+        AtbashCipher encryptToNormal = new AtbashCipher("Svool Dliow! 123, @xrksvi zyxWVU ABC 987 nzwzn aaA Kzormwilnv!");
+        String expectedText = "Hello World! 123, @cipher abcDEF ZYX 987 madam zzZ Palindrome!";
+
+        encryptToNormal.setString(encryptToNormal.convert());
+
+        assertEquals(expectedText, encryptToNormal.getString());
+    }
+}