# Thread: how do you do this?

1. Member
Join Date
Jul 2009
Posts
7
Rep Power
0

## how do you do this?

excuse me guys.
im kinda new to java and trying to learn.
so a friend of mine tried me and asked me to make this.

"a program that exchanges the values of 4 variables: a, b, c, d. The output must be:
the new value of a is the previous value of d,
the new value of b is the previous value of a,
the new value of c is the previous value of b, and,
the new value of d is the previous value of c.

Use only 4 variables in your program to come up with the answer. "

Hi Jeffrey.

I drawed the references on a page and it looks like a cyclic list of pointers between 4 objects. So I'd use a circular singly linked list, that is, a singly linked list such that the next element of tail is the head. Add the numbers to this list. Then, by traversing the list from the second position from the head, you will rotate the list by one element.

I don't know if you would consider this list as the 5th variable or not. Anyways, it should work. Welcome to Java Forums. ;)
Last edited by tim; 07-07-2009 at 10:09 AM.

3. Member
Join Date
Dec 2008
Posts
99
Rep Power
0
I can give you way to solve it : if you want to switch between A and B , but you can use only two variable (A and B) then you solve it like that: (assuming A,B numbers, real or integer)
( let say A=10, B=300)
A=A+B (A=310)
B=A-B (B=10)
A=A-B (A=300)

Now expend it for four variable . Good luck

4. Member
Join Date
Jul 2009
Posts
7
Rep Power
0
i see.
now i understand.
thx,
^_^

5. Member
Join Date
Jul 2009
Posts
2
Rep Power
0
hey that can be simply achieve by following sets of assingment
a=d;
b=a;
c=b;
d=c;

6. Member
Join Date
Dec 2008
Posts
99
Rep Power
0
a=d;
b=a;
c=b;
d=c;
nice every variable get the value of "d"

7. Member
Join Date
Jul 2009
Posts
4
Rep Power
0
why didnt you take a paper and draw as tim did ?

8. Hey guys. :)
Originally Posted by nancy0903
jave, so hard
Java isn't hard really. If you have a nice thick book, all the time in the world and a passion for it, then it's easy!
Originally Posted by mann
why didnt you take a paper and draw as tim did ?
I agree. A piece of paper may be low tech, but it goes a long way. ;) I like itaipee's solution the most. Mine's too, computer sciency. :p

9. Member
Join Date
Jul 2009
Posts
4
Rep Power
0

## i hope this solves it

the following works .. but i m not able 2 figure out how the additional 1 is creeping inside :P

import java.util.Scanner;
public class Swap {

public static void main(String[] args) {
System.out.println("Enter ABCD");
Scanner scanboy = new Scanner(System.in);
int A = scanboy.nextInt();
int B = scanboy.nextInt();
int C = scanboy.nextInt();
int D = scanboy.nextInt();
System.out.println("A B C and D are " + A + " " + B + " " + C + " " + D ++);
A = A+B+C+D;
B = B+C+D;
C= C+D;
D = D;
D = C-D;
System.out.println(" value of D is " + D);
C = B - C;
System.out.println(" value of C is " + C);
B = A - B;
System.out.println(" value of B is " + B);

A = A - (B + C + D)-1;
System.out.println(" value of A is " + A);
}

}

10. Member
Join Date
Jul 2009
Posts
20
Rep Power
0
If you used CODE-tags it would be alot nicer to read.