home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap08 / Test / math.java next >
Encoding:
Java Source  |  1996-08-01  |  954 b   |  43 lines

  1. package srd.math;
  2.  
  3. class ComplexNumber
  4. {
  5.     private double m_dReal;
  6.     private double m_dImag;
  7.  
  8.     // constructors
  9.     public ComplexNumber(double dR, double dI)
  10.     {
  11.         m_dReal = dR;
  12.         m_dImag = dI;
  13.     }
  14.  
  15.     public ComplexNumber(double dR)
  16.     {
  17.         this(dR, 0.0);
  18.     }
  19.  
  20.     // operators
  21.     public ComplexNumber Add(ComplexNumber cn)
  22.     {
  23.         return new ComplexNumber(m_dReal + cn.m_dReal,
  24.                                   m_dImag + cn.m_dImag);
  25.     }
  26.  
  27.     // toString - display a complex as (r,i)
  28.     public String toString()
  29.     {
  30.         // build a StringBuffer into which to build output
  31.         StringBuffer sb = new StringBuffer();
  32.  
  33.         // put the real part first
  34.         sb.append('(').append(m_dReal).append(',');
  35.  
  36.         // now the imaginary part
  37.         sb.append(m_dImag).append(')');
  38.  
  39.         // convert the result into String
  40.         return sb.toString();
  41.     }
  42. }
  43.