Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Assignment help.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Assignment help.

    Quote Originally Posted by lizartt View Post
    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.
    // 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
    Last edited by Zaphod_b; July 29th, 2012 at 09:19 AM.

Similar Threads

  1. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  2. Help with assignment
    By NPVinny in forum What's Wrong With My Code?
    Replies: 12
    Last Post: July 3rd, 2010, 05:31 PM
  3. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 03:20 PM
  4. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM
  5. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 08:44 PM