Advertisement
mrAnderson33

пятая лаба Java

Dec 21st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.37 KB | None | 0 0
  1. //Complex.java
  2.  
  3. package com.company;
  4.  
  5. import java.util.ArrayList;
  6.  
  7. public class Complex {
  8.  
  9.  
  10.     public Complex()
  11.     {
  12.  
  13.     }
  14.  
  15.     public  Complex(Complex val)
  16.     {
  17.         this.real = val.real;
  18.         this.imaginary = val.imaginary;
  19.     }
  20.  
  21.     public  Complex(double real , double imaginary)
  22.     {
  23.         this.real = real;
  24.         this.imaginary = imaginary;
  25.     }
  26.  
  27.     public  Complex sum(Complex right)
  28.     {
  29.         this.real+=right.real;
  30.         this.imaginary+= right.imaginary;
  31.  
  32.         return this;
  33.     }
  34.  
  35.     public  static  Complex sum(Complex left, Complex right)
  36.     {
  37.         Complex res = new Complex(left);
  38.  
  39.        return   res.sum(right);
  40.     }
  41.  
  42.     public  Complex multiply(Complex right)
  43.     {
  44.         this.real = this.real*right.real - this.imaginary*right.imaginary;
  45.         this.imaginary = this.imaginary * right.real + this.real * right.imaginary;
  46.  
  47.         return this;
  48.     }
  49.  
  50.     public  static  Complex multiply(Complex left, Complex right)
  51.     {
  52.      Complex res = new Complex(left);
  53.  
  54.      return res.multiply(right);
  55.     }
  56.  
  57.     public Complex division(Complex right)
  58.     {
  59.         if (right.magnitude() == 0) throw  new IllegalArgumentException("Division by zero!");
  60.         this.real = (this.real *right.real + this.imaginary * right.imaginary)/(right.real * right.real + right.imaginary*right.imaginary);
  61.         this.imaginary = (this.imaginary * right.real + this.real * right.imaginary)/(right.real * right.real + right.imaginary*right.imaginary);
  62.  
  63.         return this;
  64.     }
  65.  
  66.     public  static Complex division (Complex left, Complex right)
  67.     {
  68.         Complex res = new Complex(left);
  69.  
  70.         return res.division(right);
  71.     }
  72.  
  73.     private Complex pow(double degree)
  74.     {
  75.  
  76.         this.real = this.real == 0 ? 0 : Math.pow(this.magnitude(),degree) * Math.cos(degree * this.phase());
  77.         this.imaginary = this.imaginary == 0 ? 0 : Math.pow(this.magnitude(),degree) * Math.sin(degree * this.phase());
  78.  
  79.         return this;
  80.     }
  81.  
  82.     public  static  Complex pow(Complex val, double degree)
  83.     {
  84.         return val.pow(degree);
  85.     }
  86.  
  87.     private ArrayList<Complex>  sqrt(int n)
  88.     {
  89.         ArrayList<Complex> res = new ArrayList<>();
  90.  
  91.         for (int i = 0; i< n;++i)
  92.         {
  93.             double real = Math.sqrt(this.phase()) * Math.cos((this.phase() + 2 * i * Math.PI)/n);
  94.             double imaginary = Math.sqrt(this.phase()) * Math.sin((this.phase() + 2 * i * Math.PI)/n);
  95.  
  96.             res.add(new Complex(real,imaginary));
  97.         }
  98.  
  99.         return res;
  100.  
  101.     }
  102.  
  103.     public  static ArrayList<Complex> sqrt(Complex val, int n)
  104.     {
  105.         return val.sqrt(n);
  106.     }
  107.  
  108.     public  double magnitude()
  109.     {
  110.         if (real > 0)
  111.             return Math.sqrt(real*real+imaginary*imaginary);
  112.         if (real < 0 && imaginary >= 0)  return Math.PI + Math.sqrt(real*real+imaginary*imaginary);
  113.         if ( real < 0 && imaginary < 0) return  -Math.PI + Math.sqrt(real*real+imaginary*imaginary);
  114.         if(real == 0 && imaginary >0) return Math.PI/2;
  115.         if(real == 0 && imaginary <0) return -Math.PI/2;
  116.         return 0;
  117.     }
  118.  
  119.     public double phase()
  120.     {
  121.         return Math.atan2(imaginary,real);
  122.     }
  123.  
  124.     @Override
  125.     public String toString() {
  126.         final StringBuffer sb = new StringBuffer("Complex{");
  127.         sb.append("real=").append(real);
  128.         sb.append(", imaginary=").append(imaginary);
  129.         sb.append('}');
  130.         return sb.toString();
  131.     }
  132.  
  133.     private double real = 0;
  134.     private  double imaginary = 0;
  135. }
  136. //Main.java
  137.  
  138. package com.company;
  139.  
  140. import java.util.InputMismatchException;
  141. import java.util.Scanner;
  142.  
  143. public class Main {
  144.  
  145.     public static void main(String[] args) {
  146.  
  147.         double re = 0;
  148.         double im = 0;
  149.  
  150.         Scanner in = new Scanner(System.in);
  151.  
  152.         try
  153.         {
  154.             System.out.print("Please enter a real part of the firts complex naumber:");
  155.             re = in.nextDouble();
  156.             System.out.print("Please enter a imaginary part of the firts complex naumber:");
  157.             im = in.nextDouble();
  158.         }
  159.         catch (InputMismatchException ex)
  160.         {
  161.             System.err.println("Error, you should enter a number");
  162.             System.exit(1);
  163.         }
  164.  
  165.         Complex ob1 = new Complex(re, im);
  166.  
  167.         try
  168.         {
  169.             System.out.print("Please enter a real part of the second complex number:");
  170.             re = in.nextDouble();
  171.             System.out.print("Please enter a imaginary part of the second complex number:");
  172.             im = in.nextDouble();
  173.         }
  174.         catch (InputMismatchException ex)
  175.         {
  176.             System.err.println("Error, you should enter a number");
  177.             System.exit(2);
  178.         }
  179.  
  180.         Complex ob2 = new Complex(re, im);
  181.  
  182.         System.out.println(ob1 + " + " + ob2 + " = " + Complex.sum(ob1, ob2));
  183.         System.out.println(ob1 + " * " + ob2 + " = " + Complex.multiply(ob1, ob2));
  184.         try
  185.         {
  186.             System.out.println(ob1 + " / " + ob2 + " = " + Complex.division(ob1, ob2));
  187.         }
  188.         catch (IllegalArgumentException ex)
  189.         {
  190.             System.err.println(ex.getMessage());
  191.             System.exit(3);
  192.         }
  193.  
  194.         System.out.println(ob1 + " ^ " + 3 + " = " + Complex.pow(ob1,3));
  195.         System.out.println("sqrt(" + ob1 + ") = " +Complex.sqrt(ob1,3));
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement