0

While reading one of Java for beginners books I stumbled upon an exercise:

Write a program that takes three int values from the command line and prints them in ascending order. Use Math.min() and Math.max().

The problem is that if conditionals are not yet considered, so logically I can't use them.

I tried to use an answer to a similar question in C, but encountered not a statement error.

public class three_sort
{
        public static void main(String[] args)
        {
                int a = Integer.parseInt( args[0] );
                int b = Integer.parseInt( args[1] );
                int c = Integer.parseInt( args[2] );

                int min = a;
                (min > b) && (min = b); //finding minimum
                (min > c) && (min = c); 
                System.out.println(min);

                int i = a;
                (b < max) && (b > min) && (i = b); // finding intermediate
                (c < max) && (c > min) && (i = c); 
                System.out.println(i);

                int max = a;
                (max < b) && (max = b); //finding maximum
                (max < c) && (max = c); 
                System.out.println(max);
        }
}

And yes, I haven't used Math.min and Math.max, because with help of them I can find min and max, but can't find intermediate. Does anybody have any idea how to solve this problem?

Community
  • 1
  • 1
Alexander
  • 257
  • 1
  • 3
  • 11
  • 1
    see my [this](http://stackoverflow.com/a/28035535/3208640) answer I preferred 3 ways for it – void Feb 08 '15 at 12:57
  • @RealSkeptic yes looks like duplicate. I haven't found this answer before asking my. How can I delete my question? – Alexander Feb 08 '15 at 13:07
  • 1
    If you don't have a "delete" link under it (which may happen since you have upvoted answers) you can flag it for moderator attention and ask for them to take care of it. – RealSkeptic Feb 08 '15 at 13:13

3 Answers3

5

find min and max after that find the mid number:

int a = Integer.parseInt( args[0] );
int b = Integer.parseInt( args[1] );
int c = Integer.parseInt( args[2] );
int x = Math.min(a, b);
int min = Math.min(x,c);
int z = Math.max(a, b);
int max = Math.max(z, c);
int mid = a+ b+ c- min - max;
System.out.print(min);
System.out.print(mid);
System.out.print(max);

regarding this I have answered another question here you can see it.

Community
  • 1
  • 1
void
  • 7,760
  • 3
  • 25
  • 43
  • Works great for ints, but for doubles it can fall on your feet... – glglgl Feb 08 '15 at 13:05
  • yes of course, I have mentioned other ways which are in another answer of me and those ways has no problem with doubles – void Feb 08 '15 at 13:07
1

Not a full answer, but some food for thought ...

int a = 2;
int b = 5;
System.out.print(Math.min(a, b));
System.out.print(Math.max(a, b));

will print a and b in ascending order. You just need to generalize this to more than 2 numbers ...

Guillaume
  • 18,494
  • 8
  • 53
  • 74
1

Finding the minimum is easy, we can find the minimum between a and b let's say min_a_b and then the minimum between min_a_b and c. The same logic for the maximum.

In order to find the second bigger we take the maximum between each pair. If we do it we are going to take only the second bigger and the bigger. So we take the minimum between them

int min_a_b = Math.min(a, b);
int max_a_b = Math.max(a, b);
int max_b_c = Math.max(b, c);
int max_a_c = Math.max(a, c);
System.out.println(Math.min(min_a_b, c));
System.out.println(Math.min(Math.min(max_a_b, max_b_c), max_a_c));
System.out.println(Math.max(max_a_b, c));
JuniorCompressor
  • 19,631
  • 4
  • 30
  • 57