Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- MAINIK
- =-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- public class Main {
- public static void main(String[] args) {
- List<ComplexNumbers> complexNumbers = new ArrayList<>();
- complexNumbers.add(new ComplexNumbers(1, 2));
- complexNumbers.add(new ComplexNumbers(3, 4));
- complexNumbers.add(new ComplexNumbers(6, 9));
- Collections.sort(complexNumbers, (c1, c2) -> Double.compare(c1.getModulus(), c2.getModulus()));
- System.out.println(complexNumbers);
- ComplexNumbers a = complexNumbers.get(0);
- ComplexNumbers b = complexNumbers.get(1);
- System.out.println("Posortowana lista liczb zespolonych");
- System.out.println("Moduł z przykładowej liczby zespolonej: " + a.getModulus());
- System.out.println("Dodawanie liczb zespolonych: " + a.add(b));
- }
- }
- =-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- KLASKA
- =-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- class ComplexNumbers {
- private double real;
- private double imaginary;
- public ComplexNumbers(double real, double imaginary) {
- this.real = real;
- this.imaginary = imaginary;
- }
- public ComplexNumbers add(ComplexNumbers other) {
- return new ComplexNumbers(this.real + other.real, this.imaginary + other.imaginary);
- }
- public double getModulus() {
- return Math.sqrt(Math.pow(real, 2) + Math.pow(imaginary, 2));
- }
- @Override
- public String toString() {
- return real + " + " + imaginary + "i";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement