Question

In: Computer Science

- Students must develop the code required to create a square two-dimensional array just large enough...

- Students must develop the code required to create a square two-dimensional array just large enough to store a given string in encrypted form using the given encryption algorithm. The character used after the end of the encrypted string is the ASCII delete key, set as final UNPRINTABLE_CHAR_VALUE. After this value is stored, the encryption process fills the remaining elements with random values

- Students must develop the code to decrypt a given square two-dimensional array and using the given encryption algorithm

- Students must develop other supporting methods as specified (below) to manage the encryption operations*

- Students will also document all classes and methods using the Javadoc commenting process; comments are not required to be exactly the same as found in the supporting document (below) but they should be semantically equivalent

- Students will upload the EncryptionClass.java file to this assignment; any other uploaded files will result in a reduction of the project grade, and in some cases, may cause a complete loss of credit

- Assignment Specification (Javadoc output for the project):

p1_package

Class EncryptionClass

  • java.lang.Object
    • p1_package.EncryptionClass

FileIoToolsForEncryptionClass.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class EncryptionClass
   {/**
     * Constant for maximum input char limit
     */
    private final int MAX_INPUT_CHARS = 256;
  
    /**
     * Constant for unprintable char value used as message end char
     */
    private final int UNPRINTABLE_CHAR_VALUE = 127; // ASCII for delete key

    /**
     * Constant for minus sign used in getAnInt
     */
    private final char MINUS_SIGN = '-';
  
    /**
     * Class Global FileReader variable so methods can be used
     */
    private FileReader fileIn;
  
    /**
     * Downloads encrypted data to file
     * <p>
     * Note: No action taken if array is empty
     * @param fileName String object holding file name to use
     */
    void downloadData( String fileName )
       {FileWriter toFile;     
        int rowIndex, colIndex;     
        if( arrSide > 0 )
           {
            try
               {
                toFile = new FileWriter( fileName );
                toFile.write( "" + arrSide + "\r\n" );
                for( rowIndex = 0; rowIndex < arrSide; rowIndex++ )
                   {
                    for( colIndex = 0; colIndex < arrSide; colIndex++ )
                       {
                        if( encryptedArr[ rowIndex ][ colIndex ] < 100 )
                           {
                            toFile.write( "0" );
                           }              
                        toFile.write(" "+ encryptedArr[ rowIndex ][ colIndex ] + " " );
                       }
                    toFile.write( "\r\n" );
                   }
         
                toFile.flush();
                toFile.close();
               }
            catch( IOException ioe )
               {
                ioe.printStackTrace();
               }
           }
       }

    /**
     * gets an integer from the input stream
     *
     * @param maxLength maximum length of characters
     * input to capture the integer
     *
     * @return integer captured from file
     */
    private int getAnInt( int maxLength )
       {
        int inCharInt;
        int index = 0;
        String strBuffer = "";
        int intValue;
        boolean negativeFlag = false;

        try
           {
            inCharInt = fileIn.read();

            // clear space up to number
            while( index < maxLength && !charInString( (char)inCharInt,
                                                           "0123456789+-" ) )
               {
                inCharInt = fileIn.read();              
                index++;
               }    
            if( inCharInt == MINUS_SIGN )
               {
                negativeFlag = true;
                inCharInt = fileIn.read();
               }

            while( charInString( (char)inCharInt, "0123456789" ) )
               {
                strBuffer += (char)( inCharInt );

                index++;
             
                inCharInt = fileIn.read();
               }          
           }
     
        catch( IOException ioe )
           {
            System.out.println( "INPUT ERROR: Failure to capture character" );
            strBuffer = "";
           }
        
        intValue = Integer.parseInt( strBuffer );
     
        if( negativeFlag )
           {
            intValue *= -1;
           }
     
        return intValue;
       }

    /**
     * Uploads data from file holding a square array
     *
     * @param fileName String object holding file name
     */
    void uploadData( String fileName )
       {
        int rowIndex, colIndex;
    
        try
           {
            // Open FileReader
            fileIn = new FileReader( fileName );
      
            // get side length
            arrSide = getAnInt( MAX_INPUT_CHARS );         
    
            encryptedArr = new int[ arrSide ][ arrSide ];
          
            for( rowIndex = 0; rowIndex < arrSide; rowIndex++ )
               {
                for( colIndex = 0; colIndex < arrSide; colIndex++ )
                   {
                    encryptedArr[ rowIndex ][ colIndex ]
                                                  = getAnInt( MAX_INPUT_CHARS );
                   }
               }
          
            fileIn.close();
           }
    
        // for opening file
        catch( FileNotFoundException fnfe )
           {
            fnfe.printStackTrace();
           }
      
        // for closing file
        catch (IOException ioe)
           {
            ioe.printStackTrace();
           }
       }   
   }  

Solutions

Expert Solution

p1_package

Class EncryptionClass

  • java.lang.Object
    • p1_package.EncryptionClass

