home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-01 | 954 b | 43 lines |
- package srd.math;
-
- class ComplexNumber
- {
- private double m_dReal;
- private double m_dImag;
-
- // constructors
- public ComplexNumber(double dR, double dI)
- {
- m_dReal = dR;
- m_dImag = dI;
- }
-
- public ComplexNumber(double dR)
- {
- this(dR, 0.0);
- }
-
- // operators
- public ComplexNumber Add(ComplexNumber cn)
- {
- return new ComplexNumber(m_dReal + cn.m_dReal,
- m_dImag + cn.m_dImag);
- }
-
- // toString - display a complex as (r,i)
- public String toString()
- {
- // build a StringBuffer into which to build output
- StringBuffer sb = new StringBuffer();
-
- // put the real part first
- sb.append('(').append(m_dReal).append(',');
-
- // now the imaginary part
- sb.append(m_dImag).append(')');
-
- // convert the result into String
- return sb.toString();
- }
- }
-