Results 1 to 12 of 12

Thread: 3 questions

  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default 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?

    Java 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.

    Java Code:
    int c = rand.nextInt(26) + 'a'
    (I don't understand the book's explanation.)
    Last edited by silverglade; 05-09-2011 at 01:16 PM.

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    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".

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    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.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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?

  5. #5
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    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!

  6. #6
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    That's ok j2me64 Toll answered it. Thank you for responding though. Derek

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Glad I could help! Remember to mark it as solved; you can do that under "Thread tools" at the top.

  8. #8
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Thanks. I tried to mark it as solved, but I got the following error.


    Java 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.

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Yeah, it does that. It's marked as solved despite that error!

  10. #10
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Ok thank you.

  11. #11
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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.

  12. #12
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

Similar Threads

  1. questions
    By amaliutz in forum New To Java
    Replies: 13
    Last Post: 02-02-2011, 06:13 PM
  2. Questions
    By Csharks in forum New To Java
    Replies: 8
    Last Post: 01-19-2011, 04:11 AM
  3. Need help with some questions
    By El_Davidos in forum New To Java
    Replies: 5
    Last Post: 11-23-2010, 10:13 AM
  4. Three questions
    By an24 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-15-2010, 08:36 AM
  5. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •