Name: Viraj Tanaji Shinde
PRN No: 256209005
Batch: SB3
Branch: IT (Un-Aided)
Course: Java Programming Lab
Course code: 7IT271
—
Experiment no. 3
Program 1: Write a program with a class Person that uses a default constructor to
display a welcome message.
class Person
{
Person()
{
[Link]("WELCOME");
}
public static void main(String a[])
{
Person P=new Person();
}
}
Output:
Program 2: Create a class Box with a constructor that initializes height, width, and depth.
class Box
{
int height,width,depth;
Box(int height,int w,int d)
{
[Link]=height;
width=w;
depth=d;
[Link]("Initialise the Object");
}
public void display()
{
[Link]("Height="+height);
[Link]("Width="+width);
[Link]("Depth="+depth);
}
public static void main(String a[])
{
Box P=new Box(10,20,30);
[Link]();
}
}
Output:
Program 3: Create a class Mobile that uses both default and parameterized constructors
to initialize different objects.
class Mobile
{
long id;
Mobile(long id)
{
[Link]=id;
[Link]("Parameterized Constructor");
[Link]("ID="+id);
}
Mobile()
{
[Link]("Default");
[Link]("ID="+id);
}
public static void main(String a[])
{
Mobile P=new Mobile();
Mobile o=new Mobile(9049389944L);
}
}
Output:
Pragram 4: Write a program using a destructor to count how many objects are destroyed.
class e {
String name;
public static int count = 0;
e(String n) {
name = n;
}
void display() {
[Link]("Employee Name: " + name);
}
// This must be OUTSIDE main, but INSIDE class e
@Override
protected void finalize() {
count++;
[Link]("Finalize called! Object destroyed.");
public static void main(String[] args) {
e e1 = new e("Amit");
[Link]();
e e2 = new e("Viraj");
[Link]();
e2 = new e("Varad");
[Link]();
e1 = null;
e2=null;
// Suggesting Garbage Collection
[Link]();
// Give the GC a tiny bit of time to run (GC runs on a separate thread)
try { [Link](1000); } catch (InterruptedException ex) {}
[Link]("Objects destroyed count: " + [Link]);
}
}
Output:
Program 5: Create a class Company that dynamically allocates memory for employee
records. Ensure proper cleanup using a destructor.
import [Link];
import [Link];
class Employee {
private String name;
private int id;
public Employee(String name, int id) {
[Link] = name;
[Link] = id;
}
@Override
public String toString() {
return "Employee{name='" + name + "', id=" + id + '}';
}
}
class Company {
private List<Employee> employees;
public Company() {
[Link] = new ArrayList<>();
[Link]("Company initialized: Employee list created.");
}
public void addEmployee(String name, int id) {
[Link](new Employee(name, id));
[Link]("Added Employee: " + name);
}
public void displayEmployees() {
[Link]("\nCurrent Employee Records:");
for (Employee emp : employees) {
[Link](emp);
}
}
@Override
protected void finalize() throws Throwable {
try {
[Link]("\nFinalizer called: Resources (if any) are being released.");
} finally {
[Link]();
}
}
}
public class Main {
public static void main(String[] args) {
Company myCompany = new Company();
[Link]("Alice", 101);
[Link]("Bob", 102);
[Link]();
myCompany = null;
[Link]();
}
}
Output: