how do you do this?

• 07-07-2009, 08:21 AM
jeffrey
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. "
• 07-07-2009, 09:07 AM
tim
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. ;)
• 07-07-2009, 09:40 AM
itaipee
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
• 07-14-2009, 08:09 AM
jeffrey
i see.
now i understand.
thx,
^_^
• 07-14-2009, 07:39 PM
IEEE
hey that can be simply achieve by following sets of assingment
a=d;
b=a;
c=b;
d=c;
• 07-15-2009, 08:57 AM
itaipee
Quote:

a=d;
b=a;
c=b;
d=c;
nice every variable get the value of "d"
• 07-15-2009, 01:45 PM
mann
why didnt you take a paper and draw as tim did ?
• 07-15-2009, 04:12 PM
tim
Hey guys. :)
Quote:

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!
Quote:

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
• 07-24-2009, 10:47 PM
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);
}

}
• 07-25-2009, 07:21 PM
hero
If you used CODE-tags it would be alot nicer to read.