Java 题目看图 30
3个回答
展开全部
主方法:
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;
}
}
展开全部
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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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方法自己写哈 谢谢采纳
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方法自己写哈 谢谢采纳
追问
我需要完整加注释
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询