Guest User

Untitled

a guest
Apr 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. public class ComplexNumber {
  2.  
  3. private double re;
  4. private double im;
  5. ComplexNumber(double x, double y){
  6. re = x;
  7. im = y;
  8. }
  9.  
  10.  
  11. public static ComplexNumber add(ComplexNumber x,ComplexNumber y){
  12. double a = x.re + y.re;
  13. double b = x.im + y.im;
  14. ComplexNumber z1 = new ComplexNumber(a,b);
  15. return z1;
  16. }
  17.  
  18. public static ComplexNumber multiply(ComplexNumber x, ComplexNumber y){
  19. double c = ((x.re*y.re) - (x.im*y.im));
  20. double d = ((x.re*y.im)+(x.im*y.re));
  21. ComplexNumber z2 = new ComplexNumber(c,d);
  22. return z2;
  23. }
  24.  
  25. public static ComplexNumber multiply(ComplexNumber x, double y){
  26. double a = x.re*y;
  27. double b = x.im*y;
  28. ComplexNumber z3 = new ComplexNumber(a,b);
  29. return z3;
  30. }
  31.  
  32. //public void ComplexNumber(double re, double im){
  33.  
  34. //}
  35.  
  36.  
  37.  
  38. public String tostring(){
  39. return re + "+" + im + "i";
  40. }
  41.  
  42. public double getAbs(){
  43. double a = Math.sqrt(((Math.pow(re,2))+(Math.pow(im, 2))));
  44. return a;
  45. }
  46.  
  47. public double getAngle(){
  48. double a = Math.atan2(im, re);
  49. return a;
  50. }
  51.  
  52. public double getRealPart(){
  53. return re;
  54. }
  55.  
  56. public double getImaginaryPart(){
  57. return im;
  58. }
  59.  
  60. public ComplexNumber add(ComplexNumber x){
  61. return x;
  62. }
  63.  
  64. public ComplexNumber multiply(ComplexNumber x){
  65. return x;
  66. }
  67.  
  68.  
  69. public static void main(String[] args){
  70. ComplexNumber k1 = new ComplexNumber(1, 2);
  71. ComplexNumber k2 = new ComplexNumber(4, -1);
  72. System.out.println(k1 + " + " + k2 + " = " +
  73. k1.add (k2));
  74. System.out.println(k1 + " + " + k2 + " = " +
  75. ComplexNumber.add (k1, k2));
  76. System.out.println(k1 + " * " + k2 + " = " +
  77. k1.multiply(k2));
  78. System.out.println(k1 + " * " + k2 + " = " +
  79. ComplexNumber.multiply(k1, k2));
  80. System.out.println(k1 + " * " + 0.5 + " = " +
  81. ComplexNumber.multiply(k1, 0.5));
  82. }
  83.  
  84.  
  85. }
Add Comment
Please, Sign In to add comment