PDA

View Full Version : Java Questions

johnnycabbage
01-27-2012, 06:21 AM
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?

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?

Fou-Lu
01-27-2012, 02:10 PM
Despite the similar sounding names, Java is not the same as Javascript.
Moving from Java forum to Javascript forum.

devnull69
01-27-2012, 02:18 PM
Objection ... this code is JAVA and not Javascript ... so the section was correct but the headline isn't

VIPStephan
01-27-2012, 05:42 PM
Moved back to Java.

Fou-Lu
01-27-2012, 08:26 PM
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:

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.

johnnycabbage
01-27-2012, 11:57 PM
Objection ... this code is JAVA and not Javascript ... so the section was correct but the headline isn'tMy bad

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:

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.