-
Notifications
You must be signed in to change notification settings - Fork 7
/
OverloadCons2.java
73 lines (54 loc) · 1.59 KB
/
OverloadCons2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package chapter7;
//Here box allows one object to initialize another
class Box2 {
double width;
double height;
double depth;
//Constructor used when no dimension is specified.
Box2() {
width = -1;
height = -1;
depth = -1;
}
//Constructor used when all dimensions are specified.
Box2(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
//Notice this constructor , it takes an object of type Box.
Box2(Box2 ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
//Constructor used when cube is created.
Box2(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
public class OverloadCons2 {
public static void main(String[] args) {
//Creates boxes using the various constructor..
Box2 myBox1 = new Box2(10, 20, 15);
Box2 myBox2 = new Box2();
Box2 myCube = new Box2(7);
Box2 myClone = new Box2(myBox1);
double volume;
//Get volume of the first Box
volume = myBox1.volume();
System.out.println("Volume of myBox1 is " + volume);
//Get volume of second Box
volume = myBox2.volume();
System.out.println("Volume of myBox2 is " + volume);
//Get volume of cube
volume = myCube.volume();
System.out.println("Volume of myCube is " + volume);
//Get volume of clon
volume = myClone.volume();
System.out.println("Volume of clone is " + volume);
}
}