forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwap.java
More file actions
34 lines (30 loc) · 740 Bytes
/
Swap.java
File metadata and controls
34 lines (30 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#https://www.linkedin.com/posts/rohit-singh-b742bb199_participate-in-hacktoberfest-2020-complete-activity-6717303409113882624-SnO9
#Subscribed by Code House
// A Java program to demonstrate that we can swap two
// objects be swapping members.
// A car with number class Car
class Car
{
int no;
Car(int no) { this.no = no; }
}
// A class that uses Car objects
class Main
{
// To swap c1 and c2
public static void swap(Car c1, Car c2)
{
int temp = c1.no;
c1.no = c2.no;
c2.no = temp;
}
// Driver method
public static void main(String[] args)
{
Car c1 = new Car(1);
Car c2 = new Car(2);
swap(c1, c2);
System.out.println("c1.no = " + c1.no);
System.out.println("c2.no = " + c2.no);
}
}