Home : Course Map : Chapter 5 : Java : Tech :
An Immutable Complex Number Class
JavaTech
Course Map
Chapter 5
File/Package/Import
  Demo 1
  Demo 2
Access/Visibility
final & Constants
Static Import
Jar Files
  Demo 3
Applet Directories
3rd Party Packages
CLASSPATH
javadoc
CodingConventions
Exercises

    Supplements
Scope
Debug Techniques
Java Runtime
Class Class
JVM Instructions 2
JVM Processing
pack200

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

In the Complex class discussed in Chapter 4: Tech, the two instance variables for the real and imaginary parts are directly accessible to other classes.

In Chapter 5: Java we discuss access modifiers like "public" and "private". These allow you to control access to fields and methods by other classes and subclasses in the same or different packages.

In the ComplexImm class, shown below, we make the instance variables private and also we do not allow any of the methods to alter the variables after they are set by the constructor. Instances of such a class are said to be immutable because they are unchangeable once created. The getReal() and getImg() methods allow other classes to obtain the values of the real and imaginary parts, respectively.

Immutable classes can provide an elegant approach to the creation of objects in some cases. For example, instances of String are immutable. The append operations always create a new string object. We will also see in Chapter 10: Tech that the BigInteger and BigDecimal classes in the java.math core language package are immutable.

The disadvantage of such an approach is that there can be some performance tradeoffs since access to the variables must involve method invocations and you must also create and store new objects for every new value. In a fractal example in Chapter 11, we use the Complex class since the intensive processing involves lots of complex number crunching.

 

ComplexImm.java

/** This is a complex number class with immutable values, i.e.
  * the real and img parts cannot be modified once created. The
  * "Imm" is appended to "Complex" to differentiate this class
  * from the Complex class.
  *
  * Most of the common complex number operations are provided
  * as static methods. New instances of ComplexImm are created
  * by the operations.
  *
 **/

public class ComplexImm
{
  private double fReal;
  private double fImg;


  /** Constructor that initializes the values. **/
  ComplexImm  (double r, double i) {
    fReal = r; fImg = i;
  } // ctor


  /** Get methods for real & imaginary parts. **/
  public double getReal () {
    return fReal;
  } // getReal

  public double getImg () {
    return fImg;
  } // getImg


  /**
    * Provide the magnitude of the complex value.
   **/
  public double modulus () {
    return Math.sqrt (fReal*fReal + fImg*fImg);
  } // modulus


  /**
    * Check for the equality of this object with that of the argument.
   **/
  public boolean equals (ComplexImm cvalue) {
    return ( (fReal == cvalue.getReal ()) &&
             (fImg  == cvalue.getImg ()) ) ;
  } // equals


  /**
    * Define a static add method that creates a
    * a new ComplexImm object with the sum.
   **/
  public static ComplexImm add (ComplexImm cvalue1,
                                ComplexImm cvalue2) {
    double r = cvalue1.getReal () + cvalue2.getReal ();
    double i = cvalue1.getImg ()  + cvalue2.getImg ();
    return new ComplexImm (r,i);
  } // add


  /** Define a static subtract method that creates a
    * a new Complex object equal to
    *
    *  cvalue1 - cvalue2.
   **/
  public static ComplexImm subtract (ComplexImm cvalue1,
                                     ComplexImm cvalue2) {
    double r = cvalue1.getReal () - cvalue2.getReal ();
    double i = cvalue1.getImg ()  - cvalue2.getImg ();
    return new ComplexImm (r,i);
  } // subtract


  /** Define a static multiply method that creates a
    * a new ComplexImm object with the product.
   **/
  public static ComplexImm multiply (ComplexImm cvalue1,
                                     ComplexImm cvalue2) {
    double r2 = cvalue1.getReal () * cvalue2.getReal ();
    double i2 = cvalue1.getImg ()  * cvalue2.getImg ();
    return new ComplexImm (r2-i2, r2+i2);
  } // multiply


  /** Define a static divide method that creates a
    * a new ComplexImm object with the result of
    *
    *  cvalue1/cvalue2.
   **/
  public static ComplexImm divide (ComplexImm cvalue1,
                                   ComplexImm cvalue2) {
    double denom = cvalue2.getReal () * cvalue2.getReal () +
                   cvalue2.getImg ()  * cvalue2.getImg ();

    double r = cvalue1.getReal () * cvalue2.getReal () +
               cvalue1.getImg ()  * cvalue2.getImg ();

    double i = cvalue1.getImg () * cvalue2.getReal () +
               cvalue1.getReal () * cvalue2.getImg ();

    return new ComplexImm (r/denom, i/denom);
  } // divide

  /** Return a string representation of the complex value. **/
  public String toString () {
    String imgSign = (fImg < 0) ? " - " : " + ";
    return (fReal +  imgSign + fImg + "i");
  } // toString

} // ComplexImm

 

 

References & Resources

Most recent update: June 14, 2005

            Tech
DecimalFormat
  Demo 1
  Demo 2

System.out.printf
  Demo 3
CJ Format Class
  Demo 4   Demo 5
Other Format Tools

Immutable Complex
Exercises

           Physics
Interpolation
Integration
  Demo 1
Simpson Rule
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.