Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Collections;
- import java.util.LinkedList;
- class ComplexNumber<T extends Number, U extends Number> implements Comparable<ComplexNumber<?, ?>> {
- private T real;
- private U imaginary;
- ComplexNumber(T real, U imaginary) {
- this.real = real;
- this.imaginary = imaginary;
- }
- public T getReal() {
- return real;
- }
- public U getImaginary() {
- return imaginary;
- }
- public double modul() {
- /* sqrt(x^2 + y^2) */
- return Math.sqrt(Math.pow(real.doubleValue(), 2) + Math.pow(imaginary.doubleValue(), 2));
- }
- @Override
- public String toString() {
- StringBuffer sb = new StringBuffer();
- if (imaginary.doubleValue() >= 0) {
- sb.append(String.format("%.2f+%.2fi", real.doubleValue(), imaginary.doubleValue()));
- } else {
- sb.append(String.format("%.2f-%.2fi", real.doubleValue(), imaginary.doubleValue() * (-1)));
- }
- return sb.toString();
- }
- @Override
- public int compareTo(ComplexNumber<?, ?> that) {
- return Double.compare(this.modul(), that.modul());
- }
- }
- 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.getReal());
- System.out.println(c.getImaginary());
- 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.getReal());
- System.out.println(c.getImaginary());
- 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.hasNextDouble()) {
- 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.hasNextDouble()) {
- 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