kristina7

НП - Лаб 4-1 Комплексни броеви

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