Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Генеричко програмирање (4)
- Комплексни броеви Problem 1 (0 / 0)
- Треба да се развие генеричка класа за работа со комплексни броевиComplexNumber со два генерички параметри T и U кои
- мора да бидат од некоја класа која наследува од класата Number (T extends Number). ComplexNumber има две променливи од
- кои едната се однесува на реалниот дел, а другата на имагинарниот дел и треба да ги имплементира следните методи:
- ComplexNumber(T real, U imaginary) - конструктор кој ги иницијализира сите променливи
- getReal():T
- getImaginary():U
- modul():double - го пресметува модулот на комплексниот број
- compareTo(ComplexNumber<?, ?> o) - прави споредување врз основа на модулите на двата комплексни броја
- toString():String - го печати бројот во следниот формат 2.30+3.00i
- Sample input
- 0
- 0 0
- Sample output
- 0.00+0.00i
- 0
- 0
- 0.0
- */
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.Scanner;
- class ComplexNumber<T extends Number,U extends Number> implements Comparable<ComplexNumber<?,?>>{
- private T real;
- private U imaginary;
- public ComplexNumber(T real, U imaginary) {
- this.real = real;
- this.imaginary = imaginary;
- }
- public T getReal() {
- return real;
- }
- public U getImaginary() {
- return imaginary;
- }
- public double modul(){
- // z = sqrt(x^2 + y^2)
- return Math.sqrt(Math.pow(real.doubleValue(),2) + Math.pow(imaginary.doubleValue(),2));
- }
- @Override
- public int compareTo(ComplexNumber<?, ?> o) {
- /*if (modul() > o.modul())
- return 1;
- if (modul() == o.modul())
- return 0;
- return -1;*/
- return Double.compare(modul(),o.modul());
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(String.format("%.2f",real.doubleValue()));
- if (imaginary.doubleValue() >= 0)
- sb.append(String.format("+%.2fi",imaginary.doubleValue()));
- else
- sb.append(String.format("%.2fi",imaginary.doubleValue()));
- return sb.toString();
- }
- }
- 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
Advertisement