Upload files to "OOP/Java/Lab/Week5"
This commit is contained in:
parent
cb924d0b60
commit
8c2e55a281
5 changed files with 413 additions and 0 deletions
47
OOP/Java/Lab/Week5/Swap.java
Normal file
47
OOP/Java/Lab/Week5/Swap.java
Normal file
|
@ -0,0 +1,47 @@
|
|||
class Swap {
|
||||
|
||||
static void swap(int a, int b) {
|
||||
int temp = a;
|
||||
a = b;
|
||||
b = temp;
|
||||
System.out.println("Inside swap method: a = " + a + ", b = " + b);
|
||||
}
|
||||
|
||||
static void swap(int[] arr) {
|
||||
int temp = arr[0];
|
||||
arr[0] = arr[1];
|
||||
arr[1] = temp;
|
||||
System.out.println(
|
||||
"Inside swap method: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
||||
);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
java.util.Scanner scanner = new java.util.Scanner(System.in);
|
||||
|
||||
System.out.print("Enter value for x: ");
|
||||
int x = scanner.nextInt();
|
||||
System.out.print("Enter value for y: ");
|
||||
int y = scanner.nextInt();
|
||||
|
||||
System.out.println("Before swap: x = " + x + ", y = " + y);
|
||||
swap(x, y);
|
||||
System.out.println("After swap: x = " + x + ", y = " + y);
|
||||
|
||||
int[] arr = new int[2];
|
||||
System.out.print("\nEnter value for arr[0]: ");
|
||||
arr[0] = scanner.nextInt();
|
||||
System.out.print("Enter value for arr[1]: ");
|
||||
arr[1] = scanner.nextInt();
|
||||
|
||||
System.out.println(
|
||||
"Before swap: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
||||
);
|
||||
swap(arr);
|
||||
System.out.println(
|
||||
"After swap: arr[0] = " + arr[0] + ", arr[1] = " + arr[1]
|
||||
);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue