求解释Java编程问题
设计名为MyPoint的类表示一个平面坐标点--具有x坐标和y坐标的点,该类包括,成员变量:x、y;double型;无参数的构造方法:MyPoint(),创建坐标点0,0...
设计名为MyPoint的类表示一个平面坐标点--具有x坐标和y坐标的点,该类包括,
成员变量:x、y;double型;
无参数的构造方法:MyPoint( ),创建坐标点0,0);
带参数的构造方法:MyPoint(double x,double y)创建一个坐标点(x,y);
成员方法:getX()、getY(),返回坐标点相应的值;
成员方法:distance(),返回MyPoint类型的两个点之间的距离;
编写测试程序创建两个点(0,0)和(10,30.5),并显示它们之间的距离。 展开
成员变量:x、y;double型;
无参数的构造方法:MyPoint( ),创建坐标点0,0);
带参数的构造方法:MyPoint(double x,double y)创建一个坐标点(x,y);
成员方法:getX()、getY(),返回坐标点相应的值;
成员方法:distance(),返回MyPoint类型的两个点之间的距离;
编写测试程序创建两个点(0,0)和(10,30.5),并显示它们之间的距离。 展开
2个回答
展开全部
public class MyPoint {
private double X;
private double Y;
public double getX() {
return X;
}
public void setX(double x) {
X = x;
}
public double getY() {
return Y;
}
public void setY(double y) {
Y = y;
}
public MyPoint(double x, double y) {
super();
X = x;
Y = y;
}
public MyPoint() {
super();
}
public double distanceX(MyPoint p1,MyPoint p2){
return Math.abs((p1.getX()-p2.getX());
}
public double distanceY(MyPoint p1,MyPoint p2){
return Math.abs(p1.getY()-p2.getY());
}
public double distance(MyPoint p1,MyPoint p2){
return Math.sqrt(distanceX(p1, p2)*distanceX(p1, p2)+distanceY(p1, p2)*distanceY(p1, p2));
}
public static void main(String[] args) {
MyPoint p1=new MyPoint(0,0);
MyPoint p2=new MyPoint(10,30.5);
double result1 = new MyPoint().distanceX(p1, p2);
double result2 = new MyPoint().distanceX(p1, p2);
double result3 = new MyPoint().distanceX(p1, p2);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
}
private double X;
private double Y;
public double getX() {
return X;
}
public void setX(double x) {
X = x;
}
public double getY() {
return Y;
}
public void setY(double y) {
Y = y;
}
public MyPoint(double x, double y) {
super();
X = x;
Y = y;
}
public MyPoint() {
super();
}
public double distanceX(MyPoint p1,MyPoint p2){
return Math.abs((p1.getX()-p2.getX());
}
public double distanceY(MyPoint p1,MyPoint p2){
return Math.abs(p1.getY()-p2.getY());
}
public double distance(MyPoint p1,MyPoint p2){
return Math.sqrt(distanceX(p1, p2)*distanceX(p1, p2)+distanceY(p1, p2)*distanceY(p1, p2));
}
public static void main(String[] args) {
MyPoint p1=new MyPoint(0,0);
MyPoint p2=new MyPoint(10,30.5);
double result1 = new MyPoint().distanceX(p1, p2);
double result2 = new MyPoint().distanceX(p1, p2);
double result3 = new MyPoint().distanceX(p1, p2);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
}
展开全部
package com.pb.test;
public class MyPoint { private double x;
private double y;
public MyPoint(){
this.x = 0;
this.y = 0;
}
public MyPoint(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;
}
public double distance(MyPoint point1, MyPoint point2) {
return Math.sqrt((point1.x - point2.x) * (point1.x - point2.x)
+ (point1.y - point2.y)*(point1.y - point2.y));
}
}
看看这个,修改了distance方法,通过了测试
public class MyPoint { private double x;
private double y;
public MyPoint(){
this.x = 0;
this.y = 0;
}
public MyPoint(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;
}
public double distance(MyPoint point1, MyPoint point2) {
return Math.sqrt((point1.x - point2.x) * (point1.x - point2.x)
+ (point1.y - point2.y)*(point1.y - point2.y));
}
}
看看这个,修改了distance方法,通过了测试
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询