There is a class Point
that is incompleted. Please complete the class according to the test code in Main
.
//有一个未完成的类点。请根据 Main 中的测试代码完成课程。
函数接口定义:
class Point {
private double x;
private double y;
public String toString() {
return "("+this.x+","+this.y+")";
}
}
裁判测试程序样例:
import java.util.Scanner;
class Point {
private double x;
private double y;
public String toString() {
return "("+this.x+","+this.y+")";
}
/** 你所提交的代码将被嵌在这里(替换此行) **/
}
public class Main {
public static void main(String[] args) {
Point a = new Point(); // 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();
Point b = new Point(x, y); // ctor by x and y
Point c = new Point(b); // ctor by another Point
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();
}
}
答案
public Point( ){
this.x=0;
this.y=0;
}
public Point(double x,double y){
this.x=x;
this.y=y;
}
public Point(Point z){
this.x=z.x;
this.y=z.y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
public void setX(double m){
this.x=m;
}
public void setY(double n){
this.y=n;
}
public Point add(Object l){
Point ss=(Point) l;
ss.x+=this.x;
ss.y+=this.y;
return ss;
}