answersLogoWhite

0


Best Answer

/*Program to swap 2 values without using the temporary variable and Arithmetic operators*/

class Swap

{

public static void main(String args[])

{

int a=1;

int b=2;

System.out.println("Before swap: a="+a+"b="+b);

a=a^b;

b=a^b;

a=a^b;

System.out.println(" After swap: a="+a+"b="+b);

}

}

Another Method

class Swap

{

  • public static void Swap()

  • {

  • int num1 = 10;

  • int num2 = 20;

  • System.out.println("Before Swapping");

  • System.out.println("Value of num1 is :" + num1);

  • System.out.println("Value of num2 is :" +num2);

  • //add both the numbers and assign it to first

  • num1 = num1 + num2;

  • num2 = num1 - num2;

  • num1 = num1 - num2;

  • System.out.println("Before Swapping");

  • System.out.println("Value of num1 is :" + num1);

  • System.out.println("Value of num2 is :" +num2);

  • }
  • }
User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

/* ------- first method -------- */

lets assume

a = 10;

b = 20;

a = a +b; // Now a will become - 30(10+20)

b = a - b // B will be 10 (30 - 20)

a = a - b // A will be 20 (30 - 10)

Both the variable is swapped

/*---------- Second method ----------*/

lets assume

a = 10;

b = 20;

a = a^b;

b = a^b;

a = a^b;

Now both the value has been changed ...

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Consider the following declarations:


int x = 0;

int y = 1;


In order to swap the values, we need to use a temporary variable:


int t = x;

x = y;

y = t;


However, it is possible to swap the values without using a third variable:


x ^= y ^= x ^= y;

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

There are two ways how to accomplish this, using subtraction and addition operations or XOR operation. This example has in both ways of doing it.

a = 7

b = 5

a = a + b = 7 + 5 = 12

b = a - b = 12 - 5 = 7 (b)

a = a - b = 12 - 7 = 5 (a)

---

a = 0011 (binary number representation)

b = 0101

a = a xor b = 0110

b = a xor b = 0011 (b)

a = a xor b = 0101 (a)

Here is JAVA code doing exactly that:

import java.util.*;

public class Test

{

public static void main(String[] args)

{

// Without XOR opertion

int a = 7;

int b = 5;

a = a + b;

b = a - b;

a = a - b;

System.out.println("a = " + a + "; b = " + b);

// Output: a = 5; b = 7

// With XOR operation

int c = Integer.parseInt("0011", 2);

int d = Integer.parseInt("0101", 2);

c = c ^ d;

d = c ^ d;

c = c ^ d;

System.out.println("c = " + Integer.toBinaryString(c) + "; d = " + Integer.toBinaryString(d));

// Output: c = 101; d = 11

}

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

class swapprogram {

public static void main(String args[]) {

int a=10,b=11,t=0

/* Define the values to be swapped; a and b are the values to be

swapped, while t is the holding variable. */

System.out.println("The Values of A and B before the swap" + a + ", " +

b); /* Print out the variables' values before the swap. */

t=a; /* Move a into the holding variable */

a=b; /* Move b into a; the swapping occurs here */

b=t; /* Move the holding variable (a) into b */

System.out.println("The Values of A and B after the swap" + a + ", " + b);

/* Print out the variables' values after the swap */

}

}

By

Saravanan and APerson241

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

assume x, y, temp are all declared as the same type of variable and x and y have values:

temp = x; //store one of the values to a temporary

x = y; //the variable holding the value just stored can now be overwritten

y = temp; // place x's original value into y

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

This has nothing to do with the GUI. The swapping of variables will be done in code. You do this by copying one of the variable to a temprary variable. For example:

// Initial values

int a = 5;

int b = 7;

// Swap the values

int temp = a;

a = b;

b = temp;

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Most languages don't have specific commands to swap two variables; in that case, you must temporarily store the information in a third variable, for example:

//Swap "a" and "b"

temp = a;

a = b;

b = temp;

This of course assumes that all variables have already been declared.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you swap two variables in java with GUI?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can a java program has two different variable?

Yes. You can have as many variables as you want in Java


How do you write a program in Perl to swap two variables without using the third one?

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


Swap two number using pointer?

The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int& p, int& q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }


Variables as object in java?

There are two main categories of variables in Java. They are primitive and non primitive. Primitive data types are the basic data types like int, float, char etc. These are not objects. The other non primitive data types are all types of Java Objects. Example: String, ArrayList etc.


How do you swap two numbers in qbasic?

Create a form with two text boxes (txtNumber1, and txtNumber2) and a command button (cmdSwap). Option Explicit Dim numb1 As Variant Dim numb2 As Variant Private Sub cmdSwap_Click() numb1 = txtNumber1.Text numb2 = txtNumber2.Text txtNumber2.Text = numb1 txtNumber1.Text = numb2 End Sub

Related questions

How do yow swap two variables in java using a function?

You can swap two variables, by storing one of them temporarily in a third variable, like this: temp = a; a = b; b = temp; Inside a function, this won't work, because the function parameters are COPIES of the original variables, not the variables themselves. Any change won't affect the original variables. If you work with OBJECTS, and swap the CONTENTS of the objects (not the object pointers), it can work, though.


Can a java program has two different variable?

Yes. You can have as many variables as you want in Java


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


How do you swap two variables using airthematic operators?

a += b; b -= a; a -= b;


How do you write a program in Perl to swap two variables without using the third one?

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


Program to swap two numbers using three variables in java?

temp = a; a = b; b = temp;temp = a; a = b; b = temp;temp = a; a = b; b = temp;temp = a; a = b; b = temp;


Swap two number using pointer?

The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int& p, int& q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }


Write an expression that computes the sum of two variables in java?

int sum = a + b; PS: a and b are int variables that must have been already declared and initialized.


Variables as object in java?

There are two main categories of variables in Java. They are primitive and non primitive. Primitive data types are the basic data types like int, float, char etc. These are not objects. The other non primitive data types are all types of Java Objects. Example: String, ArrayList etc.


How do you swap two numbers in qbasic?

Create a form with two text boxes (txtNumber1, and txtNumber2) and a command button (cmdSwap). Option Explicit Dim numb1 As Variant Dim numb2 As Variant Private Sub cmdSwap_Click() numb1 = txtNumber1.Text numb2 = txtNumber2.Text txtNumber2.Text = numb1 txtNumber1.Text = numb2 End Sub


How do you write a program in C to swap two variables without using the third one using XOR?

a=a^b; b=a^b; a=a^b;


Write a java program of addition of two variables?

public class AddNumbers{ public int add(int a, int b){ return a + b; } }