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 6 of 6

Thread: Can someone explain to me?

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

    Default Can someone explain to me?

    Can someone explain the coding in bold to me? I don't understand how to get 40 10 20 30.

    public class Rotater {


    public static void main(String[] args) {


    int a = 10, b = 20, c = 30, d = 40;
    int temp;

    temp = d;
    d = c;
    c = b;
    b = a;
    a = temp;



    System.out.println("before rotation: 10 20 30 40");
    System.out.println("after rotation: " + a + " " + b + " " + c + " " + d);


    }
    }

  2. The Following User Says Thank You to unleashed-my-freedom For This Useful Post:

    roronoaz (July 3rd, 2012)


  3. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Can someone explain to me?

    Hello unleashed-my-freedom!
    What is your question? You use the temp variable to rotate the values of your "basic" variables (a,b,c,d). So, you give every variable the value of the next (or previous) variable* . The additional variable (temp) is used for keeping the first basic variables's value and don't loose it.

    *in your case next and previous don't have an actual meaning because the variables are not ordered but suppose they are (a-b-c-d)

    Hope I was clear.

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Can someone explain to me?

    At first, I had 10 20 30 40
    But the temp, I had 40 10 20 30.
    So you mean if I use temp, the number will (in this case) increase by 1 position?

  5. The Following User Says Thank You to unleashed-my-freedom For This Useful Post:

    roronoaz (July 3rd, 2012)

  6. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Can someone explain to me?

    After the rotation the values have moved one step forward with the last value going to the first variable (like a cycle).
    after initialization
    a   b   c   d    temp
    10  20  30  40      -
     
     
    temp  = d;
    a   b   c   d    temp
    10  20  30  40      40
     
     
    d = c;
    a   b   c  d    temp
    10  20  30  30   40
     
     
    c = b;                   
    a   b   c   d     temp
    10  20  20  30      40
     
     
    b = a;                 
    a   b   c   d     temp
    10  10  20  30      40
     
     
    a = temp;           
    a   b   c   d     temp
    40  10  20  30      40
    Hope this helps.

  7. The Following 2 Users Say Thank You to andreas90 For This Useful Post:

    roronoaz (July 3rd, 2012), unleashed-my-freedom (July 3rd, 2012)

  8. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Can someone explain to me?

    so we use temp just to increase the order by 1?

  9. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Can someone explain to me?

    Quote Originally Posted by unleashed-my-freedom View Post
    so we use temp just to increase the order by 1?
    We use temp because otherwise you would loose a value. Think like you have two cups (A and B) in two positions (1 and 2 respectively) that each of them can place only one cup and you can carry only one cup in your hands. Now, if you want to exchange positions between cups you start like this:
    take cup A and put it in position 1. But then you have to throw away cup B(because you can't have two cups in you a position or in your hands).
    Therefore you need a new assisting position (3).
    Then you put
    1) cup A to position 3 - now position 1 is empty
    2) cup B to position 1 - now position 2 is empty
    3) cup A to position 2

    So, temp is the assisting position.

    Hope that makes sense.

  10. The Following User Says Thank You to andreas90 For This Useful Post:

    roronoaz (July 3rd, 2012)

Similar Threads

  1. Can Some one explain to me why the output will be like this...
    By lunchong in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 1st, 2012, 12:19 AM
  2. i need an explain please !
    By keep smiling in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 11:22 AM
  3. Replies: 1
    Last Post: December 13th, 2010, 05:13 AM
  4. Can anyone explain this code for me
    By gudwindavids in forum Object Oriented Programming
    Replies: 1
    Last Post: December 11th, 2009, 02:29 PM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM