Home : Course Map : Chapter 3 : Java : Tech :
Complex Number Class
JavaTech
Course Map
Chapter 3

Introduction
Class Definition
Data Fields
Methods
Constructors
Instantiation
  
Demo 1
  Demo 2
Static Members
  Demo 3

Value&Reference
  Demo 4
Overloading
   Demo 5
Wrappers 
  Demo 6
Autobox/Unbox
   Demo 7
Arrays
  Demo 8
Exceptions
Exercises

    Supplements
MoreAboutObjects
Creating Types

Classes,Objs&JVM

Java OOP vs C++
Garbage Collection
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Fortran includes a complex number type but Java unfortunately does not. We can, though, create a complex class of our own. A class type will not perform at the speed of a primitive type but should otherwise work just as well.

A complex number needs two memory locations reserved for the real and imaginary parts and it must provide methods to perform operations on these values and with the values of other instances of the complex number class.

Here is a very simple complex (an oxymoronic description!) number class:

BasicComplex.java

public class BasicComplex
{

  //Properties
  double real;
  double img;

  // Constructor that initializes 
  // the values

  BasicComplex (double r, double i)
  { real = r; img = i; }

  // Define an add method
  public void add (BasicComplex cvalue) {
    real = real + cvalue.real;
    img  = img  + cvalue.img;
  }

  // Define a subtract method
  public void subtract (BasicComplex cvalue) {
    real = real - cvalue.real;
    img  = img  - cvalue.img;
  }
}

The BasicComplex class holds the real and imaginary parts in two double type instance variables. Since this class is only for demonstration purposes, it includes just two methods. An instance of the class can add/subtract another instance of the class to/from itself. (A practical complex number class would obviously need methods to provide the absolute value of the complex number, multiplication, division, etc.).

We could use such a class as follows where we create 2 instances of our complex class and then add them together:

public class complexTest {
  static public void main (String [] args)  {
   // Create complex objects
   BasicComplex a = new BasicComplex (1,2);
   BasicComplex b = new BasicComplex (3,1);

   a.add(b); // a now holds a+b
     ...

  }
}

 

This class is very limited. In Chapter 4: Tech : Complex Class we will add more features to a complex class, taking advantage of class features such as overloading of methods.

However, this example does illustrate some of the basic concepts of classes: how classes relate to data types, and how classes can be used in mathematical applications.

References and Web Resources

 

Latest update: June 14, 2005

           Tech
OOP in Tech Apps
Complex Number
Histogram Class
  Demo
More Wrappers

           Physics
OOP in Physics
Particle Class
Root Finding
  Demo 1
Newton Methods
  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.