import java.util.*; import java.io.*; import java.lang.Math; class complextest { float re; float im; static float temp; /* Only one temp no matter how many instances of the class. */ /* Main method for application. */ public static void main(String args[]) { /* Instantiate the complex structure variables */ complextest z = new complextest(0F, 0F); /* Assign values to the complex numbers z1 and z2 */ /* 1 + 0i */ complextest z1 = new complextest(1F, 0F); /* 0 + 1i */ complextest z2 = new complextest(0F, 1F); /* Print z1 and z2 */ System.out.println("z1, z2: " + z1.re + "+" + z1.im + "i, " + z2.re + "+" + z2.im + "i"); /* Invoke the complextest.mul(z1,z2) method to get z1 times z2 and put the product in z */ z.mul(z1,z2); /* Print z1*z2 */ System.out.println("z1*z2: " + z.re + "+" + z.im + "i"); } /* Methods for class complextest objects. "this" means the object that invoked the method. */ void mul(complextest x, complextest y) { temp = (x.re * y.re - x.im * y.im); /* The temporary variable keeps the new real component from messing up the imaginary calculation if this is also x or y. */ this.im = (x.im * y.re + x.re * y.im); this.re = temp; } void conjugate_mul(complextest x, complextest y) { temp = (x.im * y.re + x.re * y.im); this.im = (x.im * y.re - x.re * y.im); this.re = temp; } void div(complextest x, complextest y) { temp = ((x.re * y.re + x.im * y.im)/(y.re * y.re + y.im * y.im)); this.im = ((x.im * y.re - x.re * y.im)/(y.re * y.re + y.im * y.im)); this.re = temp; } /* Constructor */ complextest(float re, float im) { this.re = re; this.im = im; } }