⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javashy31.java

📁 《JAVA2简明教程》课后习题及部分实验内容~
💻 JAVA
字号:
/********************************************************************************************
   					第4章习题2 
定义一个复数类complex,它的内部具有两个实例变量:realPart和imagPart,分别代表复数的实部和虚部,
编程实现要求的数学运算。
(1)	实现两个复数相加。复数加运算的原则是:复数的实部和虚部分别相加。
(2)	实现两个复数相减。复数减运算的原则是:复数的实部和虚部分别相减。
(3)	输出运算结果,判断是否正确。

********************************************************************************************/

public class Javashy31 {

	private double realPart;
	private double imagePart;
	public Javashy31() {
		this.realPart = 0.0;
		this.imagePart = 0.0;
	}

	public Javashy31(double real, double image) {
		this.realPart = real;
		this.imagePart = image;
	}
	public void show() {
		if ((this.realPart == 0.0) && (this.imagePart == 0.0))
			System.out.println("0");
		else if (this.realPart == 0.0)
			System.out.println(this.imagePart + "i");
		else if (this.imagePart == 0.0)
			System.out.println(this.realPart);
		else
			System.out.println(this.realPart + "+" + this.imagePart + "i");
	}

	public void add(Javashy31 x, Javashy31 y) {
		this.realPart = x.realPart + y.realPart;
		this.imagePart = x.imagePart + y.imagePart;
	}

	public void sub(Javashy31 x, Javashy31 y) {
		this.realPart = x.realPart - y.realPart;
		this.imagePart = x.imagePart - y.imagePart;
	}

	public static void main(String arg[]) {
		Javashy31 c1 = new Javashy31(1, 2);
		Javashy31 c2 = new Javashy31(2, 2);
		Javashy31 c3 = new Javashy31();

		c1.show();
		c2.show();

		c3.add(c1, c2);
		System.out.print("add : ");
		c3.show();
		c3.sub(c1, c2);
		System.out.print("sub : ");
		c3.show();
	}

}

⌨️ 快捷键说明

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