Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Complex.java
- public class Complex {
- float real;
- float imaginary;
- // Constructors
- Complex(){
- real = 0;
- imaginary = 0;
- }
- Complex(float re, float im){
- real = re;
- imaginary = im;
- }
- // Setters, Getters
- public void setReal(float re) {
- real = re;
- }
- public void setImaginary(float im) {
- imaginary = im;
- }
- public float getReal() {
- return real;
- }
- public float getImaginary() {
- return imaginary;
- }
- // Methods
- public float computeNorm() {
- return (float)Math.sqrt(real * real + imaginary * imaginary);
- }
- }
- // Complex2.java
- public class Complex2 extends Complex{
- // Constructors
- Complex2(){
- super();
- }
- Complex2(float re, float im){
- super(re,im);
- }
- // Methods
- public Complex2 conjugate() {
- return new Complex2(real, -imaginary);
- }
- public static Complex2 multiply(Complex2 c1, Complex2 c2) {
- float realPart = c1.getReal() * c2.getReal() - c1.getImaginary() * c2.getImaginary();
- float imaginaryPart = c1.getReal() * c2.getImaginary() + c1.getImaginary() * c2.getReal();
- Complex2 result = new Complex2(realPart, imaginaryPart);
- return result;
- }
- public static void main(String[] args) {
- Complex2[] compArray = new Complex2[10];
- // Initialize with 10 random complex numbers
- for(int i=0; i<10; i++) {
- compArray[i] = new Complex2((float)Math.random(), (float)Math.random());
- }
- // For the array of the norms
- float[] norms = new float[10];
- for(int i=0; i<10; i++) {
- norms[i] = compArray[i].computeNorm();
- }
- // Calculations
- System.out.print("Complex C ");
- System.out.print("Conjugate C~ ");
- System.out.print("C * C~ ");
- System.out.print("(Norm of C)^2");
- System.out.println();
- System.out.println();
- for(int i=0; i<10; i++) {
- System.out.print(compArray[i].getReal() + " + i " + compArray[i].getImaginary() + " ");
- System.out.print(compArray[i].conjugate().getReal() + " + i " + compArray[i].conjugate().getImaginary() + " ");
- System.out.print(multiply(compArray[i], compArray[i].conjugate()).getReal() + " + i " + multiply(compArray[i], compArray[i].conjugate()).getImaginary() + " ");
- System.out.print(norms[i] * norms[i]);
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement