1 10 11 package math; 12 13 17 public class RealComplexINumber implements INumber { 18 19 private double a; 20 private double b; 21 22 23 public RealComplexINumber() { 24 this(0, 0); 25 } 26 27 28 public RealComplexINumber(double real) { 29 this(real, 0); 30 } 31 32 35 public RealComplexINumber(double real, double im) { 36 a = real; 37 b = im; 38 } 39 40 43 public INumber add(INumber no) { 44 RealComplexINumber cn = getComplexINumber(no); 45 return new RealComplexINumber(a + cn.a, b + cn.b); 46 } 47 48 51 public INumber subtract(INumber no) { 52 RealComplexINumber cn = getComplexINumber(no); 53 return new RealComplexINumber(a - cn.a, b - cn.b); 54 } 55 56 59 public INumber multiply(INumber no) { 60 RealComplexINumber cn = getComplexINumber(no); 61 double rp = a * cn.a - b * cn.b; 62 double ip = a * cn.b - b * cn.a; 63 return new RealComplexINumber(rp, ip); 64 } 65 66 public INumber min(INumber no) { 67 throw new UndefinedOperationException(); 68 } 69 70 public INumber max(INumber no) { 71 throw new UndefinedOperationException(); 72 } 73 74 77 public INumber divide(INumber no) { 78 RealComplexINumber cn = getComplexINumber(no); 79 if (cn.a + cn.b == 0) { 80 throw new UndefinedOperationException(); 81 } 82 return null; 84 } 85 86 89 public INumber negate() { 90 return new RealComplexINumber(-1 * a, -1 * b); 91 } 92 93 public boolean isReal() { 94 return b == 0; 95 } 96 97 public boolean isDecimal() { 98 return false; 99 } 100 101 104 public INumber abs() { 105 return new RealComplexINumber(absAsRealNumber(), 0); 106 } 107 108 public double absAsRealNumber() { 109 return Math.hypot(a, b); 110 } 111 112 public INumber sdruzene() { 113 return new RealComplexINumber(a, -1 * b); 114 } 115 116 public INumber prevracene() { 117 double d = absAsRealNumber(); 118 return null; 120 } 121 122 private RealComplexINumber getComplexINumber(INumber no) { 123 if (!(no instanceof RealComplexINumber)) { 124 throw new UndefinedOperationException(); 125 } 126 return (RealComplexINumber) no; 127 } 128 129 public String toString() { 130 StringBuilder retVal = new StringBuilder (12); 131 if (a != 0) { 132 if (a > 0) { 133 retVal.append(a); 134 } else { 135 retVal.append(" - "); 136 retVal.append(Math.abs(a)); 137 } 138 } 139 if (b != 0) { 140 retVal.append(" "); 141 if (b > 0) { 142 if (a != 0) { 143 retVal.append("+ "); 144 } 145 if (b != 1) { 146 retVal.append(b); 147 } 148 } else { 149 retVal.append("- "); 150 if (b != -1) { 151 retVal.append(Math.abs(b)); 152 } 153 } 154 retVal.append("i"); 155 } 156 return retVal.toString().trim().replace(".0", ""); 157 } 158 159 public boolean equals(Object obj) { 160 boolean retVal = obj instanceof RealComplexINumber; 161 if (retVal) { 162 RealComplexINumber cn = (RealComplexINumber) obj; 163 retVal = (a == cn.a) && (b == cn.b); 164 } 165 return retVal; 166 } 167 168 public int hashCode() { 169 return (int) (a * 5 + b * 7 + 27); 170 } 171 172 173 } 174
| Popular Tags
|