Advertisement
makispaiktis

SimpleComplex - 1ο εργαστήιο JAVA

Nov 17th, 2018 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. // Complex.java
  2.  
  3. public class Complex {
  4.    
  5.     float real;
  6.     float imaginary;
  7.    
  8.     // Constructors
  9.     Complex(){
  10.        
  11.         real = 0;
  12.         imaginary = 0;
  13.     }
  14.    
  15.     Complex(float re, float im){
  16.        
  17.         real = re;
  18.         imaginary = im;
  19.     }
  20.    
  21.     // Setters, Getters
  22.     public void setReal(float re) {
  23.        
  24.         real = re;
  25.     }
  26.    
  27.     public void setImaginary(float im) {
  28.        
  29.         imaginary = im;
  30.     }
  31.    
  32.     public float getReal() {
  33.        
  34.         return real;
  35.     }
  36.    
  37.     public float getImaginary() {
  38.        
  39.         return imaginary;
  40.     }
  41.    
  42.     // Methods
  43.     public float computeNorm() {
  44.        
  45.         return (float)Math.sqrt(real * real + imaginary * imaginary);
  46.     }
  47.    
  48.  
  49. }
  50.  
  51.  
  52.  
  53. // Complex2.java
  54.  
  55. public class Complex2 extends Complex{
  56.    
  57.     // Constructors
  58.     Complex2(){
  59.        
  60.         super();
  61.     }
  62.    
  63.     Complex2(float re, float im){
  64.        
  65.         super(re,im);
  66.     }
  67.    
  68.     // Methods
  69.     public Complex2 conjugate() {
  70.        
  71.         return new Complex2(real, -imaginary);
  72.     }
  73.    
  74.     public static Complex2 multiply(Complex2 c1, Complex2 c2) {
  75.        
  76.         float realPart = c1.getReal() * c2.getReal() - c1.getImaginary() * c2.getImaginary();
  77.         float imaginaryPart = c1.getReal() * c2.getImaginary() + c1.getImaginary() * c2.getReal();
  78.         Complex2 result = new Complex2(realPart, imaginaryPart);
  79.         return result;
  80.        
  81.     }
  82.  
  83.     public static void main(String[] args) {
  84.        
  85.         Complex2[] compArray = new Complex2[10];
  86.        
  87.         // Initialize with 10 random complex numbers
  88.         for(int i=0; i<10; i++) {
  89.            
  90.             compArray[i] = new Complex2((float)Math.random(), (float)Math.random());
  91.         }
  92.        
  93.         // For the array of the norms
  94.         float[] norms = new float[10];
  95.         for(int i=0; i<10; i++) {
  96.            
  97.             norms[i] = compArray[i].computeNorm();
  98.         }
  99.        
  100.         // Calculations
  101.         System.out.print("Complex C         ");
  102.         System.out.print("Conjugate C~          ");
  103.         System.out.print("C * C~                ");
  104.         System.out.print("(Norm of C)^2");
  105.        
  106.         System.out.println();
  107.         System.out.println();
  108.        
  109.         for(int i=0; i<10; i++) {
  110.            
  111.            
  112.             System.out.print(compArray[i].getReal() + " + i " + compArray[i].getImaginary() + "      ");
  113.             System.out.print(compArray[i].conjugate().getReal() + " + i " + compArray[i].conjugate().getImaginary() + "      ");
  114.             System.out.print(multiply(compArray[i], compArray[i].conjugate()).getReal() + " + i " + multiply(compArray[i], compArray[i].conjugate()).getImaginary() + "           ");
  115.             System.out.print(norms[i] * norms[i]);
  116.             System.out.println();
  117.         }
  118.  
  119.     }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement