Java第二次作业

1.作业内容

2. 代码借鉴

借鉴的代码 没想到2018年的还这么好用

3.最终代码

package OK;

import java.util.Scanner;


public class Test {
	
	private int a;
	private int b;
	
	public Test(int a, int b) {//定义一个方法Test,相当于int
		super();
		this.a = a;
		this.b = b;
	}

	public Test() {
		this.a = 0;
		this.b = 0;//要初始化
	}

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;//结合上一个public,意味着this.getA()=a
	}

	public int getB() {
		return b;
	}//区分新数和原数,原数用ab,新数用get

	public void setB(int b) {
		this.b = b;//区分实数和复数部分
	}
	
	@Override
	public String toString() { 
		return  a  + "+" + b + "i";//直接运用String库的拼接方法
	}

	public Test add(Test test) {//此时的Test test相当于int a
		return new Test(this.a + test.getA(), this.b + test.getB());
	}
	
	public Test sub(Test test) {
		return new Test(this.a - test.getA(), this.b - test.getB());
	}
	
	public Test mul(Test test) {
		int a = (this.a * test.a) - (this.b * test.b);
		int b = (this.a * test.b) + (this.b * test.a);
		Test result = new Test(a, b);
		return result;
	}
	public boolean equals(Test test) {
		return(this.a==test.a&&this.b==test.b);
	}

	public static void main(String[] args) { 
		Scanner scanner = new Scanner(System.in);
		System.out.println("第一个复数:");
		Test t1 = new Test(scanner.nextInt(), scanner.nextInt());
		System.out.println("第二个复数:");
		Test t2 = new Test(scanner.nextInt(), scanner.nextInt());
		System.out.println("(a + b) = " + t1.add(t2).toString());
		System.out.println("(a - b) = " + t1.sub(t2).toString());
		System.out.println("(a * b) = " + t1.mul(t2).toString());
		System.out.println("(a + b) + a = " + t1.add(t2).add(t1).toString());
		System.out.println("equal or not:"+t1.equals(t2));
	}

}

4.总结

没啥好说的全在注释里

5.

很喜欢成都,还想再去很多次

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值