What follows is the highlighted source code with comments. You can also download this file directly: ComplexDemo.java.

/* * This work by W. Patrick Hooper is free of known copyright restrictions. * The work is in the public domain. * * Author's website: <a href="http://wphooper.com">http://wphooper.com</a>. */ // This allows us to use the Complex numbers class we created. import number.Complex; /** This class was created to demonstrate the use of the Complex Numbers class. * * @author W. Patrick Hooper */ public class ComplexDemo { /** This is the function called when the demo is run. * We will not use the command line arguments. * * @param args the command line arguments */ public static void main(String[] args) { // First lets build two complex numbers. Complex a=new Complex(1.41235123,2.3145432), b=new Complex(3,4); // Lets tell the user what these numbers are. System.out.println("I created two complex numbers: a="+a+" and b="+b+"."); // Now lets convince the user that we can do some mathematics. System.out.println("The absolute values of these numbers are |a|="+a.abs()+" and |b|="+b.abs()+"."); System.out.println("The sum of these numbers is a+b="+a.add(b)+"."); System.out.println("The difference between these numbers is a-b="+a.minus(b)+"."); System.out.println("The product of these numbers is a*b="+a.mult(b)+"."); System.out.println("The ratio of these numbers is a/b="+a.div(b)+"."); System.out.println("Here is a sanity check (b/a)*a-b="+b.div(a).mult(a).minus(b) +"."); Complex z = Complex.fromPolar(1,2*Math.PI/7); System.out.println("A primitive 7th root of unity is "+z+"."); } }

- View more source code for the ComplexProject project.
- Return to the Java Tutorial.

HOOPER >>>>> JAVA TUTORIAL