Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Lu Liu
- * CSC-112 Intermediate Java Programming
- * FEB 15 2016
- * lliu0001@ student.stcc.edu
- *
- * Implementing Complex Numbers
- * define two doubles, known as the real part and the imaginary part.
- * define 4 constructors
- * define public methods
- */
- package chapter09;
- public class Complex {
- // define two doubles, known as the real part and the imaginary part.
- private double real;
- private double imag;
- // define 4 constructors
- public Complex(double real, double imag) {
- this.real = real;
- this.imag = imag;
- }
- public Complex(double real) {
- this.real = real;
- this.imag = 0;
- }
- public Complex() {
- this(0, 0);
- }
- // the constructor takes a String
- public Complex(String str) {
- String newStr = str.trim();
- char[] chars = newStr.toCharArray();
- if (newStr.indexOf("i") == -1)
- this.real = Double.parseDouble(newStr);
- else if (chars.length == 1) {
- this.imag = 1.0;
- } else if (chars.length == 2) {
- this.imag = (double) (chars[0] - '0');
- } else if (chars.length == 3) {
- if (Character.isDigit(chars[0])) {
- this.real = (double) (chars[0] - '0');
- this.imag = (chars[1] == '-') ? -1.0 : 1.0;
- } else
- this.imag = -(double) (chars[1] - '0');
- } else if (chars.length == 4) {
- if (Character.isDigit(chars[0])) {
- this.real = (double) (chars[0] - '0');
- this.imag = (chars[1] == '-') ? -(double) (chars[2] - '0') : (double) (chars[2] - '0');
- } else {
- this.real = -(double) (chars[1] - '0');
- this.imag = (chars[2] == '-') ? -1.0 : 1.0;
- }
- } else if (chars.length == 5) {
- this.real = -(double) (chars[1] - '0');
- this.imag = (chars[2] == '-') ? -(double) (chars[3] - '0') : (double)(chars[3] - '0');
- }
- }
- // define public methods
- public double getReal() {
- return real;
- }
- public double getImag() {
- return imag;
- }
- public Complex add(Complex c) {
- return new Complex((this.real + c.getReal()), (this.imag + c.getImag()));
- }
- public Complex subtract(Complex c) {
- return new Complex((this.real - c.getReal()), (this.imag - c.getImag()));
- }
- public Complex multiply(Complex c) {
- return new Complex((this.real * c.getReal() - this.imag * c.getImag()),
- (this.real * c.getImag() + c.getImag() * c.getImag()));
- }
- public Complex divide(Complex c) {
- double a = this.real;
- double b = this.imag;
- double x = c.getReal();
- double y = c.getImag();
- return new Complex((a * x + b * y) / (x * x + y * y), (b * x - a * y) / (x * x + y * y));
- }
- public double abs() {
- double a = this.getReal();
- double b = this.getImag();
- return Math.sqrt(a * a + b * b);
- }
- public Complex negate() {
- double a = this.getReal();
- double b = this.getImag();
- return new Complex(-a, -b);
- }
- public Complex conjugate() {
- return new Complex(this.getReal(), -this.getImag());
- }
- public double distance(Complex c) {
- double x = this.getReal() - c.getReal();
- double y = this.getImag() - c.getImag();
- return x * x + y * y;
- }
- public boolean equals(Complex c) {
- double minus = (this.abs() > c.abs()) ? (this.abs() - c.abs()) : -(this.abs() - c.abs());
- if (minus / this.abs() < 1E-6)
- return true;
- return false;
- }
- public boolean greaterThan(Complex c) {
- return (this.abs() > c.abs()) ? true : false;
- }
- public boolean lessThan(Complex c) {
- return !this.greaterThan(c);
- }
- public String toString() {
- if (this.getImag() > 0)
- return this.getReal() + "+" + (this.getImag() + "i");
- else if (this.getImag() == 0)
- return this.getReal() + "";
- else
- return this.getReal() + "" + (this.getImag() + "i");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement