裁判测试程序样例中展示的是一段二维向量类TDVector的定义以及二维向量求和的Java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。
函数接口定义:
提示:需要补充的成员方法有: 1. 无参构造方法 2. 带参构造方法 3. getX 4. getY 5. setX 6. setY 7. add方法
裁判测试程序样例:
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);
c.setX(z);
a = b.add(c);
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
代码长度限制
16 KB
时间限制
400 ms
内存限制
TDVector()
{
x= 0.0;
y= 0.0;
}
TDVector(double x1,double y1){
x=x1;
y=y1;
}
public TDVector(TDVector b) {
x=b.x;
y=b.y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public void setX(double x1){
x=x1;
}
public void setY(double y1){
y=y1;
}
public TDVector add(TDVector c){
TDVector a = new TDVector();
a.x=this.x+c.x;
a.y+=this.y+c.y;
return a;
}