In: Computer Science
Rules for this Assignment:
There are 2 classes required for this project:
Separation of code will be paramount in order to produce a proper and legible file. This will require that you analyse the problem and separate it into multiple tasks. Each task should then be represented by its own function.
The methods you define in the EncryptionProcedures class should be static.
Files to submit
In a properly developed project, all your code should be located in 2 java files, Encryption.java and EncryptionProcedures.java. You will need to submit those 2 files.
Additionally, a WORD document should also be submitted. In this document, you will explain the validity or invalidity of defining the methods in the EncryptionProcedures class as static. (minimum explanation, 5 lines)
Project Description
The goal of the project is to be able to retrieve an input from the console, encrypt it according to specifications that will be described below, and display the result.
The user input can be a single word, or an entire sentence (on a single line).
Specifications
private static final String[] arrPeriodicTableValues = {"h", "he", "li", "be", "b", "c", "n", "o", "f", "ne", "na", "mg", "al", "si", "p", "s", "cl", "ar", "k", "ca", "sc", "ti", "v", "cr", "mn", "fe", "co", "ni", "cu", "zn", "ga", "ge", "as", "se", "br", "kr", "rb", "sr", "y", "zr", "nb", "mo", "tc", "ru", "rh", "pd", "ag", "cd", "in", "sn", "sb", "te", "i", "xe", "cs", "ba", "la", "ce", "pr", "nd", "pm", "sm", "eu", "gd", "tb", "dy", "ho", "er", "tm", "yb", "lu", "hf", "ta", "w", "re", "os", "ir", "pt", "au", "hg", "tl", "pb", "bi", "po", "at", "rn", "fr", "ra", "ac", "th", "pa", "u", "np", "pu", "am", "cm", "bk", "cf", "es", "fm", "md", "no", "lr", "rf", "db", "sg", "bh", "hs", "mt", "ds", "rg", "cn", "nh", "fl", "mc", "lv", "ts", "og"};
The preceding code represents all the elements of the periodic table in the form of an Array. The index of each element can be used to reference its atomic number. This instruction can be included in your code for use in this assignment.
The encryption procedure will be a specific 1 and follow a strict set of rules in order to be properly implemented. The primary method will be called periodicEncrypt.
Here is the overall description of the procedure to follow in order to accomplish a proper encryption in this case:
Here is needed java code. (Please see screenshots given below to better understand code.)
EncryptionProcedures.java:
public class EncryptionProcedures {
private static final String[] arrPeriodicTableValues = { "h", "he", "li", "be", "b", "c", "n", "o", "f", "ne", "na",
"mg", "al", "si", "p", "s", "cl", "ar", "k", "ca", "sc", "ti", "v", "cr", "mn", "fe", "co", "ni", "cu",
"zn", "ga", "ge", "as", "se", "br", "kr", "rb", "sr", "y", "zr", "nb", "mo", "tc", "ru", "rh", "pd", "ag",
"cd", "in", "sn", "sb", "te", "i", "xe", "cs", "ba", "la", "ce", "pr", "nd", "pm", "sm", "eu", "gd", "tb",
"dy", "ho", "er", "tm", "yb", "lu", "hf", "ta", "w", "re", "os", "ir", "pt", "au", "hg", "tl", "pb", "bi",
"po", "at", "rn", "fr", "ra", "ac", "th", "pa", "u", "np", "pu", "am", "cm", "bk", "cf", "es", "fm", "md",
"no", "lr", "rf", "db", "sg", "bh", "hs", "mt", "ds", "rg", "cn", "nh", "fl", "mc", "lv", "ts", "og" };
/**
* Convert the given string to encrypted text using periodic table values
*
* @param str
* @return encryptedText
*/
public static String periodicEncrypt(String str) {
// to build encrypted text string
StringBuilder encryptedText = new StringBuilder("");
// Loop through input string
int i = 0;
while (str.length() > i) {
// getting periodic index (atomic value) for 2 chars
int periodicIndex = EncryptionProcedures.getPeriodicIndex(str.substring(i, i + 2));
if (periodicIndex != -1) {
// If found then append the index to encryptedText
encryptedText.append(Integer.toString(periodicIndex));
i += 2;
} else {
// Get periodic index for 1 char
periodicIndex = EncryptionProcedures.getPeriodicIndex(str.substring(i, i + 1));
if (periodicIndex != -1) {
// If found append index to encrypted text
encryptedText.append(Integer.toString(periodicIndex));
} else {
// append the character to encryptedText
encryptedText.append(str.charAt(i));
}
i++;
}
}
// Convert encryptedText to string and return
return encryptedText.toString();
}
/**
* Gets the periodic index (atomic value) for given string
*
* @param ch
* @return index
*/
private static int getPeriodicIndex(String ch) {
// loop through all the values in periodic table
for (int i = 0; i < EncryptionProcedures.arrPeriodicTableValues.length; i++) {
if (ch.equals(EncryptionProcedures.arrPeriodicTableValues[i])) {
// If string found then return index of string
return i;
}
}
// If not found return -1
return -1;
}
}
Encryption.java
import java.util.Scanner;
public class Encryption {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter a string: ");
String plainText = scan.next();
System.out.println("Encrypted Text: " + EncryptionProcedures.periodicEncrypt(plainText));
}
}
Output: