The example below consists of two files.

The program for testing complex numbers: ComplexDriver.java


// this is a test driver program for the class Complex

public class ComplexDriver {

    public static void main (String [] args) {
	Complex cm1 = new Complex (3.5, 4.5);
	Complex cm2 = new Complex ();

	System.out.println("cm1 = " + cm1);
	System.out.println("cm2 = " + cm2);

	// integers are converted to doubles:
	Complex cm3 = new Complex(4,5);
	System.out.println("cm3 = " + cm3);

	cm2.setIm(1.0);
	System.out.println(cm2);

	cm3 = cm1.add(cm2);
	System.out.println(cm3);

	// multiply c2 by i:
	cm3 = cm1.mult(cm2);
	System.out.println("Multiply cm2 by i:");
	System.out.println("cm3 = " + cm3);
	

	// divide cm3 by i. Should get back 3.5 + 4.5i
	System.out.println("Multiply cm3 by i:");
	cm1 = cm3.div(cm2);
	System.out.println("cm1 = " + cm1);

	// add test code for the 3 new methods
    }

}
The file with the definition of Complex class:

// the class Complex defines complex numbers
// and operations on these numbers

public class Complex {
    private double re; // real part of the complex number
    private double im; // imaginary part of the complex number

    // a constructor with two initial values
    public Complex(double r, double i) {
	re = r;
	im = i;
    }

    // a constructor that sets re and im to 0.0
    public Complex() {
	// do nothing, re and im are initialized
	// to 0.0 by default
    }

    // resets the real part of the complex number
    public void setRe(double r) {
	re = r;
    }

    // resets the imaginary part of the complex number
    public void setIm(double i) {
	im = i;
    }

    // returns the value of the real part
    public double getRe() {
	return re;
    }

    // returns the value of the imaginary part
    public double getIm() {
	return im;
    }

    // adding this number and c, returning the
    // result as a new complex number.
    // this numner is not changed
    public Complex add(Complex c) {
	// this number can access private variables of c 
	// since they are in the same class
	return new Complex(re + c.re, im + c.im);
    }

    // returns the new complex number which is this number - c
    public Complex subtr(Complex c) {
	return new Complex(re - c.re, im - c.im);
    }

    public Complex mult(Complex c) {
	double newre = re * c.re - im * c.im;
	double newim = re * c.im + im * c.re;
	return new Complex(newre, newim);
    }  

    // returns the new complex number which is this number divided by c
    public Complex div(Complex c) {
	double numerator = c.re * c.re + c.im * c.im;
	double newre = (re * c.re + im * c.im) / numerator;
	double newim = (re * c.im - im * c.re) / numerator;
	return new Complex(newre, newim);
    }

    // add methods equals, absvalue, isReal:

    // equals compares this number to the parameter. 
    // Returns true if the two numbers have the same value,
    // false otherwise

    // returns the absolute value of this number: square root
    // of the sum of the square of the real part and the square of 
    // imaginary part

    // isReal returns true if the imaginary part is 0,
    // false otherwise

    // returns a string with the value of the complex number
    public String toString() {
	String str = new String ("complex number: re = " + re + 
				 " im = " + im);
	return str;
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.