Advertisement
jovanovski

НП Лаб3 - Задача 1

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