# artemkorsakov's solution

## to Rotational Cipher in the Java Track

Published at May 27 2019 · 0 comments
Instructions
Test suite
Solution

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. Using a key of `0` or `26` will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`. The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
``````

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

• ROT5 `omg` gives `trl`
• ROT0 `c` gives `c`
• ROT26 `Cool` gives `Cool`
• ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
• ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

``````\$ gradle test
``````

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### RotationalCipherTest.java

``````import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

public class RotationalCipherTest {

private RotationalCipher rotationalCipher;

@Test
public void rotateSingleCharacterBy0() {
rotationalCipher = new RotationalCipher(0);
Assert.assertEquals("a", rotationalCipher.rotate("a"));
}

@Ignore("Remove to run test")
@Test
public void rotateSingleCharacterBy1() {
rotationalCipher = new RotationalCipher(1);
Assert.assertEquals("b", rotationalCipher.rotate("a"));
}

@Ignore("Remove to run test")
@Test
public void rotateSingleCharacterBy26() {
rotationalCipher = new RotationalCipher(26);
Assert.assertEquals("a", rotationalCipher.rotate("a"));
}

@Ignore("Remove to run test")
@Test
public void rotateSingleCharacterBy13() {
rotationalCipher = new RotationalCipher(13);
Assert.assertEquals("z", rotationalCipher.rotate("m"));
}

@Ignore("Remove to run test")
@Test
public void rotateSingleCharacterWithWrapAround() {
rotationalCipher = new RotationalCipher(13);
Assert.assertEquals("a", rotationalCipher.rotate("n"));
}

@Ignore("Remove to run test")
@Test
public void rotateCapitalLetters() {
rotationalCipher = new RotationalCipher(5);
Assert.assertEquals("TRL", rotationalCipher.rotate("OMG"));
}

@Ignore("Remove to run test")
@Test
public void rotateSpaces() {
rotationalCipher = new RotationalCipher(5);
Assert.assertEquals("T R L", rotationalCipher.rotate("O M G"));
}

@Ignore("Remove to run test")
@Test
public void rotateNumbers() {
rotationalCipher = new RotationalCipher(4);
Assert.assertEquals("Xiwxmrk 1 2 3 xiwxmrk", rotationalCipher.rotate("Testing 1 2 3 testing"));
}

@Ignore("Remove to run test")
@Test
public void rotatePunctuation() {
rotationalCipher = new RotationalCipher(21);
Assert.assertEquals("Gzo'n zvo, Bmviyhv!", rotationalCipher.rotate("Let's eat, Grandma!"));
}

@Ignore("Remove to run test")
@Test
public void rotateAllLetters() {
rotationalCipher = new RotationalCipher(13);
Assert.assertEquals("The quick brown fox jumps over the lazy dog.",
rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt."));
}
}``````
``````import java.util.stream.Collectors;

class RotationalCipher {
private int shiftKey;

RotationalCipher(int shiftKey) {
if (shiftKey < 0 || shiftKey > 26) {
throw new IllegalArgumentException();
}
this.shiftKey = shiftKey % 26;
}

String rotate(String data) {
if (shiftKey == 0) {
return data;
}
return data.chars()
.mapToObj(ch -> String.valueOf(rotate((char) ch)))
.collect(Collectors.joining());
}

private char rotate(char letter) {
return Character.isLetter(letter) ? rotateLetter(letter) : letter;
}

private char rotateLetter(char letter) {
char firstLetter = Character.isUpperCase(letter) ? 'A' : 'a';
int pos = firstLetter + (letter - firstLetter + shiftKey) % 26;
return (char) pos;
}
}``````