-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBox.java
More file actions
86 lines (69 loc) · 2.08 KB
/
Box.java
File metadata and controls
86 lines (69 loc) · 2.08 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class Box {
// Data members
private double height;
private double width;
private double depth;
// Constructor to initialize the box dimensions
public Box(double height, double width, double depth) {
this.height = height;
this.width = width;
this.depth = depth;
}
// Default constructor
public Box() {
this.height = 0.0;
this.width = 0.0;
this.depth = 0.0;
}
// Member function to calculate volume
public double volume() {
return height * width * depth;
}
// Getter methods
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
public double getDepth() {
return depth;
}
// Setter methods
public void setHeight(double h) {
height = h;
}
public void setWidth(double w) {
width = w;
}
public void setDepth(double d) {
depth = d;
}
// Method to display box information
public void displayBox() {
System.out.println("Box dimensions:");
System.out.println("Height: " + height);
System.out.println("Width: " + width);
System.out.println("Depth: " + depth);
System.out.println("Volume: " + volume());
}
// Main method to test the Box class
public static void main(String[] args) {
// Create a box using parameterized constructor
Box box1 = new Box(10.5, 8.0, 6.5);
System.out.println("Box 1:");
box1.displayBox();
System.out.println();
// Create another box using default constructor
Box box2 = new Box();
box2.setHeight(5.0);
box2.setWidth(4.0);
box2.setDepth(3.0);
System.out.println("Box 2:");
box2.displayBox();
System.out.println();
// Calculate and display volumes
System.out.println("Volume of Box 1: " + box1.volume());
System.out.println("Volume of Box 2: " + box2.volume());
}
}