(1)声明位于包test.exam中Point类,有坐标x、y两个私有成员变量,有一个返回与其它点的距离的方法,还有一个移动点的方法
package com.qst.fuxi;
public class Point {
double x;
double y;
public Point() {
}
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void movePoint(double dx,double dy) {
System.out.println("移动前的坐标为:"+this.x+this.y);
System.out.println("移动量:"+dx+dy);
this.x+=dx;
this.y+=dy;
System.out.println("移动后的坐标为:"+this.x+this.y);
}
}
测试函数
1、返回与其它点的距离
package com.qst.fuxi;
import java.util.Scanner;
import java.math.*;
public class PointDemo {
Point p1;
Point p2;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个点的x值x1");
Double x1 = scanner.nextDouble();
System.out.println("请输入第一个点的y值y1");
Double y1 = scanner.nextDouble();
System.out.println("请输入第二个点的x值x2");
Double x2 = scanner.nextDouble();
System.out.println("请输入第二个点的y值y2");
Double y2 = scanner.nextDouble();
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
PointDemo pd2= new PointDemo(p1,p2);
pd2.distance();
}
public void distance() {
Double a=Math.sqrt(Math.abs(p1.getX()-p2.getX())*Math.abs(p1.getX()-p2.getX())+Math.abs(p1.getY()-p2.getY())*Math.abs(p1.getY()-p2.getY()));
System.out.println(a);
}
public PointDemo() {
super();
}
public PointDemo(Point p1, Point p2) {
super();
this.p1 = p1;
this.p2 = p2;
}
public Point getP1() {
return p1;
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
}
2、移动点
package com.qst.fuxi;
public class PointMove {
public double x;
public double y;
public static void main(String[] args) {
Point p1=new Point(1.0,2.0);
Point p2=new Point(3.0,4.0);
p1.movePoint(p2.x,p2.y);
}
}
运行结果:
(2)声明一个位于包test.abc中的圆类,圆由圆心和