mypoint.java

来自「tutorial for java programming」· Java 代码 · 共 40 行

JAVA
40
字号
//Exercise 7.4
public class MyPoint {
	//variables
	private int x;
	private int y;
	//constructors
	public MyPoint(){//no args to initialize a point(0,0)
		x = 0;
		y = 0;
	}
	public MyPoint(int x, int y){//constructs a point with the specified x and y co‐ordinates
		this.x = x;
		this.y = y;
	}
	//getters and setters
	public int getX(){
		return x;
	}
	public void setX(int x){
		this.x = x;
	}
	public int getY(){
		return y;
	}
	public void setY(int y){
		this.y = y;
	}
	//methods
	public double distance(int x, int y){
		return Math.sqrt((this.x - x)*(this.x - x)+ (this.y - y)*(this.y - y) );
	}
	public double distance(MyPoint another){
		return Math.sqrt((x - another.x)*(x - another.x)+ (y - another.y)*(y - another.y));
	}
	//toString
	public String toString(){
		return "the point is ("+ x +"," + y +").";
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?