The goal of this lab is to write a class CharacterShift that reads lines from a specified file and writes the lines to a specified file while making a specified number of cyclic shift on each numeral, on each lower-case letter, and on each upper-case letter.
At the start, the program receives from the user:
During the process of receiving these pieces of information, the program performs the following tests:
Once these tests have passed, the program opens the input file using a new Scanner object (with the input File object in the above as the parameter) and opens the output file using a new PrintStream object (with the output File object in the above as the parameter).
The program then performs the conversion. The conversion goes as follows:
The shifting will be performed in the following cyclic manner:
Let diff be the shift amount that the user has specified at the beginning of the program. The value of diff is a positive integer.
Given a String inLine the conversion goes as follows:
LowerCase Letter
The lower-case letters ‘a’ - ‘z’ can be represented
using the numbers 0 - 25 by considering their distance from the first
letter ‘a’. In other words, we identify 0 as ‘a’
1 as ‘b’, and so on.
Given a lower-case letter, say myChar, this representative number can
be obtained by expression
myChar - ‘a’'
By shifting a lower-case letter c by 1 we mean switching from the lower-case letter c to the next letter in the lower-case leter group, e.g., from ‘b’ to ‘c&squo; and from ‘x’ to ‘y’. Since ‘z’ is the last letter of the alphabet, we circle back to the first letter of the alphabet and treat ‘a’ as the next letter of ‘’.
Now, given a positive integer k, by
shifting myChar by k
we mean to perform this one-letter shift to myChar successively k times. It is easy to observe that, since there are 26 letters in the lower-case letter group, 26 shifts take each letter back to the original. Thus, given any positive integer k, shifting myChar by k is equivalent to:
shifting myChar by (k % 26).
Furthermore, since myChar is the result of shifting ‘a’ by (myChar - ‘a’) we have that shifting myChar by k is equivalent to:
shifting ‘a’ myChar by ( mychar - ‘a’ + k ) % 26
Given a char myChar, which is a lower-case alphabet, we can obtain the letter as the result of performing shifting myChar by k by the expression
(char)( ‘a’ + ( myChar - ‘a’ + k ) % 26 )
Our goal is to each occurrence of a lower-case letter in the lines of the input file using this formula.
Upper-case Letter
The treatment of an upper-case letter is much
the same as the lower-case letter, expected that
‘A’ replaces the ‘a’ in
all the expression, that is, we use the expression
(char)( ‘A’ + ( myChar - ‘A’ + k ) % 26 )
You can write a public static method by the name of convert that takes two parameters, a char by the name of myChar and an int by the name of k, and returns the char as defined in the above, in the case where myChar is a lower-case letter (the case can be tested using the method call Character.isLowerCase( myChar )) or the case where myChar is an upper-case letter (the case can be tested using the method call Character.isUpperCase( myChar )), and returns myChar, otherwise.
Now given a line on which to make the conversion on all the alphabet letters, we can think of creating a new String as follows:
You may use the template CharacterShift.java.
Below is an example of executing the code that satisifed the requirement. In the example, the first three runs fail Test 1, Test 2, and Test 3 in this order by supplying the name of a non-existing file as the input file, by supplying the same name for the input and the output, and by supplying a negative number for the shift amount, respectively. The fourth time the program completes. The contents are shown using cat command.