Step 1:
//Package for swapping two numbers
//Developed By Ahlam Ansari
package SwapNos;
public class Swap
{
int a,b;
public Swap(int x,int y)
{
a=x;
b=y;
}
public void withoutTemp()
{
a=a+b;
b=a-b;
a=a-b;
}
public void withTemp()
{
int temp;
temp=a;
a=b;
b=temp;
}
public void display()
{
System.out.print("Values are :\n a=" +a+" b="+b);
}
}
/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac -d . Swap.java
*/
Step 2:
//Program to import a package that will swap two nos
//Developed by Ahlam Ansari
import java.util.*;
import SwapNos.Swap;
class MainSwap
{
public static void main(String ahlam[])
{
int a,b;
Scanner d=new Scanner(System.in);
System.out.println("Enter the two numbers to be swapped");
a=d.nextInt();
b=d.nextInt();
Swap s=new Swap(a,b);
System.out.println("Before Swapping:");
s.display();
System.out.println("\nPress 1 for swapping using temporary variable \nPress 2 for swapping without temporary variable");
int choice=d.nextInt();
switch(choice)
{
case 1: s.withTemp();
break;
case 2: s.withoutTemp();
break;
default: System.out.println("Invalid Choice");
}
System.out.println("After Swapping:");
s.display();
}
}
/*
Output
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MainSwap.java
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainSwap
Enter the two numbers to be swapped
432
234
Before Swapping:
Values are :
a=432 b=234
Press 1 for swapping using temporary variable
Press 2 for swapping without temporary variable
1
After Swapping:
Values are :
a=234 b=432
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainSwap
Enter the two numbers to be swapped
234
432
Before Swapping:
Values are :
a=234 b=432
Press 1 for swapping using temporary variable
Press 2 for swapping without temporary variable
2
After Swapping:
Values are :
a=432 b=234
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/
//Package for swapping two numbers
//Developed By Ahlam Ansari
package SwapNos;
public class Swap
{
int a,b;
public Swap(int x,int y)
{
a=x;
b=y;
}
public void withoutTemp()
{
a=a+b;
b=a-b;
a=a-b;
}
public void withTemp()
{
int temp;
temp=a;
a=b;
b=temp;
}
public void display()
{
System.out.print("Values are :\n a=" +a+" b="+b);
}
}
/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac -d . Swap.java
*/
Step 2:
//Program to import a package that will swap two nos
//Developed by Ahlam Ansari
import java.util.*;
import SwapNos.Swap;
class MainSwap
{
public static void main(String ahlam[])
{
int a,b;
Scanner d=new Scanner(System.in);
System.out.println("Enter the two numbers to be swapped");
a=d.nextInt();
b=d.nextInt();
Swap s=new Swap(a,b);
System.out.println("Before Swapping:");
s.display();
System.out.println("\nPress 1 for swapping using temporary variable \nPress 2 for swapping without temporary variable");
int choice=d.nextInt();
switch(choice)
{
case 1: s.withTemp();
break;
case 2: s.withoutTemp();
break;
default: System.out.println("Invalid Choice");
}
System.out.println("After Swapping:");
s.display();
}
}
/*
Output
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MainSwap.java
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainSwap
Enter the two numbers to be swapped
432
234
Before Swapping:
Values are :
a=432 b=234
Press 1 for swapping using temporary variable
Press 2 for swapping without temporary variable
1
After Swapping:
Values are :
a=234 b=432
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainSwap
Enter the two numbers to be swapped
234
432
Before Swapping:
Values are :
a=234 b=432
Press 1 for swapping using temporary variable
Press 2 for swapping without temporary variable
2
After Swapping:
Values are :
a=432 b=234
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/
No comments:
Post a Comment