java编程 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标 show(),构造函数有两 10
1.定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方...
1. 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。
展开
2个回答
展开全部
文件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();
}
}
望采纳~
展开全部
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();
}
}
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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询