Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Numbers{
- int a, b;
- }
- class Swapping{
- public static void swap_by_ref(Numbers n){
- System.out.println("[swap_by_ref(), at start]n.a = "+n.a+" and n.b = "+n.b);
- int temp = n.a;
- n.a = n.b;
- n.b = temp;
- System.out.println("[swap_by_ref()], at end]n.a = "+n.a+" and n.b = "+n.b);
- }
- public static void swap_by_val(int a, int b){
- System.out.println("[swap_by_val()], at start]a = "+a+" and b = "+b);
- int temp = a;
- a = b;
- b = temp;
- System.out.println("[swap_by_val()], at start]a = "+a+" and b = "+b);
- }
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter two numbers.");
- Numbers n = new Numbers();
- n.a = sc.nextInt();
- n.b = sc.nextInt();
- int x = n.a;
- int y = n.b;
- System.out.println("\n\n[main(), before swap_by_val()]x = "+x+" and y = "+y);
- swap_by_val(x, y);
- System.out.println("[main(), after swap_by_val()]x = "+x+" and y = "+y+"\n\n");
- System.out.println("[main(), before swap_by_ref()]n.a = "+n.a+" and n.b = "+n.b);
- swap_by_ref(n);
- System.out.println("[main(), after swap_by_ref()]n.a = "+n.a+" and n.b = "+n.b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement