0% found this document useful (0 votes)
8 views3 pages

Experiment 7 24-25 Final

The document outlines Java laboratory experiments for the academic year 2024-25, focusing on constructors and constructor overloading. It includes code examples for counting object instances and calculating the area of squares and rectangles using different types of constructors. The document also provides outputs for the implemented code demonstrating the functionality of the classes created.

Uploaded by

Mohammad Shaikh
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)
8 views3 pages

Experiment 7 24-25 Final

The document outlines Java laboratory experiments for the academic year 2024-25, focusing on constructors and constructor overloading. It includes code examples for counting object instances and calculating the area of squares and rectangles using different types of constructors. The document also provides outputs for the implemented code demonstrating the functionality of the classes created.

Uploaded by

Mohammad Shaikh
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/ 3

Object Oriented Programming using Java Laboratory (DJS23FLES201)

Academic Year 2024-25

Name: Dhanya Dedhia

SAP ID : 60003240139

RollNO: I132

Batch: I3-1

Java Experiment 7

7. To implement Constructors and constructor overloading


a. WAOOP to count the no. of objects created of a class using constructors.
Code:

public class ObjectCounter {


private static int objectCount = 0;

public ObjectCounter() {
objectCount++;
}

public static int getObjectCount() {


return objectCount;
}

public static void main(String[] args) {


ObjectCounter obj1 = new ObjectCounter();
ObjectCounter obj2 = new ObjectCounter();
ObjectCounter obj3 = new ObjectCounter();

System.out.println("Number of objects created: " + ObjectCounter.getObjectCount());


}
}

Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

ctCount++

b. WAP to display area of square and rectangle using the concept of overloaded constructor
(use parameterized, non-parameterized and copy constructor).

Code:

class Shape {
double area;

public Shape() {
area = 0.0;
}

public Shape(double side) {


area = side * side;
}

public Shape(double length, double width) {


area = length * width;
}

public Shape(Shape original) {


area = original.area;
}

public void displayArea() {


System.out.println("Area: " + area);
}
}

public class Area{


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

public static void main(String[] args) {


Shape square1 = new Shape();
System.out.println("Square 1:");
square1.displayArea();

Shape square2 = new Shape(8.0);


System.out.println("\nSquare 2:");
square2.displayArea();

Shape rectangle1 = new Shape(5.0, 9.0);


System.out.println("\nRectangle 1:");
rectangle1.displayArea();

Shape rectangle2 = new Shape(rectangle1);


System.out.println("\nRectangle 2 (copy of Rectangle 1):");
rectangle2.displayArea();
}
}

Output:

You might also like