Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Rushi Java

The document defines a Complex class that represents complex numbers with real and imaginary parts. It includes a default constructor, a parameterized constructor, a copy constructor, a method to add two complex numbers, and a method to display the complex number. The main method demonstrates the creation of complex numbers and their addition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Rushi Java

The document defines a Complex class that represents complex numbers with real and imaginary parts. It includes a default constructor, a parameterized constructor, a copy constructor, a method to add two complex numbers, and a method to display the complex number. The main method demonstrates the creation of complex numbers and their addition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

class Complex {

private int real;

private int imaginary;

// Default Constructor

public Complex() {

this.real = 0;

this.imaginary = 0;

// Parameterized Constructor

public Complex(int real, int imaginary) {

this.real = real;

this.imaginary = imaginary;

// Copy Constructor

public Complex(Complex c) {

this.real = c.real;

this.imaginary = c.imaginary;

// Method to add two complex numbers

public Complex add(Complex c) {

return new Complex(this.real + c.real, this.imaginary + c.imaginary);

// Method to display the complex number

public void display() {

System.out.println(real + " + " + imaginary + "i");

public static void main(String[] args) {

Complex c1 = new Complex(3, 4); // Using Parameterized Constructor

Complex c2 = new Complex(5, 6); // Using Parameterized Constructor


Complex c3 = new Complex(); // Using Default Constructor

Complex sum = c1.add(c2); // Performing addition

System.out.println("First Complex Number:");

c1.display();

System.out.println("Second Complex Number:");

c2.display();

System.out.println("Sum of Complex Numbers:");

sum.display();

You might also like