0% found this document useful (0 votes)
6 views4 pages

Practical 07

This lab manual focuses on Java constructors, detailing the implementation of default, parameterized, and copy constructors through practical examples. It explains constructor overloading and the conditions under which the default constructor is provided by the system. Additionally, it includes a program for adding complex numbers using different types of constructors.

Uploaded by

Huzeefa Pathan
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)
6 views4 pages

Practical 07

This lab manual focuses on Java constructors, detailing the implementation of default, parameterized, and copy constructors through practical examples. It explains constructor overloading and the conditions under which the default constructor is provided by the system. Additionally, it includes a program for adding complex numbers using different types of constructors.

Uploaded by

Huzeefa Pathan
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

Java Programming Lab Manual (Solved)

Practical:07
Resource required (additional)
Name of Resource Broad Specification Quantity Remark if any
Java Development Kit
JDK 8 or higher For Java compilation
(JDK)
Integrated
Development like Eclipse, VS code
Environment (IDE)
For command-line
Notepad Text editor
programs

Conclusion
In this practical, we developed a program to implement different types of constructors. We
explored the use of default constructors, parameterized constructors, and copy constructors
in Java.

Practical related Questions

1. Demonstrate use of at least two types of constructors.

public class ConstDemo {


// Default constructor
public ConstDemo() {
System.out.println("This is the default constructor.");
}

// Parameterized constructor
public ConstDemo(String message) {
System.out.println("Message: " + message);
}

public static void main(String[] args) {


// Using default constructor
ConstDemo obj1 = new ConstDemo();

Jamia Polytechnic Akkalkuwa Page No:1 Prepared by: Sayyed Waliullah


Java Programming Lab Manual (Solved)

// Using parameterized constructor


ConstDemo obj2 = new ConstDemo("Hello, Constructor!");
}
}

2. How constructor overloading can be done?


• Constructor overloading in Java is achieved by defining multiple constructors in the same
class with the same name but different parameter lists. Each constructor can have a
different number or type of parameters, allowing you to initialize objects in different
ways.
• If you define any constructor (whether with or without parameters), and you do not
explicitly define a no-argument constructor, the system does not provide a default
constructor. However, if you want a default constructor along with another constructor,
you need to define it explicitly yourself.

3. Specify the situation when the default constructor is provided by the system.
• If you do not define any constructor in a class, the Java compiler automatically provides a
default constructor that takes no arguments and performs no special initialization.
• The default constructor is not provided if you have explicitly defined any constructor
(whether parameterized or not) in the class. If you need a no-argument constructor in
addition to other constructors, you must define it yourself.

4. What is the use of constructor in java?


The uses of constructors in Java:
1. Object Initialization:
Initializes the object's fields when it is created.
2. Constructor Overloading:
Allows multiple constructors with different parameter lists to create objects in different
ways.
3. Enforcing Object Creation:
Ensures objects are created with valid initial states.
4. No-Argument Constructor:
Provides a default constructor when no constructor is defined, allowing basic object
creation.

Jamia Polytechnic Akkalkuwa Page No:2 Prepared by: Sayyed Waliullah


Java Programming Lab Manual (Solved)

5. Write a program to implement different types of constructors to perform addition of


complex numbers?

public class Complex {


// Fields for real and imaginary parts
private int real, imaginary;

// Default constructor (no-argument constructor)


public Complex() {
real = 0;
imaginary = 0;
}

// Parameterized constructor
public Complex(int real, int imaginary) {
this.real = real;
this.imaginary = imaginary;
}

// Method to display complex number


public void display() {
if (imaginary < 0) {
System.out.println(real + " - " + Math.abs(imaginary) + "i");
} else {
System.out.println(real + " + " + imaginary + "i");
}
}

// Method to add two complex numbers


public Complex add(Complex c) {
// Adding real and imaginary parts separately
return new Complex(this.real + c.real, this.imaginary + c.imaginary);
}

public static void main(String[] args) {


// Using default constructor

Jamia Polytechnic Akkalkuwa Page No:3 Prepared by: Sayyed Waliullah


Java Programming Lab Manual (Solved)

Complex c1 = new Complex(); // Default complex number (0 + 0i)

// Using parameterized constructor


Complex c2 = new Complex(3, 4); // Complex number (3 + 4i)
Complex c3 = new Complex(5, 6); // Complex number (5 + 6i)

// Displaying complex numbers


System.out.print("First complex number: ");
c2.display();
System.out.print("Second complex number: ");
c3.display();

// Adding the complex numbers


Complex result = c2.add(c3);
System.out.print("Sum of complex numbers: ");
result.display();
}
}

Jamia Polytechnic Akkalkuwa Page No:4 Prepared by: Sayyed Waliullah

You might also like