|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
裁判测试程序样例中展示的是一段二维向量类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
这个题目是什么意思啊,根本看不懂
这个题目要求你完成一个二维向量类TDVector的代码。你需要补充完整的代码以保证测试程序正常运行。
首先,你需要在TDVector类中添加无参构造方法和带参构造方法。无参构造方法用于创建一个默认的向量,其中x和y的值都为0。带参构造方法用于根据给定的参数创建一个具有指定x和y值的向量。
其次,你需要添加getX和getY方法,这两个方法分别用于获取向量的x和y值。
然后,你需要添加setX和setY方法,这两个方法分别用于设置向量的x和y值。
最后,你需要添加add方法,该方法接受一个TDVector对象作为参数,并返回一个新的TDVector对象,表示两个向量的和。
以下是完整的代码实现:
- import java.util.Scanner;
- class TDVector {
- private double x;
- private double y;
- public TDVector() {
- this.x = 0;
- this.y = 0;
- }
- public TDVector(double x, double y) {
- this.x = x;
- this.y = y;
- }
- public TDVector(TDVector vector) {
- this.x = vector.x;
- this.y = vector.y;
- }
- public double getX() {
- return this.x;
- }
- public double getY() {
- return this.y;
- }
- public void setX(double x) {
- this.x = x;
- }
- public void setY(double y) {
- this.y = y;
- }
- public TDVector add(TDVector vector) {
- double newX = this.x + vector.x;
- double newY = this.y + vector.y;
- return new TDVector(newX, newY);
- }
- 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();
- }
- }
复制代码
你可以将这段代码复制到你的IDE中进行编译和运行,然后输入3.14、1.9和2.72,即可得到与输出样例相同的结果。
|
|