Advertisement
Dim4eBeBrat

ComplexNumberTest

Nov 19th, 2020
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. import java.util.Collections;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4.  
  5. class ComplexNumber<T extends Number, U extends Number> implements Comparable<ComplexNumber<?, ?>> {
  6.     private T real;
  7.     private U imaginary;
  8.  
  9.     public ComplexNumber(T real, U imaginary){
  10.         this.real = real;
  11.         this.imaginary = imaginary;
  12.     }
  13.  
  14.     public T getReal(){
  15.         return real;
  16.     }
  17.  
  18.     public U getImaginary(){
  19.         return imaginary;
  20.     }
  21.  
  22.     public double modul(){
  23.         return Math.sqrt(Math.pow(real.doubleValue(),2) + Math.pow(imaginary.doubleValue(),2));
  24.     }
  25.  
  26.     @Override
  27.     public String toString() {
  28.         if(imaginary.doubleValue() >= 0) return String.format("%.2f+%.2fi",real.doubleValue(),imaginary.doubleValue());
  29.         return String.format("%.2f%.2fi",real.doubleValue(),imaginary.doubleValue());
  30.     }
  31.  
  32.     @Override
  33.     public int compareTo(ComplexNumber<?,?> o) {
  34.         if(this.modul() > o.modul()) return 1;
  35.         if(this.modul() < o.modul()) return -1;
  36.         return 0;
  37.     }
  38.  
  39. }
  40.  
  41. public class ComplexNumberTest {
  42.  
  43.     public static void main(String[] args) {
  44.         Scanner jin = new Scanner(System.in);
  45.         int k = jin.nextInt();
  46.         if ( k == 0 ) { //test simple functions int
  47.             int r = jin.nextInt();int i = jin.nextInt();
  48.             ComplexNumber<Integer, Integer> c = new ComplexNumber<Integer, Integer>(r, i);
  49.             System.out.println(c);
  50.             System.out.println(c.getReal());
  51.             System.out.println(c.getImaginary());
  52.             System.out.println(c.modul());
  53.         }
  54.         if ( k == 1 ) { //test simple functions float
  55.             float r = jin.nextFloat();
  56.             float i = jin.nextFloat();
  57.             ComplexNumber<Float, Float> c = new ComplexNumber<Float, Float>(r, i);
  58.             System.out.println(c);
  59.             System.out.println(c.getReal());
  60.             System.out.println(c.getImaginary());
  61.             System.out.println(c.modul());
  62.         }
  63.         if ( k == 2 ) { //compareTo int
  64.             LinkedList<ComplexNumber<Integer,Integer>> complex = new LinkedList<ComplexNumber<Integer,Integer>>();
  65.             while ( jin.hasNextInt() ) {
  66.                 int r = jin.nextInt(); int i = jin.nextInt();
  67.                 complex.add(new ComplexNumber<Integer, Integer>(r, i));
  68.             }
  69.             System.out.println(complex);
  70.             Collections.sort(complex);
  71.             System.out.println(complex);
  72.         }
  73.         if ( k == 3 ) { //compareTo double
  74.             LinkedList<ComplexNumber<Double,Double>> complex = new LinkedList<ComplexNumber<Double,Double>>();
  75.             while ( jin.hasNextDouble() ) {
  76.                 double r = jin.nextDouble(); double i = jin.nextDouble();
  77.                 complex.add(new ComplexNumber<Double, Double>(r, i));
  78.             }
  79.             System.out.println(complex);
  80.             Collections.sort(complex);
  81.             System.out.println(complex);
  82.         }
  83.         if ( k == 4 ) { //compareTo mixed
  84.             LinkedList<ComplexNumber<Double,Integer>> complex = new LinkedList<ComplexNumber<Double,Integer>>();
  85.             while ( jin.hasNextDouble() ) {
  86.                 double r = jin.nextDouble(); int i = jin.nextInt();
  87.                 complex.add(new ComplexNumber<Double, Integer>(r, i));
  88.             }
  89.             System.out.println(complex);
  90.             Collections.sort(complex);
  91.             System.out.println(complex);
  92.         }
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement