-
3 questions
Sorry I put so many questions in my previous post "Thinking in Java book questions" , I realized it was too much to ask the forum to answer all of them. So I Googled them, and solved most of my questions myself, but I am still lost on 3. Please if anyone can help it would be really appreciated. Thanks.
question 1
page 137 of Thinking in Java
why would a method have both "print(result + ", ");" and "return result;" in the same method right next to each other, don't I just need "return result"?
question 2 (page 138)
You can do math on characters?
Code:
for(char c = 0; c < 128; c++)
question 3 (page 152)
I don't know what the 'a' is for in the following code.
Code:
int c = rand.nextInt(26) + 'a'
(I don't understand the book's explanation.)
-
It's hard to answer without having read the book myself, but I'll take a shot at it.
A) One prints to the console, the other returns the value to use in the method that called it.
B) Yes. char is a 16-bit value that is used to describe characters. For instance, the character "a" has a value of 97.
C) My guess is that that code randomly selects a character. As described above, all characters have a numerical representation, and there are 26 characters in the English language. What the code says is "Pick a random number between 0 and 26, and pick the character that's that many steps from the character "a".
-
Oh, and if you want to see which number corresponds to which character, have a look at Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion which gives you a number in decimal, hexadecimal and octal.
-
i have only the html-version of this book. can you please tell which edition you mean and in which chapter i can find the pages you are referring?
-
Toll that was AWESOME. Thank you so much!! I wish I could just read programming books without questions, but every author puts in code that he doesn't first explain. Thank you!
-
That's ok j2me64 Toll answered it. Thank you for responding though. Derek
-
Glad I could help! Remember to mark it as solved; you can do that under "Thread tools" at the top.
-
Thanks. I tried to mark it as solved, but I got the following error.
Code:
[B]Content Encoding Error[/B]
The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.
Please contact the website owners to inform them of this problem.
-
Yeah, it does that. It's marked as solved despite that error!
-
-
It may be worth knowing that ASCII in Java is a subset of the UNICODE standard, and while the ASCII alphabet encoding is sequential and contiguous, that is not necessarily the case with other UNICODE alphabet encodings, so calculating letter offsets that way is not best practice for multi-lingual applications.
-
Ok thanks for that info dlorde.:)