Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Java Questions

  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Java Questions

    These questions are part of a captcha. I'm 75% sure I got the first one right, but the second, I have no clue. Can anyone help?
    Code:
     int a = 2;
          int b = "JZG".hashCode() % 3000;
          int c = "ARA".hashCode() % 3000;
          for (int i = 0; i <= c; i++)
             a = (a ^ i) % b;
          return a;
    Q. 1. What is the returned value?

    Q. 2. What is the greatest common divisor of the previous two results?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Despite the similar sounding names, Java is not the same as Javascript.
    Moving from Java forum to Javascript forum.

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Objection ... this code is JAVA and not Javascript ... so the section was correct but the headline isn't

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,471
    Thanks
    6
    Thanked 980 Times in 953 Posts
    Moved back to Java.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    My bad, I should have paid attention that you have datatypes here.
    The first answer is 1955. I ran this as you have here to get that, but also wrote a quick hashcode creation for it to see how you'd do it by hand if you had to.
    The greatest common divisor is more complicated. Since I don't know what this refers to by the "previous two results", I have no choice but to write a function to determine this. To do this, I used recursion, although you may also loop:
    Code:
    	public static int getGCD(int a, int b)
    	{
    		int iResult = 0;
    		if (a < 0)
    		{
    			a *= -1;
    		}
    		if (b < 0)
    		{
    			b *= -1;
    		}
    		
    		if (a == 0 || b == 1 || a == b)
    		{
    			iResult = b;
    		}
    		else if (a == 1 || b == 0)
    		{
    			iResult = a;
    		}
    		else if (a > b)
    		{
    			iResult = getGCD(b, a % b);
    		}
    		else
    		{
    			iResult = getGCD(a, b % a);
    		}
    		return iResult;
    	}
    As for where you have to use it, I have no idea. If its referring to the results of the loop, then its a matter of capturing the last two results using a shadow. Since your code is a function, I somehow doubt that its the last two within the for loop since you would have no way to return the results.

  • #6
    New Coder
    Join Date
    Nov 2011
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Objection ... this code is JAVA and not Javascript ... so the section was correct but the headline isn't
    My bad

    Quote Originally Posted by Fou-Lu View Post
    My bad, I should have paid attention that you have datatypes here.
    The first answer is 1955. I ran this as you have here to get that, but also wrote a quick hashcode creation for it to see how you'd do it by hand if you had to.
    The greatest common divisor is more complicated. Since I don't know what this refers to by the "previous two results", I have no choice but to write a function to determine this. To do this, I used recursion, although you may also loop:
    Code:
    	public static int getGCD(int a, int b)
    	{
    		int iResult = 0;
    		if (a < 0)
    		{
    			a *= -1;
    		}
    		if (b < 0)
    		{
    			b *= -1;
    		}
    		
    		if (a == 0 || b == 1 || a == b)
    		{
    			iResult = b;
    		}
    		else if (a == 1 || b == 0)
    		{
    			iResult = a;
    		}
    		else if (a > b)
    		{
    			iResult = getGCD(b, a % b);
    		}
    		else
    		{
    			iResult = getGCD(a, b % a);
    		}
    		return iResult;
    	}
    As for where you have to use it, I have no idea. If its referring to the results of the loop, then its a matter of capturing the last two results using a shadow. Since your code is a function, I somehow doubt that its the last two within the for loop since you would have no way to return the results.
    Thanks mate. Both answers are 1955. I should have mentioned that the "two previous results" meant to also refer to the answer from the very first question, which was what year Albert Einstein was born. That wasn't Java related so I didn't include it.


  •  

    Posting Permissions

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