Advertisement
SashkoKlincharov

[Java][НП] - Комплексни броеви

Aug 26th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1.  
  2.  
  3. Треба да се развие генеричка класа за работа со комплексни броевиComplexNumber со два генерички параметри T и U кои мора да бидат од некоја класа која наследува од класата Number (T extends Number). ComplexNumber има две променливи од кои едната се однесува на реалниот дел, а другата на имагинарниот дел и треба да ги имплементира следните методи:
  4.  
  5. ComplexNumber(T real, U imaginary) - конструктор кој ги иницијализира сите променливи
  6. getReal():T
  7. getImaginary():U
  8. modul():double - го пресметува модулот на комплексниот број
  9. compareTo(ComplexNumber<?, ?> o) - прави споредување врз основа на модулите на двата комплексни броја
  10. toString():String - го печати бројот во следниот формат 2.30+3.00i
  11.  
  12.  
  13.  
  14. import java.util.Collections;
  15. import java.util.LinkedList;
  16. import java.util.Scanner;
  17.  
  18. class ComplexNumber<T extends Number,U extends Number> implements Comparable<ComplexNumber<?,?>> {
  19. private T real;
  20. private U imaginary;
  21.  
  22. public ComplexNumber(T real, U imaginary){
  23. this.real = real;
  24. this.imaginary = imaginary;
  25. }
  26.  
  27. public T getReal() {
  28. return real;
  29. }
  30.  
  31. public U getImaginary() {
  32. return imaginary;
  33. }
  34.  
  35. public double modul(){
  36. return Math.sqrt(Math.pow(real.doubleValue(),2) + Math.pow(imaginary.doubleValue(), 2));
  37. }
  38.  
  39. @Override
  40. public int compareTo(ComplexNumber<?, ?> o) {
  41. return Double.compare(this.modul(),o.modul());
  42. }
  43.  
  44. @Override
  45. public String toString() {
  46. StringBuilder sb = new StringBuilder();
  47. if(getImaginary().doubleValue()<0) {
  48. sb.append(String.format("%1.2f%1.2fi", getReal().doubleValue(), getImaginary().doubleValue()));
  49. }else {
  50. sb.append(String.format("%1.2f+%1.2fi", getReal().doubleValue(), getImaginary().doubleValue()));
  51. }
  52. return sb.toString();}
  53. }
  54.  
  55. public class ComplexNumberTest {
  56.  
  57. public static void main(String[] args) {
  58. Scanner jin = new Scanner(System.in);
  59. int k = jin.nextInt();
  60. if ( k == 0 ) { //test simple functions int
  61. int r = jin.nextInt();int i = jin.nextInt();
  62. ComplexNumber<Integer, Integer> c = new ComplexNumber<Integer, Integer>(r, i);
  63. System.out.println(c);
  64. System.out.println(c.getReal());
  65. System.out.println(c.getImaginary());
  66. System.out.println(c.modul());
  67. }
  68. if ( k == 1 ) { //test simple functions float
  69. float r = jin.nextFloat();
  70. float i = jin.nextFloat();
  71. ComplexNumber<Float, Float> c = new ComplexNumber<Float, Float>(r, i);
  72. System.out.println(c);
  73. System.out.println(c.getReal());
  74. System.out.println(c.getImaginary());
  75. System.out.println(c.modul());
  76. }
  77. if ( k == 2 ) { //compareTo int
  78. LinkedList<ComplexNumber<Integer,Integer>> complex = new LinkedList<ComplexNumber<Integer,Integer>>();
  79. while ( jin.hasNextInt() ) {
  80. int r = jin.nextInt(); int i = jin.nextInt();
  81. complex.add(new ComplexNumber<Integer, Integer>(r, i));
  82. }
  83. System.out.println(complex);
  84. Collections.sort(complex);
  85. System.out.println(complex);
  86. }
  87. if ( k == 3 ) { //compareTo double
  88. LinkedList<ComplexNumber<Double,Double>> complex = new LinkedList<ComplexNumber<Double,Double>>();
  89. while ( jin.hasNextDouble() ) {
  90. double r = jin.nextDouble(); double i = jin.nextDouble();
  91. complex.add(new ComplexNumber<Double, Double>(r, i));
  92. }
  93. System.out.println(complex);
  94. Collections.sort(complex);
  95. System.out.println(complex);
  96. }
  97. if ( k == 4 ) { //compareTo mixed
  98. LinkedList<ComplexNumber<Double,Integer>> complex = new LinkedList<ComplexNumber<Double,Integer>>();
  99. while ( jin.hasNextDouble() ) {
  100. double r = jin.nextDouble(); int i = jin.nextInt();
  101. complex.add(new ComplexNumber<Double, Integer>(r, i));
  102. }
  103. System.out.println(complex);
  104. Collections.sort(complex);
  105. System.out.println(complex);
  106. }
  107. }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. Sample input
  114.  
  115. 0
  116. 0 0
  117.  
  118. Sample output
  119.  
  120. 0.00+0.00i
  121. 0
  122. 0
  123. 0.0
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement