Question

In: Computer Science

Rules for this Assignment: There are 2 classes required for this project: Encryption (the primary class...

Rules for this Assignment:

There are 2 classes required for this project:

  • Encryption (the primary class containing your main() method and the retrieval of the message)
  • EncryptionProcedures (where your code regarding the encryption method of that message will be located)

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:

  1. Go through the original string from left to right
  2. Look at the first 2 characters
  3. Verify if those 2 characters match 1 of the elements in the periodic table
    1. If it does, use the atomic number as a replacement for those 2 characters
    2. If it doesn’t, go back and look at just 1 character
      1. Verify if that character matches 1 of the elements in the periodic table
        1. If it does, use the atomic number as a replacement for that character
        2. If it doesn’t, leave that character unchanged
  4. Repeat the process until you reach the end of the original string
  5. Display the encrypted String

Solutions

Expert Solution

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:


Related Solutions

C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes...
FlashCards with Classes and Exception Handling – Project Project Overview Create at least 2 object classes (Session and Problems) and one driver class and ensure the user inputs cannot cause the system to fail by using exception handling. Overview from Project . Implement a Java program that creates math flashcards for elementary grade students. User will enter his/her name, the type (+,-, *, /), the range of the factors to be used in the problems, and the number of problems...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
Provide an example of a project, assignment, or in-class activity that could be used as a...
Provide an example of a project, assignment, or in-class activity that could be used as a means of employing multiple assessments of ELLs. Why are multiple assessments important?
Note: This is a single file C++ project - No documentation is required on this assignment....
Note: This is a single file C++ project - No documentation is required on this assignment. Write a Fraction class whose objects will represent Fractions. Note: You should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. You must provide the following member functions: A...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
You will write classes from the last in-class assignment, R&DLibrary. R&DLibrary is the main file in...
You will write classes from the last in-class assignment, R&DLibrary. R&DLibrary is the main file in which a user can choose to enroll themselves, check out an item, return an item, pay a fine and display their account (what is currently checked out, accumulated fines). Write a material class that calculates the fine based off of the type of item, the length it’s been checked out and whether or not it’s even been checked out. Lastly, write a employee Class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT