Wednesday, 9 October 2013

Program to Add two complex nos using objects passing

//Program to Add two complex nos using objects passing
//Developed by Ahlam Ansari

import java.io.*;
import java.util.*;
class ComplexNumber
{
    int real,imag;
    ComplexNumber()
    {
        System.out.println("Enter the real and imaginary parts:");
        Scanner ahlam= new Scanner(System.in);
        real=ahlam.nextInt();
        imag=ahlam.nextInt();
    }   

    ComplexNumber(ComplexNumber complexCopy)
    {
        System.out.println("The Two Complex Numbers Are:");
        real=complexCopy.real+1;
        imag=complexCopy.imag+1;
        System.out.println(complexCopy.real+"+"+complexCopy.imag+"i");
        System.out.println(real+"+"+imag+"i");
    }

    void addComplexNumbers(ComplexNumber complexCopy)
    {
        System.out.println("Addition of the Complex Numbers:");
        System.out.println((real+complexCopy.real)+ "+" + (imag+complexCopy.imag) + "i");
    }
}

class MainComplex
{
    public static void main(String ahlam[])
    {
        ComplexNumber cn1= new ComplexNumber();
        ComplexNumber cn2= new ComplexNumber(cn1);
        cn2.addComplexNumbers(cn1);
    }
}

/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MainComplex.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainComplex
Enter the real and imaginary parts:
2
3
The Two Complex Numbers Are:
2+3i
3+4i
Addition of the Complex Numbers:
5+7i

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment