THINK FIRST·CODE LATER

← All labs

A secret word: arithmetic on letters

Problem

The Caesar cipher replaces each letter by the letter shift places further in the alphabet, going back to A after Z. CaesarWord reads a word of exactly four capital letters and a shift from 0 to 25, and prints the position of the first letter in the alphabet (A is 0), the secret word, and the secret word decoded again.

One letter is encoded with

char c0 = (char) ('A' + (word.charAt(0) - 'A' + shift) % 26);

No loop yet: write the line four times. For the input

JAVA 3

the program prints

Position of J in the alphabet: 9
Secret word: MDYD
Decoded again: JAVA

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l5-8-data-1.txt

JAVA 3
What your program must print for l5-8-data-1.txt
Position of J in the alphabet: 9
Secret word: MDYD
Decoded again: JAVA

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l5-8-data-1.txt

Windows PowerShell does not understand <:

Get-Content l5-8-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.