1

So I have another assignment to do and the task is to assort 3 strings alphabetically using the compareTo method. Basically the program receives 3 strings (a, b, and c) from a tester class and its supposed to return back the "getMin", "getMiddle", and "getMax".

I figured out the getmin and max, seemed easy but im having problems with the getMiddle. this is what i have for min and max:

        String min = "";
    if (a.compareTo(b) <= 0 && a.compareTo(c) <= 0) min = a;
    else if (b.compareTo(a) <= 0 && b.compareTo(c) <= 0) min = b;
    else if (c.compareTo(b) <= 0 && c.compareTo(a) <= 0) min = c;
    return min;

and similarly for get max only slightly different. How can I go about creating the getMiddle. Also we are not allowed to use arrays as we "haven't learned" them yet. and the prof said that the code for get middle should be around 5-6 lines.

Thanks

1
  • why tagged as 'interview questions'? Commented Oct 11, 2011 at 16:53

6 Answers 6

6

Multiply return values of compareTo method. If the value is middle, results of compareTo method have different signs. do multiply result is zero or has negative sign.

String getMiddle(String a,String b,String c)
{
    String middle = "";
    if (a.compareTo(b)*a.compareTo(c) <= 0) middle = a;
    else if (b.compareTo(a)*b.compareTo(c) <= 0) middle = b;
    else if (c.compareTo(b)*c.compareTo(a) <= 0) middle = c;
    return middle;
}
Sign up to request clarification or add additional context in comments.

1 Comment

ahh thank you very much, thats actually quite brilliant, i didnt think that far :)
1
 String middle = "";
    if (a.compareTo(b) <= 0 && a.compareTo(c) >= 0) middle = a;
    else if (b.compareTo(a) <= 0 && b.compareTo(c) >= 0) middle = b;
    else if (c.compareTo(b) <= 0 && c.compareTo(a) >= 0) middle = c;
    return middle;

1 Comment

yah but its not that simple, i tried that except what if if (a.compareTo(c) <= 0 && a.compareTo(b) >= 0) middle = a; thats also a possibility, same for the other 2 variables :(
1

Doing it your way, it would look like this:

if      (a.compareTo(b) > 0 && a.compareTo(c) <= 0) middle = a;
else if (a.compareTo(c) > 0 && a.compareTo(b) <= 0) middle = a;
else if (b.compareTo(a) > 0 && b.compareTo(c) <= 0) middle = b;
else if (b.compareTo(c) > 0 && b.compareTo(a) <= 0) middle = b;
else middle = c;
return middle

Well, that's the general gist behind it. You could combine some of these together for fewer lines, but I'll leave that up to you.

1 Comment

yah i know, thats what i started doing but it ends up a little over the recommended lines of code. the way ivorycirrus did it is correct i think.
1

Why so complicated ? Just use TreeSet, it uses compareTo() internally :).

1 Comment

yah but we haven't learned it yet so we cant really use it yet
0

this gives the mid..haven't tested it thorougly..and also, as for the number of lines prediction, i'm not sure i met it very well...anyway...

    String mid = "";
    if (a.compareTo(b) <= 0) {
        if (b.compareTo(c) <= 0) mid = b;
        else mid = c;}
    else if(a.compareTo(c) <= 0) mid = a;
    else mid = c; 
    return mid;

Comments

0

I will go with TreeSet only because it is sorting the data after adding to it.

1 Comment

yah but we haven't learned it yet so we cant really use it yet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.