home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap09 / MyClass / srd / math / ComplexNumber.class (.txt)
Encoding:
Java Class File  |  1996-09-03  |  1.1 KB  |  31 lines

  1. package srd.math;
  2.  
  3. class ComplexNumber {
  4.    private double m_dReal;
  5.    private double m_dImag;
  6.  
  7.    public String toString() {
  8.       StringBuffer sb = new StringBuffer();
  9.       sb.append('(').append(this.m_dReal).append(',');
  10.       sb.append(this.m_dImag).append(')');
  11.       return sb.toString();
  12.    }
  13.  
  14.    public ComplexNumber(double dR, double dI) {
  15.       this.m_dReal = dR;
  16.       this.m_dImag = dI;
  17.    }
  18.  
  19.    public ComplexNumber(double dR) {
  20.       this(dR, (double)0.0F);
  21.    }
  22.  
  23.    public ComplexNumber Divide(double d) throws Exception {
  24.       if (d == (double)0.0F) {
  25.          throw new Exception("Attempted divide by zero in ComplexNumber.divide");
  26.       } else {
  27.          return new ComplexNumber(this.m_dReal / d, this.m_dImag / d);
  28.       }
  29.    }
  30. }
  31.