6-5 二维向量定义及加法运算 (10分)

裁判测试程序样例中展示的是一段二维向量类TDVector的定义以及二维向量求和的Java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。

函数接口定义:
请仔细阅读主程序中的相关调用。

裁判测试程序样例:
import java.util.Scanner;
class TDVector {
private double x;
private double y;
public String toString() {
return “(”+this.x+","+this.y+")";
}

/** 你所提交的代码将被嵌在这里(替换此行) **/

}
public class Main {
public static void main(String[] args) {
TDVector a = new TDVector(); /* 无参构造,向量的x和y默认为0 /
Scanner sc = new Scanner(System.in);
double x,y,z;
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
TDVector b = new TDVector(x, y); /
按照参数构造向量的x和y /
TDVector c = new TDVector(b); /
按照向量b构造向量 */
a.setY(z);
System.out.println(a);
System.out.println(b);
System.out.println©;
c.setX(z);
a = b.add©;
System.out.println(a);
System.out.println(“b.x=”+b.getX()+" b.y="+b.getY());
sc.close();
}
}
输入样例:
在这里给出一组输入。例如:

3.14 1.9 2.72
输出样例:
在这里给出相应的输出。例如:

(0.0,2.72)
(3.14,1.9)
(3.14,1.9)
(5.86,3.8)
b.x=3.14 b.y=1.9

TDVector(){
    	
    }
    TDVector(double x,double y){
    	this.x=x;
    	this.y=y;
    }
    TDVector(TDVector A){
    	this.x=A.x;
    	this.y=A.y;
    }
   void setX(double z){
    	this.x=z;
    }
   void setY(double z) {
	   this.y=z;
   }
   double getX() {
	   return x;
   }
   double getY() {
	   return y;
   }
public TDVector add(TDVector C) {
		C.x=C.x+this.x;
		C.y=C.y+C.y;
		return C;
	}


  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值