Advertisement
Guest User

ComplexNumClass

a guest
Jul 29th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.72 KB | None | 0 0
  1. package niit.summer.complex;
  2. import java.text.DecimalFormat;
  3.  
  4. public class ComplexNumber {
  5.    
  6.     public static final ComplexNumber ZERO = new ComplexNumber(0,0);
  7.     private double real;
  8.     private double imaginary;
  9.     DecimalFormat decimalFormat = new DecimalFormat("0.###");
  10.  
  11.     public ComplexNumber(double real, double imaginary) {  
  12.         this.real = real;
  13.         this.imaginary = imaginary;
  14.     }
  15.     public double GetReal () {return real; }
  16.    
  17.     public double GetImaginary () {return imaginary; }
  18.  
  19.     public ComplexNumber add(ComplexNumber z2) {
  20.         return new ComplexNumber(real + z2.real, imaginary + z2.imaginary);
  21.     }
  22.  
  23.     public ComplexNumber multiply(ComplexNumber z2) {
  24.         return new ComplexNumber(real * z2.real - imaginary * z2.imaginary,imaginary * z2.real + real * z2.imaginary);
  25.     }
  26.  
  27.     /* (non-Javadoc)
  28.      * @see java.lang.Object#equals(java.lang.Object)
  29.      */
  30.     @Override
  31.     public boolean equals(Object obj) {
  32.         if (this == obj) {
  33.             return true;
  34.         }
  35.         if (obj == null) {
  36.             return false;
  37.         }
  38.         if (!(obj instanceof ComplexNumber)) {
  39.             return false;
  40.         }
  41.         ComplexNumber other = (ComplexNumber) obj;
  42.         if (Double.doubleToLongBits(imaginary) != Double.doubleToLongBits(other.imaginary)) {
  43.             return false;
  44.         }
  45.         if (Double.doubleToLongBits(real) != Double.doubleToLongBits(other.real)) {
  46.             return false;
  47.         }
  48.         return true;
  49.     }
  50.    
  51.     /* (non-Javadoc)
  52.      * @see java.lang.Object#hashCode()
  53.      */
  54.     @Override
  55.     public int hashCode() {
  56.         final int prime = 31;
  57.         int result = 1;
  58.         long temp;
  59.         temp = Double.doubleToLongBits(imaginary);
  60.         result = prime * result + (int) (temp ^ (temp >>> 32));
  61.         temp = Double.doubleToLongBits(real);
  62.         result = prime * result + (int) (temp ^ (temp >>> 32));
  63.         return result;
  64.     }
  65.  
  66.     public double abs() {
  67.             return Math.sqrt(Math.pow(real, 2) + Math.pow(imaginary, 2));
  68.     }
  69.    
  70.     public double arg() {
  71.            
  72.         double argRes = 0;
  73.        
  74.         if(real < 0 && imaginary >= 0){
  75.             argRes = Math.PI + Math.atan(imaginary/real);
  76.         }
  77.         else if(real < 0 && imaginary < 0){
  78.             argRes = (-Math.PI) + Math.atan(imaginary/real);
  79.         }
  80.         else if(real == 0 && imaginary > 0){
  81.             argRes = Math.PI / 2;
  82.         }
  83.         else if(real == 0 && imaginary < 0){
  84.             argRes = (-Math.PI) / 2;
  85.         }
  86.         else if(real > 0){
  87.             argRes = Math.atan(imaginary/real);
  88.         }
  89.         else{
  90.             argRes = Double.NaN;
  91.         }
  92.    
  93.     return argRes;
  94.     }
  95.  
  96.         public class AlgebraicRepresentation {
  97.        
  98.             public double aReal, aImaginary;
  99.            
  100.             public double getReal() {return aReal; }
  101.             public double getImaginary() {return aImaginary; }
  102.             /* (non-Javadoc)
  103.              * @see java.lang.Object#toString()
  104.              */
  105.             @Override
  106.             public String toString() {
  107.                 String formatNumber = null;
  108.                 String formatReal = "", formatImag = "";
  109.                 DecimalFormat decimalFormat = new DecimalFormat("0.###");
  110.                
  111.                 if(aReal == 0 && aImaginary == 0){
  112.                     formatNumber = decimalFormat.format(0);
  113.                 }
  114.                 else if(aReal == 0){
  115.                     formatNumber = decimalFormat.format(aImaginary);
  116.                     formatNumber = String.format("%s" + "i", formatNumber);
  117.                 }
  118.                 else if(aImaginary == 0){
  119.                     formatNumber = decimalFormat.format(aReal);
  120.                     formatNumber = String.format("%s", formatNumber);
  121.                 }
  122.                 else if (aReal != 0 && aImaginary != 0){
  123.                     formatReal = decimalFormat.format(aReal);
  124.                     formatImag = decimalFormat.format(aImaginary);
  125.                     formatNumber = String.format("%s" + " + " + "%s" + "i", formatReal, formatImag);
  126.                 }
  127.                 return formatNumber;
  128.             }
  129.                    
  130.         }      
  131.  
  132.     public AlgebraicRepresentation asAlgebraic() {
  133.         AlgebraicRepresentation algVers = new AlgebraicRepresentation();
  134.         algVers.aReal = ComplexNumber.this.GetReal();
  135.         algVers.aImaginary = ComplexNumber.this.GetImaginary();
  136.         return algVers;
  137.     }
  138.        
  139.             public class TrigonometricRepresentation {
  140.                
  141.                 private double tReal, tImaginary;
  142.            
  143.                
  144.                 public double gettReal(){ return tReal;}
  145.                 public double gettImaginary(){ return tImaginary;}
  146.                
  147.                 public double getAbsoluteValue() {
  148.                    double result = ComplexNumber.this.abs();
  149.                    return result;
  150.                    }
  151.              
  152.                  public double getArgument() {
  153.                      double arg = ComplexNumber.this.arg();  
  154.                      return arg;
  155.                    }
  156.                  
  157.                  double r = TrigonometricRepresentation.this.getAbsoluteValue();
  158.                  double fi = TrigonometricRepresentation.this.getArgument();
  159.                  double fiDegree = (fi*180) / Math.PI;
  160.                  
  161.  
  162.  
  163.                 /* (non-Javadoc)
  164.                  * @see java.lang.Object#toString()
  165.                  */
  166.                 @Override
  167.                 public String toString() {
  168.                     String formatNumber = null;
  169.                     String formatAbs = "", formatArg = "";
  170.                     DecimalFormat decimalFormat = new DecimalFormat("0.###");
  171.                    
  172.                     if(tReal == 0 && tImaginary == 0){
  173.                         formatNumber = decimalFormat.format(0);
  174.                     }
  175.                     else if(tReal == 0){
  176.                         formatArg = decimalFormat.format(fiDegree);
  177.                         formatNumber = String.format("i" + "*" + "sin" + "%s" + "°", formatArg);
  178.                         System.out.println(formatNumber);
  179.                     }
  180.                     else if(tImaginary == 0){
  181.                         formatNumber = decimalFormat.format(tReal);
  182.                         formatNumber = String.format("%s", formatNumber);
  183.                         System.out.println(formatNumber);
  184.                     }
  185.                     else if (tReal != 0 && tImaginary != 0){
  186.                         formatAbs = decimalFormat.format(r);
  187.                         formatArg = decimalFormat.format(fiDegree);
  188.                         formatNumber = String.format("%s" + "(" + "cos" + "%s" + "°" + "+" + "i" + "*" + "sin" + "%s" + "°" +")", formatAbs, formatArg, formatArg);
  189.                         System.out.println(formatNumber);
  190.                     }
  191.                     return formatNumber;
  192.                 }
  193.                }
  194.    
  195.         public TrigonometricRepresentation asTrigonometric() {
  196.             TrigonometricRepresentation trigVers = new TrigonometricRepresentation();
  197.             trigVers.tReal = ComplexNumber.this.GetReal();
  198.             trigVers.tImaginary = ComplexNumber.this.GetImaginary();
  199.             return trigVers;
  200.             }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement