Need help with Java

Nov 24, 2008 17:15

Kay guys, here's the problem ( Read more... )

school, work

Leave a comment

darkbunny November 25 2008, 00:55:29 UTC
Math.max(number, otherNumber) gives the larger of two numbers.
Math.abs(number) gives the absolute value of a number*.

So...

public int largerAbsVal(int a, int b){
return Math.max(Math.abs(a),Math.abs(b));
}

*'number' in this case means short, long, int, float, or double.

Reply

jokergirl November 25 2008, 07:56:12 UTC
This.

Reply

reijigin November 25 2008, 08:13:49 UTC
Any advice on the quadratic one? :X

Reply

jokergirl November 25 2008, 08:16:54 UTC
I'm not quite sure I understand your question there. What do you mean by "accept any a, b and c"? Do you mean that the datatype of the a, b, and c parameters is unknown?

;)

Reply

reijigin November 25 2008, 08:32:37 UTC
Woops. I think it needs to accept variables a, b, and c to which different values can be assigned. I guess.

I have had a crazy fucking weekend/Monday and I'm more or less unable to focus on this for some reason. I pulled this out of my ass after looking at various examples in my text:

static double root(double A, double B, double C)
{
double disc = B*B - 4*A*C;
return (-B + Math.sqrt(disc)) / (2*A);
}
}

Reply

jokergirl November 25 2008, 08:34:33 UTC
Erm yeah, that's your solution, right there.

;)

Reply

jokergirl November 25 2008, 08:47:46 UTC
Oh, I'll look at the algorithm for a bit later, haven't had enough tea yet. I think a quadratic equation can have more than one solution, so you might have to return an array.
Do we assume the other side of the equation is 0?

;)

Reply

reijigin November 25 2008, 09:04:17 UTC
I... Don't know? :X I doubt it was much more complicated than that. The homework is usually just a simple review of the basic things covered in the lecture, but I missed the lecture today so I'm more than a little confused.

Reply

jokergirl November 25 2008, 09:06:33 UTC
Ah - can you check the lecture notes? It sounds like the idea was to talk about arguments and return variables, so that's what you should probably focus on :)

;)

Reply

reijigin November 25 2008, 09:54:31 UTC
Yeah I looked at them and the text, that's how I pieced that one together. Thanks :D

Reply

jokergirl November 25 2008, 09:33:23 UTC
Translating my earlier response: I think you can leave the solution as it is for now, actually. *had tea*

;)

Reply

reijigin November 25 2008, 08:06:43 UTC
Mother balls, I didn't know Math.max existed. Thanks.

Reply

jokergirl November 25 2008, 08:48:31 UTC
Even if it didn't, what's the problem with

if (abs(a)>abs(b)) {return a;} return b;

?

Reply

jokergirl November 25 2008, 08:49:06 UTC
er, return abs(a) and return abs(b), of course.

Need. More. Tea.

Reply

reijigin November 25 2008, 09:07:56 UTC
That would be fine, but we haven't done any if ___ then do ___ type things yet. We just barely started Java a little while ago and have barely covered anything.

Reply

jokergirl November 25 2008, 09:31:56 UTC
Hrm, too weird, it sounds kind of basic to me. But I guess to each teacher their own style :)
Anyway, use the Max function since it exists and it's probably faster anyway. Library functions often are.

;)

Reply


Leave a comment

Up