-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSwapObjects2Solution.java
51 lines (47 loc) · 1.11 KB
/
SwapObjects2Solution.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//A java Program to demostrate wrapper class to swap objects of a car
class Box
{
int no;
float length;
float depth;
float width;
Box(int n,float l,float d,float w)
{
no=n;
length=l;
depth=d;
width=w;
}
void characteristics()
{
System.out.println("Box Number:"+no+" Length:"+length+" Depth:"+depth+" Width:"+width);
}
}
//Wrapper class
class BoxWrapper
{
Box myBox;
BoxWrapper(Box obj)
{
myBox=obj;
}
}
/**
* SwapObjects2Solution
*/
public class SwapObjects2Solution {
public static void swap(BoxWrapper mybox1,BoxWrapper mybox2) {
Box temp=mybox1.myBox;
mybox1.myBox=mybox2.myBox;
mybox2.myBox=temp;
}
public static void main(String[] args) {
Box box1=new Box(1,30f,45.5f,67.8f);
Box box2=new Box(2,57f,90f,10f);
BoxWrapper boxw1=new BoxWrapper(box1);
BoxWrapper boxw2=new BoxWrapper(box2);
swap(boxw1,boxw2);
boxw1.myBox.characteristics();
boxw2.myBox.characteristics();
}
}