Students will build a Caesar Cipher program in Java that can encrypt and decrypt messages using a shift key. Students will practice string processing, loops, conditionals, methods, and input validation.
encrypt() method.decrypt() method.P_CaesarCipher_Lastname
Write a Java program that can:
1) Encrypt2) Decrypt3) QuitA–Z stays uppercasea–z stays lowercaseZ shift 1 → Az shift 1 → aencrypt() and decrypt()"HELLO" key 3 → KHOOR"abc" key 2 → cde"xyz" key 4 → bcd"Hello, World!" key 5 → Mjqqt, Btwqi!"KHOOR" key 3 → HELLO
START program
LOOP until user chooses Quit
DISPLAY menu: 1 Encrypt, 2 Decrypt, 3 Quit
GET userChoice
IF userChoice is 3
PRINT goodbye
EXIT loop
ENDIF
PROMPT for message
READ message (entire line)
PROMPT for key (integer)
READ key
NORMALIZE key:
key = key % 26
if key < 0 then key = key + 26
IF userChoice is 1
result = encrypt(message, key)
PRINT result
ELSE IF userChoice is 2
result = decrypt(message, key)
PRINT result
ELSE
PRINT invalid choice
ENDIF
END LOOP
END program
FUNCTION encrypt(text, key)
CREATE empty result stringbuilder
FOR each character ch in text
IF ch is uppercase letter
shift within 'A'..'Z' using wrap
append shifted character
ELSE IF ch is lowercase letter
shift within 'a'..'z' using wrap
append shifted character
ELSE
append ch unchanged
ENDIF
END FOR
RETURN result string
END FUNCTION
FUNCTION decrypt(text, key)
// decrypt is encrypt with (26 - key)
RETURN encrypt(text, 26 - key)
END FUNCTION
| Category | What You’re Looking For | Points |
|---|---|---|
| A. Program Functionality |
Encrypt works correctly (20) Decrypt works correctly (20) Non-letters preserved (10) |
50 |
| B. Required Concepts |
Uses methods: encrypt/decrypt (10) Uses loop for character processing (10) Uses selection for upper/lower/non-letter (5) |
25 |
| C. User Experience + Validation |
Clear prompts/menu/output (10) Handles invalid menu choice + invalid key (5) |
15 |
| D. Style + Documentation |
Good naming/indentation/comments (5) Includes name/class period header comment (5) |
10 |
| Total | 100 | |
(ch - 'A' + key) % 26