A Non-Trivial Examples: Complex Numbers

As mentioned in Chapter 2 one of the features needed for serious scientific computation is complex numbers. Unfortunately no popular computer language other than Fortran provides them as a built-in data type. (Actually this is such a common and useful example and was used by so many textbooks that it was recently added to the C++ standard library which makes it far less useful as an example. Fortunately, however, Java has not yet been around long enough to have all its really useful examples coopted into the standard library.) Let's see how we might implement them in Java. From the standpoint of a data type you really don't need much. Mathematically a complex number is composed of a real part u and an imaginary part v. We can create such a class in the following way:

public class ComplexNumber extends Object { public double u; public double v; } While this is sufficient to encompass all the data that one needs in a complex number it's not a very good example of object-oriented programming. To actually do anything with this number we have to know exactly how the data structure is defined. If we change the data structure, for instance by defining a complex number in terms of it's magnitude r and its argument theta instead of by its real and imaginary components we have to change all the code that depends on it.

We also have to write code to explicitly add the numbers, multiply them or do anything else we might need to do with complex numbers. If we need to add complex numbers in more than one place, then we need to write the addition code again, or, at the very least, copy and paste it.

A better implementation of a complex number class will shield us from the exact storage of the data, i.e. x and y vs. r and theta. It will also provide methods that let us perform any operation we might need to perform on or with a complex number.

Before writing code we need to ask ourselves what we'll do with a complex number. Most objects first require a constructor , a method that is called when you create a new complex number. A more complicated object may also require a destructor method that's called when you get rid of an object; but since this is a fairly simple object, we'll let Java's built-in garbage collection take care of that for us.

Since these are complex numbers it's not unlikely that we'll need to add them, subtract them, multiply them and divide them. We'll also want to be able to access their real and imaginary parts as well as their absolute values and arguments. The following class does all that.

// public class Complex extends Object { private double u; private double v; Complex (double x, double y) { u=x; v=y; } public double Real () { return u; } public double Imaginary () { return v; } public double Magnitude () { return Math.sqrt(u*u + v*v); } public double Arg () { return Math.atan2(v, u); } // Add z to w; i.e. w += z public Complex Plus (Complex z) { return new Complex(u + z.u, v + z.v); } // Subtract z from w public Complex Minus (Complex z) { return new Complex(u - z.u, v - z.v); } public Complex Times (Complex z) { return new Complex(u*z.u - v*z.v, u*z.v + v*z.u); } // divide w by z public Complex DivideBy (Complex z) { double rz = z.Magnitude(); return new Complex((u * z.u + v * z.v)/(rz*rz),(v * z.u - u * z.v)/(rz*rz)); } } Notice especially that x and y are now private. They cannot be accessed by external code even if we want them to be.

The use of one of these methods will look like the following. Add the following ComplexExamples class to the Complex.java file and compile. Then run ComplexExamples in the usual way by typing java ComplexExamples .

//Complex Arithmetic Examples class ComplexExamples { public static void main (String args[]) { Complex u, v, w, z; u = new Complex(1,2); System.out.println("u: + u.Real() + + + u.Imaginary() + "i); v = new Complex(3,-4.5); System.out.println("v: + v.Real() + + + v.Imaginary() + "i); // Add u + v; z=u.Plus(v); System.out.println("u + v: + z.Real() + + + z.Imaginary() + "i); // Add v + u; z=v.Plus(u); System.out.println("v + u: + z.Real() + + + z.Imaginary() + "i); z=u.Minus(v); System.out.println("u - v: + z.Real() + + + z.Imaginary() + "i); z=v.Minus(u); System.out.println("v - u: + z.Real() + + + z.Imaginary() + "i); z=u.Times(v); System.out.println("u * v: + z.Real() + + + z.Imaginary() + "i); z=v.Times(u); System.out.println("v * u: + z.Real() + + + z.Imaginary() + "i); z=u.DivideBy(v); System.out.println("u / v: + z.Real() + + + z.Imaginary() + "i); z=v.DivideBy(u); System.out.println("v / u: + z.Real() + + + z.Imaginary() + "i); } }

Bhopal news
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india