Uploaded by Ace Cab

FRQ CaesarCipher Part B

advertisement
FRQ: CaesarCipher Part B
Due No Due Date
Points 3
Submitting an external tool
Instructions
This question involves working with the Caesar Cipher, which is used to encrypt Strings of letter
characters by “shifting” letter characters in the alphabet. This method of disguising
communications is named after Roman Emperor Julius Caesar, who was the first recorded
person to use it (although other ciphers which also substituted letters had been used previously
throughout the world). In this question, you will write two methods in the CaesarCipher class
which is designed to allow messages to be encoded and decoded using the Caesar Cipher.
Note: in this assignment, "alphabet" always refers to the Roman Alphabet with 26 letters
(ABCDEFGHIJKLMNOPQRSTUVWXYZ).
public class CaesarCipher
{
/** Returns an encoded version of msg by applying a Caesar
* Cipher with a shift specified by the parameter shift.
* Preconditions: msg contains only upper case letter
* characters, shift is greater than or equal to 0
*/
public static String encode(String msg, int shift)
{
/* to be implemented in part (a) */
}
/** Returns a String consisting of every possible Caesar Cipher
* encoded version of msg, beginning with the original (shift of 0),
* and sequentially increasing the shift until the String ends with
* msg encoded with a shift of 25. Each encoded message should
* be on a separate line.
* Precondition: msg contains only upper case letter characters
*/
public static String allEncodings(String msg)
{
/* to be implemented in part (b) */
}
/**
*
*
*
Returns the number representing the position of letter in the
alphabet, where A is at position 0, Z is at position 25
Precondition: letter is a String containing one uppercase
letter character only.
*/
private static int letterToNum(String letter)
{
/* implementation not shown */
}
/** Returns a String consisting of the single upper case letter which
* is at the position num in the alphabet, where A is at position 0,
* Z is at position 25
* Precondition: num is between 0 and 25 inclusive
*/
private static String numToLetter(int num)
{
/* implementation not shown */
}
}
(b) Write the
allEncodings
method which returns a String made up of each possible Caesar
shifted version of the parameter String
msg
.
The allEncodings method produces a multi-line string from the parameter String msg by
concatenating every possible Caesar Cipher shifted encoding of that String (useful for decoding
a message when you don’t know what shift has been used). This is done sequentially, beginning
with the original String (equivalent to a shift of 0) and ending with the String shifted by 25. For
example the call allEncodings("APCSA") should return the following String:
APCSA
BQDTB
CREUC
DSFVD
ETGWE
FUHXF
GVIYG
HWJZH
IXKAI
JYLBJ
KZMCK
LANDL
MBOEM
NCPFN
ODQGO
PERHP
QFSIQ
RGTJR
SHUKS
TIVLT
UJWMU
VKXNV
WLYOW
XMZPX
YNAQY
ZOBRZ
Complete the allEncodings method below. Assume that encode works as specified, regardless of
what you wrote in part (a). You must use the encode method appropriately to receive full credit.
/** Returns a String consisting of every possible Caesar Cipher
* encoded version of msg, beginning with the original (shift of 0),
*
*
and sequentially increasing the shift until the String ends with
msg encoded with a shift of 25. Each encoded message should
*
*
be on a separate line.
Precondition: msg contains only upper case letter characters
*/
public static String allEncodings(String msg)
To view a PDF version of the prompt, click here
(https://files.projectstem.org/CSA/CSA_Resources/FRQ/Methods/CaesarCipher.pdf) .
If you get stuck, you can reference the scoring rubric
(https://files.projectstem.org/CSA/CSA_Resources/FRQ/Methods/CaesarCipher_Rubric.pdf) to help
you debug your code.
Please note: The code for these FRQ activities has no main method, so it will not run in the Run
Code area of your programming environment. If you would like to run your code, you will need to
add a main method to your program. To score your code, you cannot have a main method in
your program.
CaesarCipher.java
STATUS
Files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
NOT SUBMITTED
SAVE
INSTRUCTIONS RUN CODE GRADING
public class CaesarCipher
{
/** Returns a String consisting of eve
* encoded version of msg, beginning
* and sequentially increasing the sh
* msg encoded with a shift of 25. Ea
* be on a separate line.
* Precondition: msg contains only up
*/
public static String allEncodings(Stri
{
/* Implement your answer to part (b)
}
/** Returns an encoded version of msg
* Cipher with a shift specified by t
* Preconditions: msg contains only u
* characters, shift is greater than
*/
public static String encode(String msg
{
/* COMPLETE WORKING METHOD PROVIDED
/* DO NOT MODIFY THE IMPLEMENTATION
return testing.FRQFunctions.encode(m
}
/** Returns the number representing th
* alphabet, where A is at position 0
* Precondition: letter is a String c
* letter character only.
*/
private static int letterToNum(String
{
/* COMPLETE WORKING METHOD PROVIDED
/* DO NOT MODIFY THE IMPLEMENTATION
char l = '?';
if(letter.length()==1) l = letter.ch
if(!Character.isUpperCase(l)) return
return (int) l - 65;
}
/** R t
St i
i ti
f th
∥
HISTORY
This question involves working wit
the Caesar Cipher, which is used to
encrypt Strings of letter characters
“shifting” letter characters in the
alphabet. This method of disguisin
communications is named after
Roman Emperor Julius Caesar, wh
was the first recorded person to us
(although other ciphers which also
substituted letters had been used
previously throughout the world). I
this question, you will write two
methods in the CaesarCipher cla
which is designed to allow messag
to be encoded and decoded using
Caesar Cipher.
Note: in this assignment, "alphabe
always refers to the Roman Alphab
with 26 letters
(ABCDEFGHIJKLMNOPQRSTUVW
).
public class CaesarCipher
{
/** Returns an encoded ver
* Cipher with a shift sp
Download