Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2012, 05:21 AM   PM User | #1
johnnycabbage
New Coder

 
Join Date: Nov 2011
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
johnnycabbage is an unknown quantity at this point
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?
johnnycabbage is offline   Reply With Quote
Old 01-27-2012, 01:10 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,641
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Despite the similar sounding names, Java is not the same as Javascript.
Moving from Java forum to Javascript forum.
Fou-Lu is offline   Reply With Quote
Old 01-27-2012, 01:18 PM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Objection ... this code is JAVA and not Javascript ... so the section was correct but the headline isn't
devnull69 is offline   Reply With Quote
Old 01-27-2012, 04:42 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,599
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Moved back to Java.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 01-27-2012, 07:26 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,641
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-27-2012, 10:57 PM   PM User | #6
johnnycabbage
New Coder

 
Join Date: Nov 2011
Posts: 41
Thanks: 2
Thanked 0 Times in 0 Posts
johnnycabbage is an unknown quantity at this point
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.
johnnycabbage is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 PM.


Advertisement
Log in to turn off these ads.