6-6 TDVector (10分) java PTA

6-6 TDVector (10分)

6-6 TDVector (10分)
There is a class TDVector that is incompleted. Please complete the class according to the test code in Main.
函数接口定义:
class TDVector {
private double x;
private double y;
public String toString() {
return “(”+this.x+","+this.y+")";
}
}

裁判测试程序样例:
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(); // default ctor, x and y are zeros
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); // ctor by x and y
TDVector c = new TDVector(b); // ctor by another TDVector
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

/*

	作者:马志勇

*/

    public TDVector() {

         

    }

    public TDVector(double x, double y) {

         this.x = x;

         this.y = y;

    }

    public void setX(double x) {

         this.x = x;

    }

    public void setY(double y) {

         this.y = y;

    }

    public TDVector(TDVector b) {

         x = b.x;

         y = b.y;

    }

    public TDVector add(TDVector r) {

         TDVector tmp = new TDVector(this);

         tmp.x =tmp.x+r.x;

         tmp.y =tmp.y + r.y;

         return tmp;

    }

    public double getX() {

         return x;

    }

    public double getY() {

         return y;

    }

发布了10 篇原创文章 · 获赞 8 · 访问量 412

猜你喜欢

转载自blog.csdn.net/mzy1711231996/article/details/104806090
今日推荐