Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is a solution of the equation x2 =1. Because no real number satisfies this equation, i is called an imaginary number. For the complex number a + bi, a is called the real part, and b is called the imaginary part. To add or subtract two complex numbers, just add or subtract the corresponding real and imaginary parts. For instance, the sum of 5 + 3i and 4 + 2i is 9 + 5i. For another, the sum of 3 + i and –1 + 2i is 2 + 3i.
  2.  
  3. Write a class with the name ComplexNumber. The class needs two fields (instance variables) with name real and imaginary of type double. It represents the Complex Number.
  4.  
  5. The class needs to have one constructor. The constructor has parameters real and imaginary of type double and it needs to initialize the fields.
  6.  
  7. Write the following methods (instance methods):
  8. * Method named getReal without any parameters, it needs to return the value of real field.
  9. * Method named getImaginary without any parameters, it needs to return the value of imaginary field.
  10. * Method named add with two parameters real and imaginary of type double, it needs to add parameters to fields. In other words, it needs to do a complex number add operation as described above.
  11. * Method named add with one parameter of type ComplexNumber. It needs to add the ComplexNumber parameter to the corresponding instance variables.
  12. * Method named subtract with two parameters real and imaginary of type double, it needs to subtract parameters from fields, in other words, it needs to do a complex number subtract operation as described above.
  13. * Method named subtract with one parameter other of type ComplexNumber. It needs to subtract the other parameter from this complex number.
  14.  
  15.  
  16. TEST EXAMPLE
  17.  
  18. → TEST CODE:
  19.  
  20. ComplexNumber one = new ComplexNumber(1.0, 1.0);
  21. ComplexNumber number = new ComplexNumber(2.5, -1.5);
  22. one.add(1,1);
  23. System.out.println("one.real= " + one.getReal());
  24. System.out.println("one.imaginary= " + one.getImaginary());
  25. one.subtract(number);
  26. System.out.println("one.real= " + one.getReal());
  27. System.out.println("one.imaginary= " + one.getImaginary());
  28. number.subtract(one);
  29. System.out.println("number.real= " + number.getReal());
  30. System.out.println("number.imaginary= " + number.getImaginary());
  31.  
  32. → OUTPUT
  33.  
  34. one.real= 2.0
  35. one.imaginary= 2.0
  36. one.real= -0.5
  37. one.imaginary= 3.5
  38. number.real= 3.0
  39. number.imaginary= -5.0
  40.  
  41.  
  42. NOTE: Try to avoid duplicated code.
  43.  
  44. NOTE: All methods should be defined as public NOT public static.
  45.  
  46. NOTE: In total, you have to write 6 methods.
  47.  
  48. NOTE: Do not add a main method to the solution code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement