6-13 Point (10 分)

There is a class Point that is incompleted. Please complete the class according to the test code in 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.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 Point( ){
    
    
    this.x=0;
    this.y=0;
}
public Point(double x,double y){
    
    
    this.x=x;
    this.y=y;
}
public Point(Point o){
    
    
    this.x=o.x;
    this.y=o.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 l
    Point ss=(Point) l;
    ss.x+=this.x;
    ss.y+=this.y;
    return ss;//真的不要忘记类型的返回值啊
}

Guess you like

Origin blog.csdn.net/qq_51976555/article/details/117398225