Java Exp 3 Print
Java Exp 3 Print
THEORY:
class ClassName {
// Default constructor
ClassName() {
// Initialization code
}
}
Syntax
class ClassName {
// Parameterized constructor
ClassName(datatype param1, datatype param2, ...) {
// Initialization code
}
}
Method overloading is demonstrated with three constructors.
SOURCE CODE:
import java.util.*;
public class Boxx {
double width, length, height;
public Boxx()
{
width = 20;
height = 40;
length = 60;
}
public Boxx(double side)
{
length = width = height = side;
}
public Boxx (double w, double h, double l)
{
width = w;
height = h;
length = l;
}
void volume()
{
double vol ;
vol = length * width * height;
System.out.println("Volume of the Box : "+vol);
}
}
class Main
{
public static void main (String[]args)
{
double side,w,h,l;
Boxx obj= new Boxx();
obj.volume();
Scanner obj1 = new Scanner (System.in);
System.out.println("Enter side of the cube: ");
side=obj1.nextDouble();
Boxx obj2= new Boxx();
obj2.volume();
System .out.println("Enter width, height, length of the cube");
w=obj1.nextDouble();
h=obj1.nextDouble();
l=obj1.nextDouble();
Boxx obj3= new Boxx(w,h,l);
obj3.volume();
}
}
OUTPUT: