/** * Author: Josiah Yoder et al. * User: yoder * Class: SE1011-011 * Lesson: Week 8, Day 1 */ public class Compex051_8_1 { // The complex number is // real + imaginary * i // where i is sqrt(-1) private double real; private double imaginary; public Compex051_8_1() { real = 0; imaginary = 0; } public Compex051_8_1(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Compex051_8_1(double real) { this.real = real; imaginary = 0; } public Compex051_8_1(Compex051_8_1 old) { this.real = old.real; this.imaginary = old.imaginary; } // From main: // Compex051_8_1 c1 = new Compex051_8_1(3,4); // Compex051_8_1 c2 = new Compex051_8_1(1,2); // c1 = c1.add(c2); // += public Compex051_8_1 add(Compex051_8_1 other) { // this; // reference to this object // other; // reference to the other object Compex051_8_1 result = new Compex051_8_1(); this.real = this.real + other.real; this.imaginary = this.imaginary + other.imaginary; return result; } // // Treat complex number as a vector // and return the magnitude. // // That is, we want to find the length // of the vector from the origin // to the complex number // when plotted on a graph. public double magnitude() { // sqrt(x^2 + y^2) // sqrt(real^2 + imaginary^2) return Math.sqrt(real*real+imaginary*imaginary); } }