Untitled

 avatar
unknown
plain_text
2 years ago
1.2 kB
3
Indexable
class Box {
    protected double length;
    protected double breadth;
    
    public Box(double length, double breadth) {
        this.length = length;
        this.breadth = breadth;
    }
    
    public void setDimensions(double length, double breadth) {
        this.length = length;
        this.breadth = breadth;
    }
    
    public double calculateArea() {
        return length * breadth;
    }
}

class Box3D extends Box {
    protected double height;
    
    public Box3D(double length, double breadth, double height) {
        super(length, breadth);
        this.height = height;
    }
    
    public void setDimensions(double length, double breadth, double height) {
        super.setDimensions(length, breadth);
        this.height = height;
    }
    
    public double calculateVolume() {
        return length * breadth * height;
    }
}

public class Main {
    public static void main(String[] args) {
        Box3D box3d = new Box3D(5.0, 3.0, 2.0);
        
        double area = box3d.calculateArea();
        double volume = box3d.calculateVolume();
        
        System.out.println("Area: " + area);
        System.out.println("Volume: " + volume);
    }
}
Output:
Editor is loading...