-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDemoBoxWeight.java
More file actions
65 lines (51 loc) · 1.32 KB
/
DemoBoxWeight.java
File metadata and controls
65 lines (51 loc) · 1.32 KB
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
package chapter8;
//This program uses inheritance to extend Box
class Box {
double width;
double height;
double depth;
Box(Box ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1;
height = -1;
depth = -1;
}
Box(int len) {
width = height = depth = len;
}
//Compute and return volume
double volume() {
return width * height * depth;
}
}
//Here Box is extended to include weight
class BoxWeight extends Box {
double weight;
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
public class DemoBoxWeight {
public static void main(String[] args) {
BoxWeight myBox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight myBox2 = new BoxWeight(2, 3, 4, 0.076);
double vol;
System.out.println("Volume of myBox1 is " + myBox1.volume());
System.out.println("Weight of myBox1 is " + myBox1.weight);
System.out.println();
System.out.println("Volume of myBox2 is " + myBox2.volume());
System.out.println("Weight of myBox2 is " + myBox2.weight);
}
}