java编程 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标 show(),构造函数有两

1. 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。

文件MyPoint.java

public class MyPoint{
    public double x,y;
    public MyPoint(){
        x = 0.0;
        y = 0.0;
    }
    public MyPoint(double x,double y){
        this.x = x;
        this.y = y;
    }
    public void show(){
        System.out.println("("+x+","+y+")");
    }
    public static MyPoint getMiddle(MyPoint p1,MyPoint p2){
        MyPoint p = new MyPoint((p1.x+p2.x)/2,(p1.y+p2.y)/2);
        return p;
    }
}

文件Test.java

public class Test{
    public static void main(String[] args){
        MyPoint p1 = new MyPoint();
        MyPoint p2 = new MyPoint(6.0,8.0);
        MyPoint p = MyPoint.getMiddle(p1,p2);
        System.out.print("点1:");
        p1.show();
        System.out.print("点2:");
        p2.show();
        System.out.print("中点:");
        p.show();
    }
}

望采纳~

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-05-03
public class Point {

public double x;

public double y;

Point(double x, double y) {
this.x=x;
this.y=y;
}
static Point getMiddle(Point point1,Point point2){
double px=(point1.x+point2.x)/2;
double py=(point1.y+point2.y)/2;
Point p=new Point(px,py);
return p;
}
void show(){
System.out.println("点的坐标:("+x+","+y+")");
}
public static void main(String[] args) {
Point point1=new Point(0,0);
point1.show();
Point point2=new Point(4,9);
point2.show();
Point p=getMiddle(point1,point2);
p.show();
}
}

java编程 定义一个类Point,代表一个点,public属性有x和y,方法有显示点...
文件MyPoint.java public class MyPoint{ public double x,y; public MyPoint(){ x = 0.0; y = 0.0; } public MyPoint(double x,double y){ this.x = x; this.y = y; } public void show(){ System.out.println("("+x+","+y+")"); ...

java编写一个表示坐标点的类(Point),其中包含x和y两个坐标点的值,并且...
public class Point{ private int x; private int y; public getX(){ return x; } public getY(){ return y; } public setX(int x){ this.x = x; } public setY(int y){ this.y = y; } public Point(int x, int y){ this.x = x; this.y = y...

定义一个点类Point,有横坐标x和纵坐标y,定义构造函数初始化点,定义displ...
classPoint{public:Point(inta,intb):x(a),y(b){cout~Point(){coutvoidshow(){coutdoubledistance (Pointp){returnsqrt((p.y-y)*(p.x-y)+(p.x-x)*(p.x-x));}private:intx;inty;};intmain(){Pointa(0,0);Pointb(1,1);coutretu...

Java定义一个Point(点)类
public static void main(String[] args){ Point p1=new Point();Point p2=new Point(1,2);p1.show();p1.move(3,4);p1.show();p2.show();p2.move(5,6);p2.show();} Point(){ this(0,0);} Point(float x,float y){ this.x=x;this.y=y;} void move(float x,float ...

Java面向对象程序设计 习题 设计一个名为MyPoint的类表示一个点(x...
\\n" + ex 也就是第二个参数是消息框弹出消息的内容。"BinaryRain 也就是第三个参数是消息框的标题。JOptionPane.ERROR_MESSAGE 也就是第四个参数,是消息框的类型,表示错误提示,弹出来的消息框有个红色的叉叉标识,如JOptionPane.WARNING_MESSAGE,表示警告提示,弹出来的消息框有个叹号标识。

一个Point(点)类,该类具有x,y(表示点的横、纵坐标)两个属性,并定义两个...
} public void show(){ System.out.println("("+x+","+y+")");} } 测试该类的如下代码 public class test { \/ param args \/ public static void main(String[] args) { Point point1 =new Point();point1.show();Point point2 =new Point(2,3);point2.show();} } 应该是这样...

定义一个坐标类Pointer,成员变量包括x和y;成员方法有: (1)构造方法...
private double y;public Pointer(){ } public Pointer(double x, double y){ 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;} publi...

java创建一个Point类,包含坐标x、y。然后创建一个MyPoint类,定义两个...
class Point{ int x, y;} class MyPoint{ Point start, end;public MyPoint(Point start, Point end){ this.start=start;this.end=end;} double calc(){ return start.x-end.x+start.y-end;\/\/其实使用三角函数计算。。。http:\/\/zhidao.baidu.com\/question\/918415034931906699 } } ...

定义一个Point类,其属性包括点的坐标,提供计算两点之间的距离_百度知 ...
{double x,y;friend class Rectangle;public:Point(){x=y=0;} Point(double a,double b){x=a;y=b;} double distance(Point& p1,Point& p2){return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));double distance(Point& p){return sqrt((x-p.x)*(x-...

java编程题,写一个point类,计算两个point实例间的距离
Point类Point.java public class Point { 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 ...

相似回答