Task: Write a Java program to create a new Box class in Java. Use three instance variables width, height and depth of type double. Write down two constructors. Default ( no-arg )constructor will intialize the Box class objects with 0.0 value for all instance variables. A parameterized constructor will be used to intialize the object with different values.

Write down a method called volume() that will calculate and show the volume of the box.
Write down a main class to use this Box class. Create some objects of Box class and show the functionality.
Java Source code for designing and using a new class Box
/* Write a Java program to create a new Box class in Java. */ package boxdemo; /** * * @author www.EasyCodeBook.com */ class Box { private double width; private double height; private double depth; public Box() { width=height=depth=0.0; } public Box(double w,double h,double d) { width=w; height=h; depth=d; } public void volume() { System.out.println("Volume ="+(width*height*depth)); } } public class BoxDemo { public static void main(String[] args) { Box myBox1=new Box(); Box myBox2=new Box(10,15,20); myBox1.volume(); myBox2.volume(); } }
Output of the Java program to create new class Box
Volume =0.0
Volume =3000.0
9,381 total views, 6 views today