Question: CIPHER PROBLEM in JAVA For this question your job is to complete the definition of a Cipher class whose instances can be used to encode

CIPHER PROBLEM in JAVA

For this question your job is to complete the definition of a Cipher class whose instances can be used to encode and decode encrypted messages. Messages are encrypted by substituting each letter with another letter according to a key. This key is a String that consists of a permutation of the 26 letters of the English alphabet such as

"BAYZCDWXEFUVGHSTIJQRKLOPMN". To encrypt a message using this key, each A (the FIRST letter in the alphabet) is substituted by an B (the FIRST letter in the key), each B (the SECOND letter in the alphabet) is substituted by an A (the SECOND letter on the key), and so on. To decode an encrypted message the reverse transformation is used. Any characters not representing alphabetic letters are not encrypted. Non-letter symbols such as digits, quotes and question marks are not encoded, and therefore do not require decoding.

For instance the following encrypted message:

GCCR GC EH RXC WMG

Should decode to the following message using the key mentioned above:

MEET ME IN THE GYM

The class must have encode and decode methods which MUST use corresponding Map data structures build by the Cipher constructor to conduct the letter substitutions.

NOTE: DO NOT CHANGE THE CODE. ONLY ADD WHERE INDICATED!

GIVEN CLASS:

import java.util.HashMap;

import java.util.Map;

public class Cipher {

private Map encodeMap;

private Map decodeMap;

private static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public Cipher(String key) {

encodeMap = new HashMap();

decodeMap = new HashMap();

for (int i=0; i

char clearChar = alphabet.charAt(i);

char cipherChar = key.charAt(i);

encodeMap.put(clearChar, cipherChar);

decodeMap.put(cipherChar, clearChar);

}

}

/**

* validKey(key) - returns true iff key consists of exactly 26 upper case alphabetic characters

*/

public static boolean validCipher(String key) {

// YOUR CODE

return false; // Dummy return

}

public String encode(String msg) {

// YOUR CODE

return ""; // Dummy return

}

public String decode(String msg) {

// YOUR CODE

return ""; // Dummy return

}

}

G I V E N TEST CASE

import static org.junit.Assert.*;

import org.junit.Test;

public class CipherTest {

@Test

public void testValidCipher() {

assertTrue("validCipher: Incorrect validation", Cipher.validCipher("ABYZCDWXEFUVGHSTIJQRKLOPMN"));

assertFalse("validCipher: Incorrect validation", Cipher.validCipher("NMPOLKRQJITSHG1UFEXWDC#YBA"));

assertFalse("validCipher: Incorrect validation", Cipher.validCipher("NMPOLKRQJI"));

}

@Test

public void testDecode1() {

Cipher c1 = new Cipher("BAYZCDWXEFUVGHSTIJQRKLOPMN");

String t = c1.decode(new String("GCCR GC EH RXC WMG"));

assertTrue("decode: Incorrect result", c1.decode(new String("GCCR GC EH RXC WMG")).equals("MEET ME IN THE GYM"));

}

@Test

public void testDecode2() {

Cipher c1 = new Cipher("BACDZYXWEFGHVUTSIJKLRQPOMN");

String t = c1.decode(new String("PWTK BYJBED TY NZAJBK?"));

assertTrue("decode: Incorrect result", c1.decode(new String("PWTK BYJBED TY NZAJBK?")).equals("WHOS AFRAID OF ZEBRAS?"));

}

@Test

public void testEncode1() {

Cipher c1 = new Cipher("ABYZCDWXEFUVGHSTIJQRKLOPMN");

assertTrue("encode: Incorrect result", c1.encode(new String("MEET ME IN THE GYM")).equals("GCCR GC EH RXC WMG"));

}

@Test

public void testEncode2() {

Cipher c1 = new Cipher("ABCDZYXWEFGHVUTSIJKLRQPOMN");

assertTrue("encode: Incorrect result", c1.encode(new String("WHOS AFRAID OF ZEBRAS?")).equals("PWTK AYJAED TY NZBJAK?"));

}

@Test

public void testEncode3() {

Cipher c1 = new Cipher("ABYZCDWXEFUVGHSTIJQRKLOPMN");

assertTrue("encode: Incorrect result", c1.encode(new String("MEET ME IN THE GYM")).equals("GCCR GC EH RXC WMG"));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!