Results 1 to 10 of 10

# Thread: how do you do this?

- 07-07-2009, 09:21 AM #1Member
- 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.

i find it likely nice to know something about this.

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, 10:07 AM #2
## Singly Linked List

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.

Eyes dwelling into the past are blind to what lies in the future. Step carefully.

- 07-07-2009, 10:40 AM #3Member
- 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

- 07-14-2009, 09:09 AM #4Member
- Join Date
- Jul 2009
- Posts
- 7

- Rep Power
- 0

i see.

now i understand.

thx,

^_^

- 07-14-2009, 08:39 PM #5Member
- 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;

- 07-15-2009, 09:57 AM #6Member
- Join Date
- Dec 2008
- Posts
- 99

- Rep Power
- 0

a=d;

b=a;

c=b;

d=c;

- 07-15-2009, 02:45 PM #7Member
- Join Date
- Jul 2009
- Posts
- 4

- Rep Power
- 0

why didnt you take a paper and draw as tim did ?

- 07-15-2009, 05:12 PM #8
Hey guys. :)

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!

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. :pEyes dwelling into the past are blind to what lies in the future. Step carefully.

- 07-24-2009, 11:47 PM #9Member
- 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);

}

}

- 07-25-2009, 08:21 PM #10Member
- Join Date
- Jul 2009
- Posts
- 20

- Rep Power
- 0

## Bookmarks