题:
计算机画图时,有点的概念,每个点由它的横坐标x 和 纵坐标 y 描述。
写一个类。
求两个点之间的曼哈顿距离 = 横向距离 + 纵向距离
例如,一个点(0,0) 和另一个点(1,1)的曼哈顿距离为2
1package test; 2 3public class Point 4{ 5 6 public Point() 7 { 8 // TODO Auto-generated constructor stub 9 } 10 11 public int x; 12 public int y; 13 14 public int manhattan(Point n) 15 { 16 int dx = Math.abs(n.x - this.x); 17 int dy = Math.abs(n.y - this.y); 18 return dx + dy; 19 } 20} 21 22 23 24package test; 25 26public class test 27{ 28 29 public test() 30 { 31 // TODO Auto-generated constructor stub 32 } 33 34 public static void main(String[] args) 35 { 36 // TODO Auto-generated method stub 37 Point p1 = new Point(); 38 p1.x = 4; 39 p1.y = 5; 40 Point p2 = new Point(); 41 p2.x = 3; 42 p2.y = 5; 43 int d = p1.manhattan(p2); 44 System.out.println(d); 45 46 } 47 48}