” 写作COMP122编程、java程序调试COMP122 Assessment 1 Due 2021-03-05 at 5pmThe Caesar CipherThe Caesar cipher is an ancient Method of encrypting text, i.e. to transform text into a format that isunreadable for anyone without a secret key. It is believed that Julius Caesar actually used such a cipherfor his correspondence. Unfortunately for him, This type of cipher is easily broken using a frequencyanalysis method outlined below. Your assignment is to implement this in Java.Part 1: Encrypting and DecryptingThe Caesar cipher is a rotation cipher and operates by translating each letter into the one that isshied along the alphabet by a fixed distance. This distance is called the shi. It is the same for allletters in the alphabet and therefore can be seen as the secret key to encrypt and decrypt: To encryptyour text using a given shi, you translate letters by that many places later in the alphabet. A Caesarcipher with shi 3 can be illustrated as follows.For example, if your text to Encrypt is Meet me at midnight under the bridge and your shi is 3, theencrypted text is Phhw ph dw plgqljkw xqghu wkh eulgjh, as the letter b gets translated into an e,and e gets translated into h and so on. We wrap around at the end of the alphabet, so that zgets changed to a c given a shi of 3. We can interpret a negative value for the shi as translatingletters backwards (e.g. an f gets encrypted as the letter b if the shi is 4).RequirementsIn a file called Caesar.java, implement the following (public static) methods. a method called rotate that rotates a single character. It should have two arguments: an integer(int) shi and a char to rotate, and return the character rotated by the given shi, as a char.Lower-case characters should be translated into lower-case characters, capitalised ones intocapitalised ones, and all other characters should remain untouched.1COMP122 Assessment 1 Due 2021-03-05 at 5pm another method called rotate that rotates a whole string. It should have two arguments: aninteger (int) shi and a String to rotate and return the string rotated by the given shi, as aString. a main method, that allows to encode/decode text, as follows. This should interpret the first twoThe first argument is interpreted as an integer shi and the second one as a (string) message tobe rotated. The only output printed by the program should be the rotated string output.Moreover, the main method should check if it was called with exactly two arguments and complainotherwise. See below for example outputs. Your program needs to print the exact sameoutput to be considered correct.$ java Caesar 3 The ships hung in the sky in much the same way that bricks dont.Wkh vklsv kxqj lq wkh vnb lq pxfk wkh vdph zdb wkdw eulfnv grqw.$ java Caesar -13 The ships hung in the sky in much the same way that bricks dont.Gur fuvcf uhat va gur fxl va zhpu gur fnzr jnl gung oevpxf qbag$ java Caesar 13 The ships hung in the Sky in much the same way that bricks dont.Too many parameters!Usage: java Caesar n cipher text$ java Caesar 13Too few parameters!Usage: java Caesar n cipher textPart 2: Cracking the Caesar cipher.Suppose we are given a cipher text, i.e. text that has already been encrypted with some (unknown)shi, and we want to determine the original unencrypted text (typically referred to as the plaintext).To reconstruct the original text we could decode with each of the 26 possible shis, and take the resultthat looks closest to an English sentence.How do we measure closeness? This is where letter frequencies and a small statistical trick comes in.First of all, we know how oen each letter occurs on average in English text. For instance, e is themost common letter, then t, and so on. To decode our cipher text, we can compute the frequenciesof each letter as they appear in the text. To measure how close these are to the known English letterfrequencies, we use the 2-score (that is the Greek letter chi, so its the chi-squared score). Thisscore is defined as2 =Xz=a(freq English)2Englishwhere freq denotes the frequency of a letter (the number of occurrences divided by the total number2COMP122 Assessment 1 Due 2021-03-05 at 5pmof letters in the text), and Englishis the corresponding English letter frequency. In other words, wesum the fraction over all 26 possible letters to determine this score, which is a single number.The 2score will be lower when the frequencies are closer to English. Note that when we do this, weare ignoring the case of letters (we Want to treat upper and lower case equally for our purposes).You are provided with a file called Brutus.java, which already defines letter frequencies in Englishtexts. This is given as array of doubles, in alphabetical order:1 public static final double[] english = {2 0.0855, 0.0160, 0.0316, 0.0387, 0.1210, 0.0218, 0.0209, 0.0496, 0.0733,3 0.0022, 0.0081, 0.0421, 0.0253, 0.0717, 0.0747, 0.0207, 0.0010, 0.0633,4 0.0673, 0.0894, 0.0268, 0.0106, 0.0183, 0.0019, 0.0172, 0.00115 };Accordingly, the frequency of the letter a, the probability that a randomly chosen letter in an Englishtext is an a, is Englisha = 0.0855 and can be accessed as english[0]. Similarly, Englishb = 0.0160and so on.RequirementsIn Brutus.java, write a method called count that takes a single String parameter and returns a length-26 integerarray whose values reflect how oen each character occurred. You should not make a dierencebetween upper and lower case letters and the returned array should be in alphabetical order.This way, if letterCounts is an array resulting from your method then letterCounts[25] isthe number of times the letter z or Z occurs. a method called frequency that takes a single String and returns a length-26 array of doubles whose values correspond, in alphabetical order, to the frequency of the letter. This way, ifletterFreq is an array resulting from this method then letterFreq[24] is the number oftimes the letter x or X occurs, divided by the length of the string input. a method called chiSquared, which returns the 2-score (a double) for two given sets offrequencies. That is, it should take two parameters, both of type double[], and return a singledouble value that tells us how close these two arrays are. You may assume that the two inputsare always of length 26. a main method that can be used to decipher Caesar-encoded English cryptotext without thekey. Of course, you should be using your chiSquared method as well as the given English letterfrequencies.3COMP122 Assessment 1 Due 2021-03-05 at 5pmThe string that is to be deciphered should be read from the first command line argument andyour program should ensure that it gets exactly this one argument and complain otherwise.Sample output below.$ java Brutus Vg vf n zvfgnxr Gb guvax lbh pna fbyir nal znwbe ceboyrzf whfg jvgucbgngbrf.It is a mistake to think you can solve any major problems just with potatoes.$ java BrutusToo few parameters!Usage: java Brutus cipher text$ java Brutus Too Many ParametersToo many parameters!Usage: java Brutus cipher textHints and Comments1. In Java, char and int variables are (more or less) interchangeable. A Java statement like1 int diff = e – b;is perfectly legal, i.e. Java can interpret the dierence of two letters with no problem, andthis will give an integer value. If the two letters are of the same case, then this will give a valuebetween 25 and 25. In particular, if ch is a lower case (char) letter, then1 int diff = ch – a;tells you how many places aer a that letter is in the alphabet. (The value of diff will bebetween 0 and 25 inclusive).2. When translating letters, notice that if ch-a is between 0 and 25 (inclusive), then ch is a lowercase letter, and we encrypt as above. Alternatively, if ch-A is between 0 and 25 (inclusive), weencrypt ch similarly to get a new upper case letter. You may also use helper methods isLetter,isLowerCase etc from the Character class.3. In order to translate whole strings, recall that you can access the individual characters in a stringusing the charAt method: if str is a String then str.charAt(i) gives you the char at indexi in str.4. When counting letter frequencies remember that for this exercise, we consider upper and lowercase to be the same and do not consider spaces and punctuation characters. For example, in thestring Mississippi Moon!, the frequency of the letter m is 2/15, while the frequency ofthe letter s is 4/15.5. Make sure you that your code is readable and appropriately documented. There will be points forjavadoc comments that explain what each method does and how its parameters are interpreted.4COMP122 Assessment 1 Due 2021-03-05 at 5pm6. You can run a (partial) automarker to check that your submission is in the correct format.$ check50 liv-ac-uk/comp122/2021/problems/caesarSubmissionSubmit you solution using submit50 just like the lab exercises.$ submit50 liv-ac-uk/comp122/2021/problems/caesarYou can submit multiple times and only the latest version (and its submission time) will be consideredfor grading.Submissions are subject to UoLs Code of Practice on Assessment and the usual late penalties (-5% perfor each 24 hour period immediately following the deadline) apply. If you require an extension due toextenuating circumstances please get in touch with the CS student oice (csstudy@liv.ac.uk) beforethe submission deadline. We will Not grant any extensions aerwards.如有需要,请加QQ:99515681 或WX:codehelp
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。