# Assignment help.

Printable View

• July 29th, 2012, 03:27 AM
lizartt
Assignment help.
Implement a java method with three local int variables A,b,c
Sort three of the values in ascending order by comparing and exchanging their values.
end of program a<=b<=c must hold.

its a util.scanner programme
how do i create. I have tried but to no avail.

package assignment;
import java.util.Scanner;

public class Q3 {

public static void main(String[] args) throws Exception

{

int A,B,C,D;
String result;
Scanner reader= new Scanner(System.in);
System.out.print("Enter value A : ");
A=reader.nextInt();
System.out.print("Enter value B :");
B=reader.nextInt();
System.out.print("Enter value C :");
C=reader.nextInt();

if (A>B)
D=A;
A=B;
B=D;

{
System.out.println("Results is ");
}

}
}

* I only know up to here
Seriously im not gd in this... And i have no one to help
• July 29th, 2012, 07:24 AM
Norm
Re: Assignment help.
You need to work out the logic before trying to write code.
What comparisons need to be made? The logic should use several if tests to determine the order of the values.
• July 29th, 2012, 08:18 AM
Zaphod_b
Re: Assignment help.
Quote:

Originally Posted by lizartt
Implement a java method with three local int variables A,b,c
Sort three of the values in ascending order by comparing and exchanging their values.

Code :

```// A "bubble sort" with three numbers Given numbers a, b, c   IF a IS GREATER THAN b THEN SWAP a AND b END IF // Now we know that a is larger than b, but we don't // know whether or not b is larger than c // // So...   IF b IS GREATER THAN c THEN SWAP b and c END IF   // Now, at this point we know that c contains the largest of the // three values. // We don't know whether or not a is larger than b any more // since b may have changed from what it was after the first // step. // // So...   IF a IS GREATER THAN b THEN SWAP a AND b END IF   //Taa-daa```

Try the following six cases with pencil and paper. Go through the steps of the algorithm shown above. Really. Do it with pencil and paper.

Don't just look at then and nod your head and say, "Oh, yeah. Now I get it." Write down all of the steps. There are only six sorting sequences with different values of a, b, c, and each case has only two steps. Invest the time to come to a complete understanding. (You will thank me later...)

a b c
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Just for kicks, you can even try with repeated numbers to make sure it always works
a b c
1 1 2
1 2 1
2 1 1
1 1 1

Now, you have covered all possible sort sequences for three variables. The Java that comes from a direct implementation of the above pseudo-code is "trivial." (You have already implemented the first step, right?)

Test the code with the same cases that you worked out with pencil and paper (or anything else that covers all possible ordering of three variables).

Cheers!

Z