Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.Scanner;
- class ComplexNumber<T extends Number, U extends Number> implements Comparable<ComplexNumber<T,U>>{
- private T realen;
- private U imaginaren;
- public ComplexNumber(){
- realen = null;
- imaginaren = null;
- }
- public ComplexNumber(T realen, U imaginaren){
- this.realen = realen;
- this.imaginaren = imaginaren;
- }
- public T getR(){
- return this.realen;
- }
- public U getI(){
- return this.imaginaren;
- }
- public double modul(){
- return Math.sqrt(Math.pow(realen.doubleValue(), 2) + Math.pow(imaginaren.doubleValue(), 2));
- }
- public String toString(){
- return ""+realen.toString()+"+"+imaginaren.toString()+"i";
- }
- public int compareTo(ComplexNumber<T, U> o) {
- if(this.modul()>o.modul()) return 1;
- else if(this.modul()==o.modul()) return 0;
- else return -1;
- }
- }
- public class ComplexNumberTest {
- public static void main(String[] args) {
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if (k == 0) { // test simple functions int
- int r = jin.nextInt();
- int i = jin.nextInt();
- ComplexNumber<Integer, Integer> c = new ComplexNumber<Integer, Integer>(
- r, i);
- System.out.println(c);
- System.out.println(c.getR());
- System.out.println(c.getI());
- System.out.println(c.modul());
- }
- if (k == 1) { // test simple functions float
- float r = jin.nextFloat();
- float i = jin.nextFloat();
- ComplexNumber<Float, Float> c = new ComplexNumber<Float, Float>(r,
- i);
- System.out.println(c);
- System.out.println(c.getR());
- System.out.println(c.getI());
- System.out.println(c.modul());
- }
- if (k == 2) { // compareTo int
- LinkedList<ComplexNumber<Integer, Integer>> complex = new LinkedList<ComplexNumber<Integer, Integer>>();
- while (jin.hasNextInt()) {
- int r = jin.nextInt();
- int i = jin.nextInt();
- complex.add(new ComplexNumber<Integer, Integer>(r, i));
- }
- System.out.println(complex);
- Collections.sort(complex);
- System.out.println(complex);
- }
- if (k == 3) { // compareTo double
- LinkedList<ComplexNumber<Double, Double>> complex = new LinkedList<ComplexNumber<Double, Double>>();
- while (jin.hasNextInt()) {
- double r = jin.nextDouble();
- double i = jin.nextDouble();
- complex.add(new ComplexNumber<Double, Double>(r, i));
- }
- System.out.println(complex);
- Collections.sort(complex);
- System.out.println(complex);
- }
- if (k == 4) { // compareTo mixed
- LinkedList<ComplexNumber<Double, Integer>> complex = new LinkedList<ComplexNumber<Double, Integer>>();
- while (jin.hasNextInt()) {
- double r = jin.nextDouble();
- int i = jin.nextInt();
- complex.add(new ComplexNumber<Double, Integer>(r, i));
- }
- System.out.println(complex);
- Collections.sort(complex);
- System.out.println(complex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement