public class ComplexNumber implements ComplexInterface { private double real,imaginary; //CONSTRUCTOR*********************************************** public ComplexNumber (double Re, double Im) { real = Re; imaginary = Im; } //************************************************************* public double getReal () { return real; } public double getImaginary () { return imaginary; } //CONJUGATE COMPLEX NUMBERS************************************ public ComplexNumber conjugate() { return new ComplexNumber (real, imaginary * (-1)); } //ADD COMPLEX NUMBERS****************************************** public ComplexNumber add (ComplexNumber cn2) { double real1 = real + cn2.getReal(); double imaginary1 = imaginary + cn2.getImaginary(); return new ComplexNumber (real1, imaginary1); } //SUBTRACT COMPLEX NUMBERS************************************* public ComplexNumber subtract (ComplexNumber cn2) { double real2 = real - cn2.getReal(); double imaginary2 = imaginary - cn2.getImaginary(); return new ComplexNumber (real2, imaginary2); } //MULTIPLY COMPLEX NUMBERS************************************* public ComplexNumber multiply (ComplexNumber cn2) { double mul1 = real * cn2.getReal() - imaginary * cn2.getImaginary(); double mul2 = real * cn2.getImaginary() + imaginary * cn2.getReal(); return new ComplexNumber (mul1, mul2); } //DIVIDE COMPLEX NUMBERS************************************** public ComplexNumber divide (ComplexNumber cn2) { double c = cn2.getconjugate(); double phase1 = real * con.getReal() - imaginary * con.getImaginary(); double phase2 = real * con.getImaginary() + imaginary * con.getReal(); double phase3 = cn2.getReal() * con.getReal() - cn2.getImginary() * con.getImaginary(); double phase4 = phase1 / phase3; double phase5 = phase2 / phase3; return new ComplexNumber (phase4, phase5); } //CONVERT FROM POLAR TO RECTANGUAR**************************** public static ComplexNumber toRect (double mag, double angle) { double realv = mag * (Math.cos(angle)); double imaginaryv = mag * (Math.sin(angle)); return new ComplexNumber (realv, imaginaryv); } //CHECK IF COMPLEX NUMBERS ARE EQUAL************************** public boolean isLike (ComplexNumber cn2) { return ( real == cn2.getReal() && imaginary == cn2.getImaginary() ); //Convert the numbers to polar******************************* private ComplexNumber toPolar (double real, double imaginary) { double magnitude = Math.sqrt(Math.pow(real, 2) + Math.pow(imaginary, 2)); double angles = Math.atan2(real, imaginary); return new ComplexNumber (magnitude, angles); } //Returns this complex number as a string********************* public String toString () { String result; if (real == 0) { if (imaginary == 0) { return "0"; } else{ if (imaginary == 0) { return String.valueOf(real); } else if (imaginary < 0) { return(real + " " + imaginary + "i"); } else { return(real + " +" + imaginary + "i"); } } }

This post has been edited by **smohd**: 01 January 2012 - 10:10 AM

Reason for edit:: Code tags added. Please use [code] tags when posting codes