I dont know. I figured someone would find a use for it. I wrote it for a friend for his java class. Thought it was decent bit of code and that someone else could use it sometime.
edit: tried to fix the way some of the indenting and such looks when posted....failed completelyCode:// complex number calc by: M.N.Chur // please feel free to use this code for your own ends but please give credit where it is due. package program9; import java.io.*; import java.lang.Math; import java.lang.StringBuffer; public class Main { /** Creates a new instance of Main */ public Main() { } /** * @param args the command line arguments */ public class Complex { private double real; // the real number part private double imaginary; // the imaginary part Complex(double rr, double ii) { // Contruct a Complex real = rr; imaginary = ii; Complex a = new Complex(1,2); Complex b = new Complex(3,4); Complex c = new Complex(5,6); } } public String toString(){ // display the current Complex as a String, for use in println() double rr = 0; // Instantiate rr variable double ii = 0; StringBuffer sb = new StringBuffer().append(rr); if (ii>0) sb.append('+'); // else append(i) return sb.append(ii).append('i').toString(); } public double getReal() { // return the real part of equation return rr; } public double getImaginary() { //return the real part return ii; } public double magnitude() { // return the sq root of Complex number return Math.sqrt(r*r + i*i); } public Complex add(Complex other) { // adds another complex number to this one return add(this, other); } // Add two complexes public static Complex add(Complex c1,Complex c2) { return new Complex(c1.real + c2.real,c1.imaginary + c2.imaginary); } // Subtract two Complexes public Complex subract(Complex other){ return subtract(this, other); } public static Complex subtract(Complex c1, Complex c2) { return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary); } // Multiply two Complexes public Complex multiply( Complex other) { return multiply(this, other); } public static Complex multiply(Complex c1, Complex c2){ return new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary, c1.real * c2.imaginary + c1.imaginary * c2.real); } // Divide two Complexes public static Complex divide(Complex other) { return divide(this, other); } public static Complex divide(Complex c1,Complex c2) { return new Complex ((c1.real*c2.real + c1.imaginary * c2.imaginary )/ (c2.real * c2.real + c2.imaginary * c2.imaginary), (c1.imaginary * c2.real - c1.real * c2.imaginary)/ (c2.real * c2.real + c2.imaginary * c2.imaginary)); } public static void main(String[] args) { // TODO code application logic here Complex c = new Complex(3,5); Complex d = new Complex(2,-2); System.out.println(c); System.out.println(c + ".getReal() = " + c.getReal()); System.out.println(c + " + " + d + " = " + c.add(d)); System.out.println(c + " + " + d + " = " + Complex.add(c, d)); System.out.println(c + " * " + d + " = " + c.multiply(d)); System.out.println(Complex.divide(c, d)); } }


Reply With Quote