/** * Author: Josiah Yoder et al. * Class: SE1011-011 * Date: 10/21/13 1:20 AM * Lesson: Week 7, Day 1 */ public class Complex011_10_2 { private double real; private double imaginary; public static final double MY_CONSTANT = 5.0; public static int numberOfInstances = 0; public Complex011_10_2() { real = 0; imaginary = 0; numberOfInstances = numberOfInstances + 1; } public Complex011_10_2(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Complex011_10_2(double real) { // System.out.println("the double one"); this(real, 0); } public Complex011_10_2(long real) { this(real, 0); // System.out.println("the long one"); } public Complex011_10_2(Complex011_10_2 other) { this(other.real, other.imaginary); } public Complex011_10_2 add(Complex011_10_2 other) { Complex011_10_2 result = new Complex011_10_2(); result.real = this.real + other.real; result.imaginary = this.imaginary + other.imaginary; return result; } public Complex011_10_2 add(double real) { return add(new Complex011_10_2(real,0)); // Complex011_10_2 result = new Complex011_10_2(); // result.real = this.real + real; // result.imaginary = this.imaginary; // return result; } /* * Arithmetic inverse (aka -c) */ public Complex011_10_2 inverse() { Complex011_10_2 result = new Complex011_10_2(); result.real = -this.real; result.imaginary = -this.imaginary; return result; } /* * Subtract the other from this * this - other */ public Complex011_10_2 subtract(Complex011_10_2 other) { // Complex011_10_2 result = new Complex011_10_2(); // unused object return this.add(other.inverse()); // return add(other.inverse()); // alternative. Not as clear in this case. } /* * Add an imaginary number to the complex number * imagineN -- "b" in the expression b*i that represents the imaginary number */ public void incrementReal(double realN) { real = realN + real; } /* * Add an imaginary number to the complex number * imagineN -- "b" in the expression b*i that represents the imaginary number */ public void incrementImaginary(double imagineN) { // need a & b // What do we have? // this.real; // this.imaginary; imaginary = imagineN + imaginary; } // Return to Chapter 7 public void swap(Complex011_10_2 other) { // other.real; // this.real; double temp; temp = other.real; other.real = this.real; this.real = temp; temp = other.imaginary; other.imaginary = this.imaginary; this.imaginary = temp; // Assign a to b // b = a; // Assign a to be b // a = b; } public String toString() { return "("+real+", "+imaginary+")"; } // TODO: Implement hashCode as well. public boolean equals(Object obj) { Complex011_10_2 other = (Complex011_10_2) obj; System.out.println("This: "+this); System.out.println("other: " + other); final double threshold = 0.001; return Math.abs(this.real-other.real) < threshold && Math.abs(this.imaginary-other.imaginary) < threshold; } public Complex011_10_2 incrementALot(Complex011_10_2[] others) { // TODO: return null; } public double getReal(){ return real; } }