JAVA 定义一个Point类 它的对象是指一个平面上的点(x,y),在定义Point类中要定义它的三个构造函数
JAVA 定义一个Point类 它的对象是指一个平面上的点(x,y),在定义Point类中要定义它的三个构造函数
定义一个Point类 它的对象是指一个平面上的点(x,y),在定义Point类中要定义它的三个构造函数,并且要重写toString()方法.定义二点距离的方法distance(…),并在main()主程序中调用这些方法,例如Point t=new Point(3.0,5.0); System.out.println(t.toString().); 打印的结果为:[3.0,5.0].
Point t1=new Point(3.0,7.0);System.out.println(distance(t,t1)); 打印的结果t 和t1二点距离为:2
public class Point { \x05double x;double y; Point(){ System.out.println("ok");} Point(double x){ this.x=x;} Point(double x,double y){ this.x=x;this.y=y;} public String toString (){ return ("ok");} public static double distance (Point p1,Point p2){ return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));} public static void main(String[] args) { Point t1;Point t2; t1=new Point(3.0,5.0); t2=new Point(6.0,9.0); System.out.print(t1.toString()); System.out.print(‘[’+t2.x+’,’+ t2.y+’]’); System.out.print(Point.distance(t1,t2)); }
好的话可以加分.
例如Point t=new Point(3.0,5.0); System.out.println(t.toString().); 打印的结果为:[3.0,5.0].
Point t1=new Point(3.0,7.0);System.out.println(distance(t,t1)); 打印的结果t 和t1二点距离为:2
public class Point { \x05
double x;double y;
Point(){ System.out.println("ok");}
Point(double x)
{ this.x=x;}
Point(double x,double y){ this.x=x;this.y=y;}
public String toString (){
return ("ok");}
public static double distance (Point p1,Point p2){
return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}
public static void main(String[] args) {
Point t1;Point t2;
t1=new Point(3.0,5.0);
t2=new Point(6.0,9.0);
System.out.print(t1.toString());
System.out.print(‘[’+t2.x+’,’+ t2.y+’]’);
System.out.print(Point.distance(t1,t2));
我帮你