Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Canaan Khatib
- * CSE-112
- * Complex Input
- * Prof Silvestri
- * Cakhatib-colon0001@student.stcc.edu
- */
- import java.util.StringTokenizer;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Complex extends Number implements Comparable<Complex> {
- private enum STATES {
- INIT_STATE,
- LEADING_OPTIONAL_SIGN_READ,
- POSSIBLE_REALPART,
- REALPARTREAD_NOW_IMAGINARY_PART,
- IMAGINARY_PART_VALUE,
- COMPLEX_READ_IN_TOTAL,
- ERROR_STATE
- }
- private double imag = 0;
- private double real = 0;
- public final static Complex ZERO = new Complex();
- /*** Constructors ***/
- public Complex() {
- this.real = 0;
- this.imag = 0;
- }
- public Complex(double real, double imag) {
- this.real = real;
- this.imag = imag;
- }
- public Complex(double real) {
- this.real = real;
- this.imag = 0;
- }
- public Complex(String complexNumber) {
- double values[] = complexString(complexNumber);
- this.real = values[0];
- this.imag = values[1];
- }
- private double[] complexString(String str) {
- double values[] = new double[2];
- String strReg[] = {"^[+-]?(\\d+(\\.\\d*)?|\\.\\d+)$", "^([+-]?(\\d+(\\.\\d*)?|\\.\\d+))?)i$",
- "^([+-]?(\\d+)?(\\.\\d*)?|\\.\\d+)(\\s+)?(([+-](\\s+)?((\\d+(\\.\\d*)?|\\.\\d+))?)i)$"};
- Pattern pattern [] = new Pattern [5];
- Matcher match [] = new Matcher [5];
- for(int i = 0; i < strReg.length; i++) {
- pattern[i] = Pattern.compile(strReg[i]);
- match[i] = pattern[i].matcher(str);
- }
- // Only for real inputs
- if (match[0].find()) {
- real = Double.parseDouble(match[0].group());
- imag = 0;
- values[0]=Double.parseDouble(match[0].group());
- values[1] = 0;
- }
- return values;
- }
- /*** Getters ***/
- public double getImag() {
- return this.imag;
- }
- public double getReal() {
- return this.real;
- }
- /*** Setters ***/
- public void setReal(double real) {
- this.real = real;
- }
- public void setImag(double imag) {
- this.imag = imag;
- }
- /*** Operations ***/
- public Complex add(Complex other) {
- // a+bi + c+di is (a+c)+(b+d)i
- return new Complex(this.real + other.real, this.imag + other.imag);
- }
- public Complex subtract(Complex other) {
- // a+bi - c+di is (a-c)+(b-d)i
- return add(other.negate());
- }
- public Complex multiply(Complex other) {
- // (a+bi) * (c+di) is (ac-bd)+(ad+bc)i
- double ac = this.real * other.real;
- double bd = this.imag * other.imag;
- double ad = this.real * other.imag;
- double bc = this.imag * other.real;
- return new Complex(ac - bd, ad + bc);
- }
- public Complex divide(Complex other) {
- // (a+bi) / (c+di) is (ac+bd)/(c^2+d^2) + (bc-ad)/(c^2+d^2)i
- double a = this.real;
- double b = this.imag;
- double c = other.real;
- double d = other.imag;
- double quotientReal = ((a * c) + (b * d)) / ((c * c) + (d * d));
- double quotientImag = ((b * c) - (a * d)) / ((c * c) + (d * d));
- return new Complex(quotientReal, quotientImag);
- }
- public double abs() {
- // |a + bi| is sqrt(a^2 + b^2)
- return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2));
- }
- public Complex conjugate() {
- // Complex conjugate of a+bi is a-bi
- return new Complex(this.real, -this.imag);
- }
- public double distance(Complex other) {
- // Distance between a+bi and c+di is sqrt((a-c)^2 + (b-d)^2)
- return Math.sqrt(Math.pow(this.real - other.real, 2) + Math.pow(this.imag - other.imag, 2));
- }
- public Complex negate() {
- // -(a+bi) is -a-bi
- return new Complex(-this.real, -this.imag);
- }
- /*** Comparisons ***/
- public boolean equals(Complex other) {
- /*
- * If A is the larger complex number and B is the smaller one, A and B
- * are approximately equal if the following is true: (|A| - |B|)/|A| <
- * 1E-6 (one millionth)
- */
- return this.distance(other) / (this.greaterThan(other) ? this.abs() : other.abs()) < 1E-6;
- }
- public boolean greaterThan(Complex other) {
- return this.abs() > other.abs();
- }
- public boolean lessThan(Complex other) {
- return this.abs() < other.abs();
- }
- public String toString() {
- String toString = "";
- if (this.imag == 0.0)
- toString += this.real;
- else if (this.real == 0.0)
- toString += this.imag + "i";
- else {
- toString += this.real;
- toString += this.imag < 0 ? " - " : " + ";
- toString += Math.abs(this.imag) + "i";
- }
- return toString;
- }
- // Round components to the specified number of decimal places
- public String getRoundedtoString(int decimalPlaces) {
- // Assume negative input should be changed to positive
- decimalPlaces = Math.abs(decimalPlaces);
- // Ensure that any number which rounds to 0 is displayed as positive 0
- double realPart = Math.abs(this.real) < Math.pow(10, -decimalPlaces) ? 0.0 : this.real;
- double imagPart = Math.abs(this.imag) < Math.pow(10, -decimalPlaces) ? 0.0 : this.imag;
- // Format numbers according to decimal places
- String format = "%." + decimalPlaces + "f";
- String toString = realPart < 0 ? "-" : "";
- toString += String.format(format, Math.abs(realPart));
- toString += imagPart < 0 ? " - " : " + ";
- return toString + String.format(format + "i", Math.abs(imagPart));
- }
- @Override
- public int compareTo(Complex other) {
- double diff = this.abs() - other.abs();
- if (diff > 0)
- return 1;
- if (diff == 0)
- return 0;
- return -1;
- }
- @Override
- public double doubleValue() {
- return this.abs();
- }
- @Override
- public float floatValue() {
- return (float)doubleValue();
- }
- @Override
- public int intValue() {
- return (int)doubleValue();
- }
- @Override
- public long longValue() {
- return (long)doubleValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement