Java 题目看图 30

 我来答
樂學者
2018-05-21 · 超过26用户采纳过TA的回答
知道答主
回答量:96
采纳率:83%
帮助的人:25.9万
展开全部

主方法:

public class PointDis {

public static void main(String[] args) {
// TODO Auto-generated method stub
Point p1=new Point();
Point p2=new Point();
p1.setPoint(1, 1);
p2.setPoint(4, 5);
System.out.println(MyMath.distance(p1, p2));
}

}

Point类:

public class Point {
private double x,y;
public void setPoint(double x,double y) {
this.x=x;
this.y=y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}

}

MyMath类://这个类是关键

public class MyMath {
static double distance;
public static double distance(Point p1,Point p2){
double x=p2.getX()-p1.getX();  //两个坐标x差
double y=p2.getY()-p1.getY();  //两个坐标y差
distance=Math.sqrt(x*x+y*y);  //距离计算公式(勾股定理)
return distance;
}
}
a470198949
2018-05-21 · TA获得超过359个赞
知道小有建树答主
回答量:592
采纳率:59%
帮助的人:198万
展开全部
package com.Coordinate;

public class Point {
    
    private double x;
    private double 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;
    }

}
package com.Coordinate;

public class MyMath {
    
    private Point p1;
    private Point 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;
    }
    
    public MyMath() {
        
    }
    
    public MyMath(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }
    
    public double distance(Point p1, Point p2) {
        double X = p1.getX() - p2.getX();
        double Y = p1.getY() - p2.getY();
        double s = X * X + Y * Y;
        return Math.sqrt(s);        
    }
    
    public double distance() {
        double X = p1.getX() - p2.getX();
        double Y = p1.getY() - p2.getY();
        double s = X * X + Y * Y;
        return Math.sqrt(s);        
    }

}
package com.Coordinate;

import java.util.Scanner;

public class MainClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sin = new Scanner(System.in);
        Point [] p = new Point[2];
        for(int i=0; i<2; i++) {
            System.out.print("请输入第" + (i+1) + "个点的X坐标:");
            double x = sin.nextDouble();
            System.out.print("请输入第" + (i+1) + "个点的Y坐标:");
            double y = sin.nextDouble();
            p[i] = new Point();
            p[i].setX(x);
            p[i].setY(y);
        }
        
        MyMath myMath = new MyMath();
        double s = myMath.distance(p[0], p[1]);
        
//        MyMath myMath = new MyMath(p[0], p[1]);
//        double s = myMath.distance();
        
        System.out.println("两点之间的距离是:" + s);

    }

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
山梅2Z
2018-05-21 · 超过45用户采纳过TA的回答
知道答主
回答量:150
采纳率:20%
帮助的人:26.6万
展开全部
class Point{
private double x;
private double y;
point(double x,double y){
this.x=x;this.y=y;
}
get(),set()方法自己加
}
class MyMath{
double distance( Point a,poin b){
return Math.sqrt(Math.pow(a.getX()-b.getX(),2)+Math.pow(a.getY()-b.getY(),2) );
}
main方法自己写哈 谢谢采纳
追问
我需要完整加注释
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式