/* COMP 1006 - Day 04 Complex Class (with static method) mjh-2013 */ public class Complex{ int re_; // real part of the number int im_; // imaginary part of the number // method for adding two complex numbers public Complex plus(Complex other){ this.re_ += other.re_; // using "this" here is optional this.im_ += other.im_; return this; // using "this" here is needed } // another method for addition // this method modifies the calling object // by adding the input number to it public void addWith(Complex other){ this.re_ += other.re_; // using "this" is optional this.im_ += other.im_; } // static method for addition // there is no calling object with this method public static Complex add(Complex first, Complex second){ Complex answer = new Complex(); answer.re_ = first.re_ + second.re_; answer.im_ = first.im_ + second.im_; return answer; /* this method can be more compactly written as return Complex(first.re_ + second.re_, first.im_ + second.im_); */ } // constructors for complex numbers Complex(){ // zero paramater constructor re_ = 0; // could have put this.re_ = re; im_ = 0; // could have put this.im_ = im; } Complex(int re, int im){ this.re_ = re; // I will use "this" when I write code this.im_ = im; // You can do either. Just be consistent } }