Essay on M150 TMA5 JAVA PROGRAMING

Submitted By dusancermak
Words: 870
Pages: 4

Tutor marked assignment 5 (M150)
By Dusan Cermak (dc8352)
Question 1

Task 1.
1. Decoding simple substitution ciphers using frequency analysis is based on the fact, that some letters, letter pairs and repeat letters are more frequent then others and some are rare, this pattern is translated to the cipher. Letters E, T, A and O are the most common in English text as well as pairs TH, ER, ON, and AN and doubles SS, EE , TT , and FF. If the text is long enough this information can be used to assign letters of alphabet to the letters of code, according to there fervency, repeating and length of words.

2. The code for the first word “eve” is “Bsjs”, where “B” is signal character. The second word ”ever” is coded “Crire” and “eavesdrop” is coded “Dqmhqepdabe”. The coded text will be “BsjsCrireDqmhqepdabe”

3. Decoded message is “ALBERTI THWARTS EVE” .

Task 2.

Structured English:
Initialise new Array with as many elements as original array. No values are assigned.
For every element in the original array
If position in array is 0 first element of the new array is the last element of the original array.
Else the shifted element is assigned a value of cipher element on (position -1)
Assign shifted alphabet to cipher alphabet
Return new array.

Function code: function right(letterArray)
{
// Initialise new Array with as many elements as original array. No values are assigned var shiftedArray = new Array(letterArray.length);
// for every element in the original array for (var x=0; x<letterArray.length; x=x+1) { if (x==0) // If position in array is 0 first element of the new array is the last element of the original array. shiftedArray[x] = cipherArray[letterArray.length-1]; else //Else the shifted element is assigned a value of cipher element on position (position -1) shiftedArray[x] = cipherArray[x-1]; } for (var y=0; y< letterArray.length; y=y+1) cipherArray[y] = shiftedArray[y]; // assign shifted alphabet to cipher alphabet return cipherArray; // return new array.

}

Function testTask02(): function testTask02()
{
return window.alert (right(cipherArray));
}

Task 3.
Structured English:
Declare signal character position index
For every element in cipher Alphabet
If signal character is equal to plain alphabet assign position to index.
Repeat as long as cipher alphabet element is different from index character
Use function right shift elements one position to the right
Returns aligned cipher alphabet

Function : function rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
// declare signal character position index var index;
//repeat for every element in cipher alphabet for (var x=0; x<cipherAlphabet.length; x=x+1)
{
if (plainArray[x] == signalCharacter) // if signal character is equal to plain alphabet assign position to index. index = x;
}
while (cipherArray[index] != indexCharacter) // repeat as long as cipher alphabet element is different from index character right(cipherArray); // Use function right shift elements one position to the right return cipherArray; // returns aligned cipher alphabet
}

function testTask03()
{
return window.alert (rotateToPosition('A','g', plainArray, cipherArray));
}

Task 4.
Structured English:
Aligns the plain and cipher alphabets according to index and signal character
For every letter of plain text
For every character of plain alphabet
If plain alphabet character is same as plain text character assign cipher alphabet character with same index to the string
Return string

Function code: function encrypt(plainText, signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
rotateToPosition('X','n', plainArray, cipherArray); // aligns the plain and cipher alphabets according to index and signal character var coded = signalCharacter; for (var x=0; x<= plainText.length; x=x+1) // for every letter of plain text { for ( var y=0; y<plainAlphabet.length; y=y+1)// for every character of plain alphabet