(Complex Number) Write a ComplexNumber class that is similar to the RationalNumber. The class contains:
- Private double data fields named as re and im (corresponding to real and imaginary parts, respectively).
- The following methods
- A constructor that creates a ComplexNumber object for the specified two double parameters (public ComplexNumber(double r, double i)
- get methods for the real and imaginary parts (public double getReal(), public double getImaginary() )
- public ComplexNumber reciprocal()
- public ComplexNumber add(ComplexNumber)
- public ComplexNumber subtract(ComplexNumber)
- public ComplexNumber multiply(ComplexNumber)
- public ComplexNumber divide(ComplexNumber)
- public boolean equals(ComplexNumber)
- public ComplexNumber conjugate()
- public double getAngle()
- public double getMagnitude()
- public String toString()
Implement the class. Write a test program that tests all methods in the class.
For any information regarding Complex Numbers you can visit Wikipedia (http://en.wikipedia.org/wiki/Complex_number).
ComplexNumber.java
/** * Complex number. */ public class ComplexNumber { private double re, im; // Constructor 1 public ComplexNumber(double r, double i){ this.re = r; this.im = i; } // Get complex number real part. public double getReal(){ return this.re; } // Get complex number imaginary part. public double getImaginary(){ return this.im; } // Complex number reciprocal. public ComplexNumber reciprocal(){ double comnum1 = re / (Math.pow(re, 2) + Math.pow(im, 2)); double comnum2 = (im * (-1)) / (Math.pow(re, 2) + Math.pow(im, 2)); return new ComplexNumber(comnum1, comnum2); } // Complex number addition. public ComplexNumber add(ComplexNumber cn){ double comnum1 = re + cn.getReal(); double comnum2 = im + cn.getImaginary(); return new ComplexNumber(comnum1, comnum2); } // Complex number subtraction. public ComplexNumber subtract(ComplexNumber cn){ double comnum1 = re - cn.getReal(); double comnum2 = im - cn.getImaginary(); return new ComplexNumber(comnum1, comnum2); } // Complex number multiplication. public ComplexNumber multiply(ComplexNumber cn){ double comnum1 = re * cn.getReal() - im * cn.getImaginary(); double comnum2 = im * cn.getReal() + re * cn.getImaginary(); return new ComplexNumber(comnum1, comnum2); } // Complex number division. public ComplexNumber divide(ComplexNumber cn){ double comnum1 = (re * cn.getReal() + im * cn.getImaginary()) / (Math.pow(cn.getReal(), 2) + Math.pow(cn.getImaginary(), 2)); double comnum2 = (im * cn.getReal() - re * cn.getImaginary()) / (Math.pow(cn.getReal(), 2) + Math.pow(cn.getImaginary(), 2)); return new ComplexNumber(comnum1, comnum2); } // Complex number equations. public boolean equals(ComplexNumber cn){ boolean result; if(re == cn.getReal() && im == cn.getImaginary()){ result = true; }else{ result = false; } return result; } // Complex number conjugation. public ComplexNumber conjugate(){ return new ComplexNumber(re, im * (-1)); } // Get angle. public double getAngle(){ double X = Math.toDegrees(Math.atan( im / re)); if(re >= 0 && im > 0){ return X; }else if((re < 0 && im < 0) || (re < 0 && im > 0)){ return X + 180; }else{ return X + 360; } } // Calculate magnitude. public double getMagnitude(){ return Math.sqrt(Math.pow(re, 2) + Math.pow(im, 2)); } // toString method. public String toString(){ String result; if(im == 0){ result = re + ""; }else if(re == 0){ result = im + "i"; }else if (im < 0){ result = re + "-" + (im * (-1)) + "i"; }else{ result = re + "+" + im + "i"; } return result; } } // End od class
ComplexNumberTest.java
/** * Complex number. */ public class ComplexNumberTest { public static void main(String [] args){ // Create ComplexNumber object. ComplexNumber cn1 = new ComplexNumber(4, 3); ComplexNumber cn2 = new ComplexNumber(2, -2); // Print complex numbers. System.out.println("The Complex Number 1 is "+ cn1 +"\nThe Complex Number 2 is "+ cn2); // Print complex numbers equal. if(cn1.equals(cn2)){ System.out.println("\nComplex Number 1 and Complex Number 2 are equal."); }else{ System.out.println("\nComplex Number 1 and Complex Number 2 are not equal."); } // Print complex numbers reciprocal. System.out.println("\nThe reciprocal of Complex Number 1 is " + cn1.reciprocal()); System.out.println("The reciprocal of Complex Number 2 is " + cn2.reciprocal()); // Print complex numbers reciprocal. System.out.println("\nThe conjugate of Complex Number 1 is " + cn1.conjugate()); System.out.println("The conjugate of Complex Number 2 is " + cn2.conjugate()); // Print complex numbers addition, subtraction, multiplication, division. System.out.println("\nComplex Number 1 + Complex Number 2 is " + cn1.add(cn2)); System.out.println("Complex Number 1 - Complex Number 2 is " + cn1.subtract(cn2)); System.out.println("Complex Number 1 * Complex Number 2 is " + cn1.multiply(cn2)); System.out.println("Complex Number 1 / Complex Number 2 is " + cn1.divide(cn2)); // Pring complex numbers angle. System.out.println("\nThe angle of Complex Number 1 is " + cn1.getAngle()); System.out.println("The angle of Complex Number 2 is " + cn2.getAngle()); // Print complex numbers magnitude. System.out.println("\nThe magnitude of Complex Number 1 is " + cn1.getMagnitude()); System.out.println("The magnitude of Complex Number 2 is " + cn2.getMagnitude()); } }
Yorum Yok:
Yorum Yap:
Yorum yapabilmek için giriş yapmalısınız.