mycomplex.java

来自「tutorial for java programming」· Java 代码 · 共 58 行

JAVA
58
字号
//Exercise 7.6
public class MyComplex {
	//instance variable
	private int real;
	private int imag;

	//constructor
	public MyComplex(int real, int imag){
			this.real = real;
			this.imag = imag;
	}

	//getter and setter
	public int getReal(){
		return real;
	}
	public int getImag(){
		return imag;
	}
	public void setReal(int real){
		this.real = real;
	}
	public void setImag(int imag){
		this.imag = imag;
	}


	//other
	
	public boolean isReal(){
		return (imag == 0);
	}
	public boolean isImag(){
		return (real == 0);
	}
	public boolean equals(int real, int imag){
		return (this.real == real && this.imag == imag);
	}
	public boolean equals(MyComplex another){
		return (this.real == another.real && this.imag == another.imag);
	}
	//x + jy
	public String toString(){
		return real + " + " + imag +"j";
	}
	public MyComplex add(MyComplex another){
		this.real += another.real;
		this.imag += another.imag;
		return this;
	}
	public MyComplex multiply(MyComplex another){
		this.real = this.real * another.real - this.imag * another.imag;
		this.imag = -(this.real *another.imag + this.imag * another.real);
		return this;
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?