Write a Java program. A study of cryptography and its growth as a science can sh
ID: 3588798 • Letter: W
Question
Write a Java program.
A study of cryptography and its growth as a science can shed light on the nature of languages and on certain aspects of history. For example, the level of education of the Boers in the Boer War can be noted from the fact that the British officers often sent messages written in normal schoolboy Latin, secure in the fact that if they did fall into the wrong hands, they could not be understood. One of the simplest codes (used from the times of the Greeks) is representing the letters of the alphabet by numbers. An array contains a series of two-digit integers. Each integer represents one character of the alphabet as follows:
00 represents a blank space
01 represents an A
02 represents a B
03 represents a C
.
.
.
26 represents a Z
Write a program that reads the entries and prints the characters corresponding to the integers. The program should check that all integers are between 0 and 26. If this is not the case, the program should print a message indicating that the data is invalid. Decode 10-01-22-01-00-25-05-01.
Explanation / Answer
/***************************Cryptography.java*****************************/
import java.util.Scanner;
/**
* The Class Cryptography.
* @author lmali
*/
public class Cryptography {
/**
* Decode.
*
* @param encrypt the encrypt
* @return the string
*/
public static String decode(String encrypt) {
String[] values = encrypt.split("-");
int baseValue = 64;
StringBuilder sb = new StringBuilder();
for (String digits : values) {
int num = Integer.parseInt(digits);
if (num == 0) {
sb.append(" ");
} else if (num > 0 && num < 26) {
sb.append(Character.toString((char) (baseValue + num)));
} else {
System.out.println("Data is invalid.");
return "";
}
}
return sb.toString();
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please Enter the encoded line");
String line = input.nextLine();
String result = decode(line);
System.out.println(result);
input.close();
}
}
/***************************output*********************************/
Please Enter the encoded line
10-01-22-01-00-25-05-01
JAVA YEA
Thanks a lot. Please let me know if you have any doubt.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.