Padre Conceição College Of Engineering
DATE: 05/01/2025 EXPERIMENT NO: 03
AIM: Java program to implement constructor.
THEORY:
Constructor:These are special methods used to intialise objects.
Default Constructor(no argument):The default constructor in Java is a constructor that does not
take any parameters and initializes an object with default values.
Syntax of Default Constructor
class ClassName {
// Default constructor
ClassName() {
// Initialization code
}
}
Parameterized Constructor (Single Argument):A parameterized constructor is a constructor that
takes arguments to initialize an object with specific values.
Syntax
class ClassName {
// Parameterized constructor
ClassName(datatype param1, datatype param2, ...) {
// Initialization code
}
}
Method overloading is demonstrated with three constructors.
DEPARTMENT OF ELECTRONICS AND COMPUTER ENGINEERING, JAVA PROGRAMMING
PAGE NO. : 1 ROLL NO. : 23EC20
Padre Conceição College Of Engineering
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)
DEPARTMENT OF ELECTRONICS AND COMPUTER ENGINEERING, JAVA PROGRAMMING
PAGE NO. : 2 ROLL NO. : 23EC20
Padre Conceição College Of Engineering
{
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();
}
}
DEPARTMENT OF ELECTRONICS AND COMPUTER ENGINEERING, JAVA PROGRAMMING
PAGE NO. : 3 ROLL NO. : 23EC20
Padre Conceição College Of Engineering
OUTPUT:
CONCLUSISON: Java Program to Implement Constructor is studied and implemented successfully.
DEPARTMENT OF ELECTRONICS AND COMPUTER ENGINEERING, JAVA PROGRAMMING
PAGE NO. : 4 ROLL NO. : 23EC20