Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CIPHER PROBLEM in JAVA For this question your job is to complete the definition

ID: 3880401 • Letter: C

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 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<Character, Character> encodeMap;

private Map<Character, Character> decodeMap;

private static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public Cipher(String key) {

encodeMap = new HashMap<Character, Character>();

decodeMap = new HashMap<Character, Character>();

for (int i=0; i<key.length(); 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("PWT’K BYJBED TY NZAJBK?"));

assertTrue("decode: Incorrect result", c1.decode(new String("PWT’K BYJBED TY NZAJBK?")).equals("WHO’S 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("WHO’S AFRAID OF ZEBRAS?")).equals("PWT’K 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"));

}

}

Explanation / Answer

Hi.. I have added the code in method bodies and tested according to that. Please check below code.

Cipher.java

import java.util.HashMap;

import java.util.Map;

public class Cipher {

private Map<Character, Character> encodeMap;

private Map<Character, Character> decodeMap;

private static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public Cipher(String key) {

encodeMap = new HashMap<Character, Character>();

decodeMap = new HashMap<Character, Character>();

for (int i=0; i<key.length(); 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) {

int lowerCase=0;

for (int k = 0; k < key.length(); k++) {

if (Character.isLowerCase(key.charAt(k))) lowerCase++;

}

if(lowerCase>0){

return false;

}

return true;

}

public String encode(String msg) {

String decodeMsg="";

for (int k = 0; k < msg.length(); k++) {

char chr;

char ch = msg.charAt(k);

int asciivalue = (int)ch;

if(asciivalue >=65 && asciivalue <=90){

chr = encodeMap.get(ch);

}else{

chr=ch;

}

decodeMsg+=chr;

}

return decodeMsg;

}

public String decode(String msg) {

String encodeMsg="";

for (int k = 0; k < msg.length(); k++) {

char chr;

char ch = msg.charAt(k);

int asciivalue = (int)ch;

if(asciivalue >=65 && asciivalue <=90){

chr = decodeMap.get(ch);

}else{

chr=ch;

}

encodeMsg+=chr;

}

return encodeMsg;

}

}

I get output for your example like below:

WYYJ WY CX JPY OGW
MEET ME IN THE GYM

Please check the code and let me know any issues. Thank you. All the best.