BoxVolume.java
42 linesjava
DOWNLOAD
1// Aim: Program to perform constructor operations (Box Volume).
2public class BoxVolume{
3    double width, height, depth;
4
5    public BoxVolume(){
6        width = 0;
7        height = 0;
8        depth = 0;
9    }
10    public BoxVolume(double w, double h, double d){
11        this.width = w;
12        this.height = h;
13        this.depth = d;
14    }
15    public BoxVolume(double size){
16        this.width = size;
17        this.height = size;
18        this.depth = size;
19    }
20
21    public void volume(){
22        double vol = width * height * depth;
23        System.out.println("Volume: " +vol);
24    }
25}
26
27class boxDemo{
28    public static void main(String[] args) {
29        BoxVolume box1 = new BoxVolume();
30        BoxVolume box2 = new BoxVolume(10, 20, 30);
31        BoxVolume box3 = new BoxVolume(25);
32
33        System.out.print("Box1 ");
34        box1.volume();
35        
36        System.out.print("Box2 ");
37        box2.volume();
38        
39        System.out.print("Box3 ");
40        box3.volume();
41    }
42}