Results 1 to 10 of 10
  1. #1
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default help with simple programming question

    I hope this question isn't too simple or dumb...

    Java Code:
    int x = 2;
    int y = "WKD".hashCode() % 3000;
    int z = "WWI".hashCode() % 3000;
    for (int i = 0; i <= z; i++)
      a = (x ^ i) % y;
    return z;
    Can someone please tell me what value is returned for z? Thanks!

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Why don't you use System.out.println(z); and find out?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    I was hoping that someone here could tell me the (correct) answer... :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eezilyBefuddled View Post
    I was hoping that someone here could tell me the (correct) answer... :)
    It all depends on what the hasCode() method in the String class returns; here it is:

    Java Code:
        public int hashCode() {
    	int h = hash;
    	if (h == 0) {
    	    int off = offset;
    	    char val[] = value;
    	    int len = count;
    
                for (int i = 0; i < len; i++) {
                    h = 31*h + val[off++];
                }
                hash = h;
            }
            return h;
        }
    b.t.w. you can find this in the src.zip file stored in your JDK directory.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    You think we keep the value of something like:
    "WWI".hashCode()
    in our heads?

    Well, maybe Jos does, but he's strange.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    You think we keep the value of something like:
    "WWI".hashCode()
    in our heads?

    Well, maybe Jos does, but he's strange.
    86377, but why ask?

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You were asking for a handout, I told you how to figure it out.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    86377, but why ask?

    kindest regards,

    Jos ;-)
    You realise that forced me to go and check, don't you?
    You swine!
    :mad:

  9. #9
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    You were asking for a handout, I told you how to figure it out.
    Sound like something my dad would say... of course, he was usually right. :eek:

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    You swine!
    :mad:
    That's Mrs Swine for you.

    kindest regards,

    Loretta ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. programming career advice question
    By silverglade in forum Jobs Discussion
    Replies: 2
    Last Post: 05-23-2011, 07:39 AM
  2. simple game programming help
    By b.m in forum New To Java
    Replies: 0
    Last Post: 11-25-2010, 05:16 PM
  3. Fairly Simple (I think) programming project
    By Glyph in forum Jobs Offered
    Replies: 0
    Last Post: 10-22-2010, 12:16 AM
  4. Simple Programming problem loop in array
    By katon in forum New To Java
    Replies: 1
    Last Post: 12-20-2009, 03:14 PM
  5. Programming Socket Question
    By paul in forum Networking
    Replies: 1
    Last Post: 07-30-2007, 07:26 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
  •