Class Encrypt


  • public class Encrypt
    extends java.lang.Object
    A class that "encrypts" data through a simple transformation.

    The provided code is incomplete. Modify it so that it works properly and passes the unit tests in EncryptTest.java.

    See Also:
    MP1 Documentation, Caesar Cipher Documentation
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_SHIFT
      Maximum shift that encrypter and decrypter need to handle.
      static int MIN_SHIFT
      Minimum shift that encrypter and decrypter need to handle.
      static int TRANSFORM_END
      Transformation end.
      static int TRANSFORM_MODULUS
      Modulo to use for our transformation.
      static int TRANSFORM_START
      Transformation start.
    • Constructor Summary

      Constructors 
      Constructor Description
      Encrypt()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static char[] decrypter(char[] line, int shift)
      Decrypt a single line of text using a rotate-N transformation.
      static char[] encrypter(char[] line, int shift)
      Encrypt a single line of text using a rotate-N transformation.
      static void main(java.lang.String[] unused)
      Solicits a single line of text from the user, encrypts it using a random shift, and then decrypts it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MIN_SHIFT

        public static final int MIN_SHIFT
        Minimum shift that encrypter and decrypter need to handle.
        See Also:
        Constant Field Values
      • MAX_SHIFT

        public static final int MAX_SHIFT
        Maximum shift that encrypter and decrypter need to handle.
        See Also:
        Constant Field Values
      • TRANSFORM_START

        public static final int TRANSFORM_START
        Transformation start.
        See Also:
        Constant Field Values
      • TRANSFORM_END

        public static final int TRANSFORM_END
        Transformation end.
        See Also:
        Constant Field Values
      • TRANSFORM_MODULUS

        public static final int TRANSFORM_MODULUS
        Modulo to use for our transformation.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Encrypt

        public Encrypt()
    • Method Detail

      • encrypter

        public static char[] encrypter(char[] line,
                                       int shift)
        Encrypt a single line of text using a rotate-N transformation.

        The printable range of ASCII characters starts at decimal value 32 (' ') and ends at 126 ('~'). You should shift characters within this range by the shift value provided. For example, ' ' (32) shift 1 becomes '!' (33), while '~' (126) shift 1 wraps around and becomes ' ' (32). You may want to explore modular arithmetic to simplify the transformation.

        Your function should return a new character array, not modify the one that it is passed.

        Both encrypter and decrypter may receive invalid inputs. If the character array contains invalid characters (outside of the range defined above), or if the shift value is outside the range defined above (e.g., larger than MAX_SHIFT), you should return null.

        Your solution must match the expected output exactly, otherwise you will not receive credit.

        Complete the Javadoc comment for this function and write it.

        Parameters:
        line - array of characters to encrypt
        shift - amount to shift each character
        Returns:
        line encryped by rotating the specified amount
        See Also:
        ASCII Character Table
      • decrypter

        public static char[] decrypter(char[] line,
                                       int shift)
        Decrypt a single line of text using a rotate-N transformation.

        See comment for encrypter above.

        Parameters:
        line - array of characters to decrypt
        shift - amount to shift each character
        Returns:
        line decrypted by rotating the specified amount
        See Also:
        ASCII Character Table
      • main

        public static void main(java.lang.String[] unused)
        Solicits a single line of text from the user, encrypts it using a random shift, and then decrypts it.

        You are free to review this function, but should not modify it. Note that this function is not tested by the test suite, as it is purely to aid your own interactive testing.

        Parameters:
        unused - unused input arguments