Ultra Geile Studenten Benutzer Oberfläche (UGSBO)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.8 KiB

  1. package com.ugsbo.complexnumcalc;
  2. public class ComplexNumber {
  3. private Double realPart;
  4. private Double imaginaryPart;
  5. /**
  6. * @param realPart The real part of the complex Number
  7. * @param imaginaryPart The imaginary part of the complex Number
  8. */
  9. public ComplexNumber(Double realPart, Double imaginaryPart) {
  10. this.realPart = realPart;
  11. this.imaginaryPart = imaginaryPart;
  12. }
  13. /**
  14. * @return the realPart
  15. */
  16. public Double getRealPart() {
  17. return realPart;
  18. }
  19. /**
  20. * @param realPart the realPart to set
  21. */
  22. public void setRealPart(Double realPart) {
  23. this.realPart = realPart;
  24. }
  25. /**
  26. * @return the imaginaryPart
  27. */
  28. public Double getImaginaryPart() {
  29. return imaginaryPart;
  30. }
  31. /**
  32. * @param imaginaryPart the imaginaryPart to set
  33. */
  34. public void setImaginaryPart(Double imaginaryPart) {
  35. this.imaginaryPart = imaginaryPart;
  36. }
  37. /**
  38. * Checks if the given complex Number is equal to this object.
  39. *
  40. * @param complexNumber The number wich gets compared with this Instance
  41. * @return True if the complex Numbers are Equal
  42. */
  43. @Override
  44. public boolean equals(Object complexNumber) {
  45. if (complexNumber instanceof ComplexNumber){
  46. ComplexNumber that = (ComplexNumber) complexNumber;
  47. return this.realPart.equals(that.realPart) && this.imaginaryPart.equals(that.imaginaryPart);
  48. } else {
  49. return false;
  50. }
  51. }
  52. /**
  53. * Adds two complex Numbers together.
  54. *
  55. * @param addend The complex Number.
  56. * @return The result of adding the two complex Numbers together, as a conplex
  57. * Number.
  58. */
  59. public ComplexNumber add(ComplexNumber addend) {
  60. Double sumRealPart, sumImaginaryPart;
  61. sumRealPart = this.realPart + addend.realPart;
  62. sumImaginaryPart = this.imaginaryPart + addend.imaginaryPart;
  63. ComplexNumber sum = new ComplexNumber(sumRealPart, sumImaginaryPart);
  64. return sum;
  65. }
  66. /**
  67. * Substracts the Subtrahend form this instance.
  68. *
  69. * @param subtrahend The Number wich will be substracted form the Minuend
  70. * @return The Differenz of the Minuend and Subtrahend.
  71. */
  72. public ComplexNumber substract(ComplexNumber subtrahend) {
  73. Double differenzRealPart, differenzImaginaryPart;
  74. differenzRealPart = this.realPart - subtrahend.realPart;
  75. differenzImaginaryPart = this.imaginaryPart - subtrahend.imaginaryPart;
  76. ComplexNumber differenz = new ComplexNumber(differenzRealPart, differenzImaginaryPart);
  77. return differenz;
  78. }
  79. /**
  80. * Multiplies the faktor with this Instance.
  81. *
  82. * @param faktor The ComplexNumber by wich this Instance will get multiplyed
  83. * @return The product of this Instance and the faktor
  84. */
  85. public ComplexNumber multiply(ComplexNumber faktor) {
  86. Double productRealPart, productImaginaryPart;
  87. productRealPart = this.realPart * faktor.realPart - this.imaginaryPart * faktor.imaginaryPart;
  88. productImaginaryPart = this.realPart * faktor.imaginaryPart + this.imaginaryPart * faktor.realPart;
  89. ComplexNumber product = new ComplexNumber(productRealPart, productImaginaryPart);
  90. return product;
  91. }
  92. /**
  93. * Divides the dividend by the divisor, the dividend is this Instance.
  94. *
  95. * @param divisor The ComplexNumber by wich this Instance will get divided
  96. * @return The Qoutient of the Instance and the divisor
  97. */
  98. public ComplexNumber divide(ComplexNumber divisor) {
  99. Double qoutientRealPart, qoutientImaginaryPart, tempDivisor;
  100. tempDivisor = divisor.realPart * divisor.realPart + divisor.imaginaryPart * divisor.imaginaryPart;
  101. qoutientRealPart = this.realPart * divisor.realPart + this.imaginaryPart * divisor.imaginaryPart;
  102. qoutientImaginaryPart = this.imaginaryPart * divisor.realPart - this.realPart * divisor.imaginaryPart;
  103. qoutientImaginaryPart /= tempDivisor;
  104. qoutientRealPart /= tempDivisor;
  105. ComplexNumber qoutient = new ComplexNumber(qoutientRealPart, qoutientImaginaryPart);
  106. return qoutient;
  107. }
  108. /**
  109. * Calucates the absolute value of this complex number
  110. * @return the absolute value
  111. */
  112. public Double absolutValueOf(){
  113. Double absoluteValue = Math.sqrt(Math.pow(this.realPart, 2) + Math.pow(this.imaginaryPart, 2)) ;
  114. return absoluteValue;
  115. }
  116. /**
  117. * Calucates the absolute value of this complex number
  118. * @return the absolute value
  119. */
  120. public ComplexNumber conjugationOf(){
  121. if(this.imaginaryPart.equals(Double.valueOf(0))){
  122. return this;
  123. } else {
  124. this.imaginaryPart *= (-1);
  125. return this;
  126. }
  127. }
  128. }