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.

153 lines
4.7 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. public boolean equals(ComplexNumber complexNumber) {
  44. if (this.realPart.equals(complexNumber.realPart) && this.imaginaryPart.equals(complexNumber.imaginaryPart)) {
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. /**
  51. * Adds two complex Numbers together.
  52. *
  53. * @param addend The complex Number.
  54. * @return The result of adding the two complex Numbers together, as a conplex
  55. * Number.
  56. */
  57. public ComplexNumber add(ComplexNumber addend) {
  58. Double sumRealPart, sumImaginaryPart;
  59. sumRealPart = this.realPart + addend.realPart;
  60. sumImaginaryPart = this.imaginaryPart + addend.imaginaryPart;
  61. ComplexNumber sum = new ComplexNumber(sumRealPart, sumImaginaryPart);
  62. return sum;
  63. }
  64. /**
  65. * Substracts the Subtrahend form this instance.
  66. *
  67. * @param subtrahend The Number wich will be substracted form the Minuend
  68. * @return The Differenz of the Minuend and Subtrahend.
  69. */
  70. public ComplexNumber substract(ComplexNumber subtrahend) {
  71. Double differenzRealPart, differenzImaginaryPart;
  72. differenzRealPart = this.realPart - subtrahend.realPart;
  73. differenzImaginaryPart = this.imaginaryPart - subtrahend.imaginaryPart;
  74. ComplexNumber differenz = new ComplexNumber(differenzRealPart, differenzImaginaryPart);
  75. return differenz;
  76. }
  77. /**
  78. * Multiplies the faktor with this Instance.
  79. *
  80. * @param faktor The ComplexNumber by wich this Instance will get multiplyed
  81. * @return The product of this Instance and the faktor
  82. */
  83. public ComplexNumber multiply(ComplexNumber faktor) {
  84. Double productRealPart, productImaginaryPart;
  85. productRealPart = this.realPart * faktor.realPart - this.imaginaryPart * faktor.imaginaryPart;
  86. productImaginaryPart = this.realPart * faktor.imaginaryPart + this.imaginaryPart * faktor.realPart;
  87. ComplexNumber product = new ComplexNumber(productRealPart, productImaginaryPart);
  88. return product;
  89. }
  90. /**
  91. * Divides the dividend by the divisor, the dividend is this Instance.
  92. *
  93. * @param divisor The ComplexNumber by wich this Instance will get divided
  94. * @return The Qoutient of the Instance and the divisor
  95. */
  96. public ComplexNumber divide(ComplexNumber divisor) {
  97. Double qoutientRealPart, qoutientImaginaryPart, tempDivisor;
  98. tempDivisor = divisor.realPart * divisor.realPart + divisor.imaginaryPart * divisor.imaginaryPart;
  99. qoutientRealPart = this.realPart * divisor.realPart + this.imaginaryPart * divisor.imaginaryPart;
  100. qoutientImaginaryPart = this.imaginaryPart * divisor.realPart - this.realPart * divisor.imaginaryPart;
  101. qoutientImaginaryPart /= tempDivisor;
  102. qoutientRealPart /= tempDivisor;
  103. ComplexNumber qoutient = new ComplexNumber(qoutientRealPart, qoutientImaginaryPart);
  104. return qoutient;
  105. }
  106. /**
  107. * Calucates the absolute value of this complex number
  108. * @return the absolute value
  109. */
  110. public Double absolutValueOf(){
  111. Double absoluteValue = Math.sqrt(Math.pow(this.realPart, 2) + Math.pow(this.imaginaryPart, 2)) ;
  112. return absoluteValue;
  113. }
  114. /**
  115. * Calucates the absolute value of this complex number
  116. * @return the absolute value
  117. */
  118. public ComplexNumber conjugationOf(){
  119. if(this.imaginaryPart.equals(Double.valueOf(0))){
  120. return this;
  121. } else {
  122. this.imaginaryPart *= (-1);
  123. return this;
  124. }
  125. }
  126. }