aspose file tools*
The moose likes Beginning Java and the fly likes Arithmetic with char types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arithmetic with char types" Watch "Arithmetic with char types" New topic
Author

Arithmetic with char types

tom jenkins
Greenhorn

Joined: Apr 18, 2012
Posts: 27
Hello Folks,

I'm currently working on a project that involves programming a Caesar ciphering program.

Currently I'm supposed to develop a way to wrap around the alphabet to meet the requirements of the encoding method for the Caesar shift.

This was given to me:

char ch = word.charAt(i);
ch = (char)(‘a’ + ch – ‘a’ + 3) % 26);

I understand that this arithmetic is cast as a char type, but I'm not sure as how the whole a+ch+3 ..ect.. bit works. I know that char types have numeric values and can be used in arithmetic, but I'm not really so clear as to how this works

can someone please help me understand the meaning and use of char types in this situation?

Thank you
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

that doesn't make a lot of sense to me.

the 'a' - 'a' cancels itself out. effectively I believe you have this:

ch + 3 % 26

Which shifts each letter three places up, so 'a' becomes 'd', 'b' becomes 'e', etc. But what happens to 'x','y', and 'z'? If you add three to their ascii values, you get '{', '|', and '}'.

You need to somehow roll those back down to the values for 'a', 'b', and 'c'. But using the mod operator won't do that...it would give you some value between 0 and 25, which are NOT the ascii values for the alphabet at ALL.

Who gave you this code? I think they were crazy.

OR

I need more coffee.

or both, i guess.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

tom jenkins wrote:This was given to me:
char ch = word.charAt(i);
ch = (char)(‘a’ + ch – ‘a’ + 3) % 26);
I understand that this arithmetic is cast as a char type, but I'm not sure as how the whole a+ch+3 ..ect.. bit works...

Me neither, because
(a) it won't compile.
(b) if it's only supposed to rotate alphabetic letters (and specifically, lower-case ones), it's wrong.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
tom jenkins
Greenhorn

Joined: Apr 18, 2012
Posts: 27
Well this assignment is from my professor, I guess he's not very clear at times.. Here is some more.. but I guess this was to serve as some sort of example.

Abstract class Cipher

The class Caesar inherits the abstract class Cipher. This class defines the methods code and decode. The method encode takes a String parameter and returns a String result. It takes each character of the parameter and performs a Caesar shift on the character. That is, a shift with possible wrap around can be coded as follows:

char ch = word.charAt(i);
ch = (char)(‘a’ + ch – ‘a’ + 3) % 26);



not sure what the modulus is for really.. can anyone point me to a more clearer strategy to take on wrapping around the alphabet and making the encoding method?

Thanks
tom jenkins
Greenhorn

Joined: Apr 18, 2012
Posts: 27
this is the method that was given to me for encoding



I'm just looking for someone to explain how this works
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4491
    
    8

tom jenkins wrote:

Those brackets make a world of difference, because the % has higher precedence than the +.

Roughly speaking, that line translates as
- shift the character so it's a number between 0 and 25 (inclusive)*
- shift it by a fixed amount (the cipher)
- take the remainder when you divide by 26. That makes sure you've still got a value between 0 and 25. The effect of this is for your shift to wrap round.
- Convert it back to a letter.

* assuming it's a lower-case letter. This approach won't work for anything except a-z.

Try it out with some representative values:

ch = 'x'
ch - 'a' = ?
ch - 'a' + 5 = ?
(ch - 'a' + 5) % 26 = ?
'a' + (ch - 'a' + 5) % 26 = ?


Does that help you see how it works?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

ch - 'a' takes the ASCII value of ch and subtracts the ASCII value of 'a'. If ch is a lowercase letter then this will return a value between 0 and 26 (exclusive). The shift is added, then the mod operator turns it back into a value between 0 and 26 (assuming that Constants.WRAP_AROUND is 26). You add that back to 'a' to again get a lowercase letter.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
tom jenkins
Greenhorn

Joined: Apr 18, 2012
Posts: 27
thanks folks, i got it a little clearer now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arithmetic with char types