FileIoToolsForEncryptionClass.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class EncryptionClass
   {/**
     * Constant for maximum input char limit
     */
    private final int MAX_INPUT_CHARS = 256;
  
    /**
     * Constant for unprintable char value used as message end char
     */
    private final int UNPRINTABLE_CHAR_VALUE = 127; // ASCII for delete key

    /**
     * Constant for minus sign used in getAnInt
     */
    private final char MINUS_SIGN = '-';
  
    /**
     * Class Global FileReader variable so methods can be used
     */
    private FileReader fileIn;
  
    /**
     * Downloads encrypted data to file
     * <p>
     * Note: No action taken if array is empty
     * @param fileName String object holding file name to use
     */
    void downloadData( String fileName )
       {FileWriter toFile;     
        int rowIndex, colIndex;     
        if( arrSide > 0 )
           {
            try
               {
                toFile = new FileWriter( fileName );
                toFile.write( "" + arrSide + "\r\n" );
                for( rowIndex = 0; rowIndex < arrSide; rowIndex++ )
                   {
                    for( colIndex = 0; colIndex < arrSide; colIndex++ )
                       {
                        if( encryptedArr[ rowIndex ][ colIndex ] < 100 )
                           {
                            toFile.write( "0" );
                           }              
                        toFile.write(" "+ encryptedArr[ rowIndex ][ colIndex ] + " " );
                       }
                    toFile.write( "\r\n" );
                   }
         
                toFile.flush();
                toFile.close();
               }
            catch( IOException ioe )
               {
                ioe.printStackTrace();
               }
           }
       }

    /**
     * gets an integer from the input stream
     *
     * @param maxLength maximum length of characters
     * input to capture the integer
     *
     * @return integer captured from file
     */
    private int getAnInt( int maxLength )
       {
        int inCharInt;
        int index = 0;
        String strBuffer = "";
        int intValue;
        boolean negativeFlag = false;

        try
           {
            inCharInt = fileIn.read();

            // clear space up to number
            while( index < maxLength && !charInString( (char)inCharInt,
                                                           "0123456789+-" ) )
               {
                inCharInt = fileIn.read();              
                index++;
               }    
            if( inCharInt == MINUS_SIGN )
               {
                negativeFlag = true;
                inCharInt = fileIn.read();
               }

            while( charInString( (char)inCharInt, "0123456789" ) )
               {
                strBuffer += (char)( inCharInt );

                index++;
             
                inCharInt = fileIn.read();
               }          
           }
     
        catch( IOException ioe )
           {
            System.out.println( "INPUT ERROR: Failure to capture character" );
            strBuffer = "";
           }
        
        intValue = Integer.parseInt( strBuffer );
     
        if( negativeFlag )
           {
            intValue *= -1;
           }
     
        return intValue;
       }

    /**
     * Uploads data from file holding a square array
     *
     * @param fileName String object holding file name
     */
    void uploadData( String fileName )
       {
        int rowIndex, colIndex;
    
        try
           {
            // Open FileReader
            fileIn = new FileReader( fileName );
      
            // get side length
            arrSide = getAnInt( MAX_INPUT_CHARS );         
    
            encryptedArr = new int[ arrSide ][ arrSide ];
          
            for( rowIndex = 0; rowIndex < arrSide; rowIndex++ )
               {
                for( colIndex = 0; colIndex < arrSide; colIndex++ )
                   {
                    encryptedArr[ rowIndex ][ colIndex ]
                                                  = getAnInt( MAX_INPUT_CHARS );
                   }
               }
          
            fileIn.close();
           }
    
        // for opening file
        catch( FileNotFoundException fnfe )
           {
            fnfe.printStackTrace();
           }
      
        // for closing file
        catch (IOException ioe)
           {
            ioe.printStackTrace();
           }
       }   
   }  


Related Solutions

1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
You will put a possible magic square into a two dimensional array and determine if it...
You will put a possible magic square into a two dimensional array and determine if it is a magic square or not. Some of the code is completed. You can assume that it is a square array and that all of the integers are unique (no repeats) CODE: package TwoDimensionalArrays; public class MagicSquare {    public static boolean checkMagicSquare(int[][]array)    {   //Pre: Assume that the array is a square two dimensional array        //Pre: Assume that there are no...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table! struct mult_div_values { int mult; float div; }; The program needs to read the number of rows and columns from the user as command line arguments. You...
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names intArray17.b. Create a loop to calculate the sum of every element in the first column.c. Create a loop to calculate the sum of every element in the array.
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names...
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names intArray17. Create a loop to calculate the sum of every element in the first column. Create a loop to calculate the sum of every element in the array.
Create a game of Connect Four using a two dimensional array (that has 6 rows and...
Create a game of Connect Four using a two dimensional array (that has 6 rows and 7 columns) as a class variable. It should be user friendly and be a two person game. It should have clear directions and be visually appealing. The code MUST have a minimum of 3 methods (other than the main method). The player should be able to choose to play again. **Code must be written in Java**
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT