0% found this document useful (0 votes)
22 views2 pages

Ass 1 Prac 1

Uploaded by

nawazsayyad62it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Ass 1 Prac 1

Uploaded by

nawazsayyad62it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Problem Statement: Design a class ‘Complex ‘with data

members for real and imaginary part. Provide default and


Parameterized constructors. Write a program to perform
arithmetic operations of two complex numbers.

class Complex {
private double real;
private double imag;
// Constructor to initialize complex number
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
// Method to add two complex numbers
public Complex add(Complex other) {
return new Complex(this.real + other.real, this.imag +
other.imag);
}
// Method to display the complex number
public void display() {
System.out.println(this.real + " + " + this.imag + "i");
}

public static void main(String[] args) {


// Creating complex number objects
Complex c1 = new Complex(2.5, 3.5);
Complex c2 = new Complex(1.6, 2.7);
// Adding two complex numbers
Complex c3 = c1.add(c2);
// Displaying the result
System.out.print("The sum of ");
c1.display();
System.out.print("and ");
c2.display();
System.out.print("is ");
c3.display();
}
}

OUTPUT:

Conclusion :
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________

You might also like