Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 2/19/16
- * Csc-112
- * Intro to Java
- * Implementing Complex Numbers p1
- * Ojnatour0001@student.stcc.edu or natour@cs.stcc.edu
- * Implement support for imaginary numbers through the use of objects and classes.
- */
- public class Complex { // decleration of doubles and constructors.
- private double real;
- private double imag;
- public Complex(double r, double i) {
- this.real = r;
- this.imag = i;
- }
- public Complex(double r) {
- this.real = r;
- this.imag = 0;
- }
- public Complex() {
- this.real = 0;
- this.imag = 0;
- }
- //this is where the regex constructor would go... I guess that means no java parties for me.
- /////////////////////////////////////////////////////////////////////////////////////////
- // public methods to manipulate the complex numbers.
- public Complex add(Complex c) {
- double r = this.real + c.real;
- double i = this.imag + c.imag;
- return new Complex(r, i);
- }
- public Complex subtract(Complex c) {
- double r = this.real - c.real;
- double i = this.imag - c.imag;
- return new Complex(r, i);
- }
- public Complex multiply(Complex c) {
- double r = (this.real * c.real) - (this.imag * c.imag);
- double i = (this.real * c.imag) + (this.imag * c.real);
- return new Complex(r, i);
- }
- public Complex divide(Complex c) {
- double r0 = (this.real * c.real) + (this.imag * c.imag);
- double r1 = ((c.real * c.real) + (c.imag * c.imag));
- double i0 = (this.imag * c.real) - (this.real * c.imag);
- double i1 = ((c.real * c.real) + (c.imag * c.imag));
- double r = r0 / r1;
- double i = i0 / i1;
- return new Complex(r, i);
- }
- public double abs() {
- return (Math.sqrt((this.real * this.real) + (this.imag * this.imag)));
- }
- public Complex negate() {
- double r = -1 * (this.real);
- double i = -1 * (this.imag);
- return new Complex(r, i);
- }
- public Complex conjugate() {
- return new Complex(this.real, -this.imag);
- }
- public double distance(Complex c) {
- return Math.sqrt(Math.pow(this.real - c.real, 2) + Math.pow(this.imag - c.imag, 2));
- }
- public boolean equals(Complex c) {
- double e;
- double d = this.distance(c);
- double abs1 = this.abs();
- double abs2 = c.abs();
- if (abs1 > abs2)
- e = d / abs1;
- else
- e = d / abs2;
- if (e < .000001)
- return true;
- return false;
- }
- public boolean greaterThan(Complex c) {
- double d0 = this.abs();
- double d1 = c.abs();
- if (d0 > d1)
- return true;
- return false;
- }
- public boolean lessThan(Complex c) {
- double d0 = this.abs();
- double d1 = c.abs();
- if (d0 < d1)
- return true;
- return false;
- }
- public String toString(Complex c) {
- double r = this.real;
- double i = this.imag;
- return (Double.toString(r) + " + " + Double.toString(i) + "i");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement