0% found this document useful (0 votes)
27 views

Worksheet-EXP 1 JAVA

The document describes an experiment to study default constructors in Java. It defines a Calc class with integer variables and different constructors. The main method creates Calc objects using the default, parameterized, and copy constructors and displays the values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Worksheet-EXP 1 JAVA

The document describes an experiment to study default constructors in Java. It defines a Calc class with integer variables and different constructors. The main method creates Calc objects using the default, parameterized, and copy constructors and displays the values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT – 1

Student Name: UTSAV VASHIST


UID: 20BCS9452
Branch: CSE Section/Group : 41-A
Semester: 03
Date of Performance:12-09-2021
Subject Name : JAVA PROGRAMMING LAB
Subject Code: 20CSP-219

PRACTICAL 1.1

1. Aim/Overview of the practical:

Write a program to study Default Constructor in Java.

2. Task to be done/ Which logistics used:

About default constructor default constructor if there is no constructor available in the


class. In such case, Java compiler provides a default constructor by default.

3. Algorithm/Flowchart (For programming based labs):

1. Start
2. Declare a class named Calc()
3. Inside the class
4. Declare an integer variable len
5. Declare an integer variable br
4. Print the value of len and br by create an object.
5. Call method is used to call the constructor in main function and print the value .
6. END

4. Steps for experiment/practical/Code:

class Calc{
int len;
int br;
Calc()
{
System.out.println("This is a default constructor.");
}
Calc(int len, int br)
{
this.len= len;
this.br= br;
}
Calc(Calc C)
{
len=C.len;
br=C.br;
}
void display()
{
System.out.println("Length: "+" " +len);
System.out.println("Breadth: "+" " +br);
}
}
public class Main {
public static void main(String[] args)
{
//Default Constructor
Calc C1= new Calc();
//Parameterized Constructor
Calc C2= new Calc(2,5);
//Constructor passed as an argument
Calc C3= new Calc(C1);
Calc C4= new Calc(C2);
//display default constructor
C3.display();
//display parameterized constructor
System.out.println("This is a parameterized constructor.");
C4.display();
}
}

5. Observations/Discussions/ Complexity Analysis:

You might also like