Advertisement
Nikolovska

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

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