Help with a java project

Hello, me and my classmates are really stumped with our current project. We now we want to use loops instead of an array but we are having trouble setting up the loop in the main file and the tester. Can anyone help? I'll post the instructions below. 

 INSTRUCTIONS

Several years after mankind has developed letters to convey information to each other, people already started to think about ways to hide this information from those who shouldn't have it. Encryption was born. An early method was easy to conduct manually: Each letter of the alphabet is shifted to the right by a certain number of places. So with 12 shifts the letter A becomes the letter M, B becomes N, etc. The letter N becomes Z and O would then become A again. Here is the method for the shift value of 12:

Original: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Code: MNOPQRSTUVWXYZABCDEFGHIJKL

Each letter in the first line is then replaced by the corresponding letter in the second line. So the word "ENCRYPTION" could be shifted by 12 giving the encrypted word "QZODKBFUAZ". Interestingly enough, if you then shift "QZODKBFUAZ" by 14, you'll get... You can figure it out, I'm sure!

Yep! Shifting by a value of n encrypts a message which then can be decrypted by shifting it by 26 - n. Obviously this method only works with uppercase letters and shifts between 1 and 25 including – shifting by 0 or 26 is not (!) an effective encryption method!

And how do I break the encryptions?

Assume we want to decrypt "YFIXIDLKBK". You run the code "YFIXIDLKBK" through all values between 1 and 25. One of the 25 results will make sense. These kinds of attacks against an encryption method are called "brute force" attacks, because they do not attempt to use any sophisticated statistical methods, they just try all possible values and rely on the human brain to figure out the right one.

Project Requirements:

Write a class StringShifter with one instance field text of type String. It has one constructor public StringShifter(String initialText) (as usual!). It also has a method public String shift(int n) which returns the text shifted right by n positions. If n is not between 1 and 25, the original text is returned.
Write a little tester that quickly tests the example from the background information, especially making sure that the encryption and decryption works as expected!
Write a class CodeBreaker with only a main method that generates all 25 solutions by using a loop – not by manually calling the methods 25 times.
Try to decrypt "KZINVXJGV". Run your program. The program must have 25 lines of words. Take the screenshot and submit.

You should share the code you've got so far.

Hint: Use pastebin, this forum's code tags are horribly broken.

 

Also, you might get in trouble if you copy-paste this instead of learning from it:

StringShifter.java

TestCase.java

CodeBreaker.java

 

I'll take a look at what you linked ty. I usually don't copy and paste. I use the code for reference and try and understand where I went wrong. I would have posted my code but the file is currently on the school computer so I don't have access to it atm. Thanks again. 

Could create a numerical range and refer to the ASCII values of the letters. If you wanted to do that.

If you intend to just use an array of characters, rather than actually moving the elements of the array itself, you can just change the index value to point to the letter that it ought to be. so moving over 12 would simply by something more like 'Alphabet [index+12]' or something. I guess if you looked at it a bit more vaguely, it would look like 'Alphabet [index + offset - correction]'. Correction would be set if index + offset is greater than the length of the alphabet. This would just subtract the length of the entire array.

Basically, just don't think about actually moving the array elements themselves. You could do this, but it would be extremely annoying. Just alter the index value so that it points to the section of the array that you want.

If you do absolutely HAVE to move everything, you can use the same technique, create a new array, and use a loop to insert each value into the array based on the offset index value you got. What I would probably do is have a fixed array that is always the default alphabet (starting at A, ending at Z), then I would have a temp array and a current array. The current array would be the current arrangement of letter. If there is a point that you would like to shift the letters from the current array (rather than the default one), then you would move that to the temp array, set your offset, and use that to put the new values into the current array.

But like I said before, if you can do it, just use the offset.

 

Oh, and for reference, if you need to have a method of correcting for going over the entire alphabet twice, you can just divide and floor the value. basically, if (offset > array_max) { correction = array_max } would work in most scenarios, but it would break if you went over the array_max again. So instead, you could do something like this: 'correction = floor(offset / array_max) * array_max'

basically, offset / array_max would get the number of times you've gone past the end of the array. Flooring the value makes sure it doesn't break whenever you're offset is half of your array_max. You'll only get 0, then 1, then 2, etc, rather than .1, .2, .3, ... 1, 1.1, 1.2, ... 2 and so on. That type of thing would break the program. Multiplying the number by array_max makes sure that it goes back to the beginning of the array each time that you reach the end.

 

Keep in mind, all of this is pseudocode. Oh, and I overcomplicated it a bit, so sorry for that. But anyways, these are just a few ideas. I didn't look at the other post yet, but if his way is easier, feel free to go with that. I'm just posting the first thing that comes to mind as I think about the project.

 

If you need me to elaborate on anything, feel free to ask. I probably could have commented a bit more about WHY things work the way I did them.

As you may very well already be aware, Strings in Java are composed of an array of chars, which in turn store individual characters in UTF-16 encoding. This means each character ('a', 'b', etc.) is stored as a 16 bit number. The mapping between characters to numbers can be referenced HERE.

In my sample code I transform each character by casting to int and shifting the value so that 'A' or 'a' (my code is case insensitive and ignores non-alphabetical characters) would have the value 0 ('a' - 'a' = 0). I do this so that later it is easy to wrap shifts which extend past the end of the alphabet back around to the beginning. Next I add the shift amount ('a' + 1 = 'b'), use modulus to automatically wrap shifts past z (('z' + 1) % 26 = 'a'; ('p' + 2) % 26 = 'r'), then add 'a' back to the value so that 'a' = 'a' again (0 + 'a' = 'a') and finally I cast the result back to a char.

All that is done in one line of code:

return (char)(((int)c + n - (int)base) % 26 + (int)base);

c is the character from the string, n is the number of letters to shift, and base is 'a' or 'A' depending on the case of the character c. The step by step process is mercifully condensed thanks to the order of operations.