views:

343

answers:

4

Ok I'm doing this programming assignment and need a little help.

Here is the problem:

Given three ints, a b c, return true if it is possible to add two of the ints to get the third.

twoAsOne(1, 2, 3) → true
twoAsOne(3, 1, 2) → true
twoAsOne(3, 2, 2) → false

Here's the what I have gotten so far:

public boolean twoAsOne(int a, int b, int c) {
  return a + b != c;
}

It keeps saying it is not fully correct and I do not know where I am going wrong.

+11  A: 

The question asks if it is possible to add any two to get the remaining one. Your code tests only if the first two add to the third.

Thus, twoAsOne(3,1,2) should return true because 3 = 1 + 2; but you are only checking whether 3 + 1 = 2, which is false.

itowlson
could you give me an example
@erics2008: it is YOUR job to write the code. If you don't engage your brain to work it out for yourself, you won't learn anything from this homework exercise. And learning is THE POINT of the exercise!
Stephen C
What Stephen said - I've given you an example of another combination that you need to check, but since this is homework, you're going to get more value out of it by figuring out the code yourself. You've obviously figured out how to do one of the cases: consider what are the other cases and how might you handle those?
itowlson
Think about the math. What's the property that the sum has in relation to the addends?
Calyth
+7  A: 

You're only checking one of the possibilities and, on top of that, you're checking it wrongly since you'll return false if a + b == c (because you're using the != operator).

I'm not going to do you homework for you, but the full list of possibilities is:

n1 = n2 + n3
n2 = n1 + n3
n3 = n1 + n2

It should be a simple matter: the result should be true if any of those is true. Otherwise the result should be false.

Or, to provide even a more obvious clue: it should be true if one or more of those conditions are met. Else it should be false.

I don't know how much more obvious I can make it without writing the code for you :-)

Update: And now that more than enough time has probably elapsed to make the homework point moot, here's my solution:

public boolean twoAsOne (int n1, int n2, int n3) {
    if (n1 == n2 + n3) return true;
    if (n2 == n1 + n3) return true;
    if (n3 == n1 + n2) return true;
    return false;
}

Although those last two lines could be replaced with:

    return (n3 == n1 + n2);

I prefer the (to me, anyway) more readable version.

paxdiablo
More obvious = pseudo-code. In something-that's-not-java.
nilamo
+2  A: 

Besides the answers provided by itowlson and Pax, since you are dealing with ints, there is a possibility that they will overflow, e.g.

Integer.MAX_VALUE + 1 == Integer.MIN_VALUE

Which is not mathematically true

You may want to check this kind of scenarios to make your program complete.

guigui
A: 

Dude I hope you got the answer by now... if you haven't

public boolean twoAsOne(int a, int b, int c) {
    boolean ans = false;

    if((a+b==c) || (a+c==b) || (b+c==a))  
      ans= true;

    return ans;
}
Richie
@Richie, it's considered "bad form" to just give the answers to homework questions. Not that I'll waste a downvote since (1) you're new here and (2) you can't go negative on rep :-)
paxdiablo