Top Secret
Back to the Program-A-Day homepage
ProblemYou are a software developer for CSIS (Canadian Security Inteligence Service) and you've been sent a secret message, but all of the software at your station has been deleted! However, this message is very important and must be decoded right away. That's where you come in. You will write a program that decodes the message, verifies the authenticity, then prints out the mission directive. First, the message is in a secret code. Every character is represented by a different character. The following arrays show the relationship. The first one is our alphabet, the second is the secret one. char [] array1 = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','.','!',' '};
Once you've verified that, you'll need to get the mission instruction out of the message. It is formed by taking every other letter starting with the second character of the decoded message. (I suggest making your method that takes every other character work for both palindrome and instruction) There is one final verification needed. The instruction must start with the phrase "top secret". If not, it's a fake! After you've determined that the instruction is the real deal, you have to print out just the mission instruction. (Use substring to print out the message after the "top secret" part)
|
Problem Solving and Programming Examples | |
---|---|---|
SolutionWhen you start working on your program, don't forget to copy in those two char arrays and secret message. We'll start this program by first deciding what methods we will need (apart from main), and what the signatures of those methods will be. This is an important step (especially during the exam) because it will prevent an unnecessary amount of erasing later on. From the problem stated above, we realise we need 4 methods. The first is to decode the message, and this one will have to be passed the secret message and the two char arrays. It will be returning the decoded message as a String. The second method is to take every other character, and combine them into a new String, which will be returned. It needs to be passed the String to perform this on, and we will be passing it an integer value. (The reason for this will be mentioned later on) The third method is to check the string produced by every even character to see if it's a palindrome. This will be passed the string to be checked, and will return a boolean. The fourth and last method is to check the string produced by every odd character to make sure it starts with the words "top secret". It too will be passed the string to check and the string containing the key to look for (the words "top secret"), and return a boolean.
String result = decode(secretMessage, array1, array2); String palindrome = everyOther(result,0); String message = everyOther(result,1);
if(checkPalindrome(palindrome)) { } else System.out.println("There was no palindrome. This message is a fake!");
if(checkSecrecy(message, start)) { System.out.print("Your secret mission is: "); System.out.println(message.substring(start.length()+1,message.length())); } else System.out.println("There was no \"top secret\". This message is a fake!");
public static String decode(String message, char [] array1, char [] array2) { String result = ""; return result; }
for(int i = 0; i < message.length(); i++) { for(int j = 0; j < array2.length; j++) { if(message.charAt(i) == array2[j]) result += array1[j]; } }
public static String everyOther(String str, int num) { String result = ""; return result; }
for(int i = 0; i < str.length(); i++) { if(i % 2 == num) result += str.charAt(i); }
public static boolean checkPalindrome(String str) { boolean result = true; for(int i = 0; i < str.length() / 2; i++) { } return result; }
if(str.charAt(i) != str.charAt(str.length() - 1 - i)) result = false;
public static boolean checkSecrecy(String message, String start) { boolean result = false; return result; }
if(message.substring(0,start.length()).equals(start)) result = true;
Also, for those curious, here is the palindrome used. I had to remove the spaces so checking it in the program would be easier. "Are we not drawn onward, we few, drawn onward to new era?"
| ||
Code |