home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap09 / MyClass / ComplexNumbers.java next >
Encoding:
Java Source  |  1996-08-02  |  881 b   |  39 lines

  1. package srd.math;
  2. import java.lang.Exception;
  3.  
  4. class ComplexNumber
  5. {
  6.     private double m_dReal;
  7.     private double m_dImag;
  8.  
  9.     // constructors
  10.     public ComplexNumber(double dR, double dI)
  11.     {
  12.         m_dReal = dR;
  13.         m_dImag = dI;
  14.     }
  15.     public ComplexNumber(double dR)
  16.     {
  17.         this(dR, 0.0);
  18.     }
  19.  
  20.     // division operator written to use exceptions
  21.     public ComplexNumber Divide(double d) throws Exception
  22.     {
  23.         if (d == 0)
  24.         {
  25.             throw new 
  26.   Exception("Attempted divide by zero in ComplexNumber.divide");
  27.         }
  28.         return new ComplexNumber(m_dReal / d, m_dImag / d);
  29.     }
  30.  
  31.     public String toString()
  32.     {
  33.         StringBuffer sb = new StringBuffer();
  34.         sb.append('(').append(m_dReal).append(',');
  35.         sb.append(m_dImag).append(')');
  36.         return sb.toString();
  37.     }
  38. }
  39.