Java 上机测验2020.4.17

4月17日上机课 堂上作业:
1、编写一个Point类,要求如下:
属性:横坐标、纵坐标(double类型)
方法:
(1) 无参数的构造方法:调用1个参数的构造方法,横、纵坐标都设置为0
(2) 包含1个参数的构造方法:调用2个参数的构造方法,横、纵坐标都设置为参数值
(3) 包含2个参数的构造方法:分别把参数的值赋值给横、纵坐标。
(4) double distance(); 求该点到原点的距离。
(可用double b=Math.sqrt( a );求某数的平方根 )
(5) double distance(double a, double b); 求该点到坐标为(a,b )的点的距离
(6) double distance(Point p);求该点到另一点p的距离。
(7) void print(); 输出该点的坐标。

代码

package game;

public class Person {

	public static void main(String[] args) {
		Point text1 = new Point();
		Point text2 = new Point(3);
		Point text3 = new Point(3, 4);
		
		text1.print();
		System.out.println(text2.distance());
		System.out.println(text3.distance(text2));
		System.out.println(text2.distance(12, 6));
	}
}
class Point{
	private double x,y;
	
	public Point() { this(0); }

	public Point(double x) { this(x, x); }

	public Point(double x, double y) {
		super();
		this.x = x;
		this.y = y;
	}
	public double distance() { return Math.sqrt(Math.pow(Math.abs(this.x), 2)+Math.pow(Math.abs(this.x), 2)); }
	public double distance(double a, double b) { return Math.sqrt(Math.pow(Math.abs(this.x-a), 2)+Math.pow(Math.abs(this.x-b), 2));  }
	public double distance(Point p) { return this.distance(p.x, p.y); }
	public void print() { System.out.println(this.x+" "+this.y); }	
}
发布了26 篇原创文章 · 获赞 11 · 访问量 2359

猜你喜欢

转载自blog.csdn.net/weixin_45696526/article/details/105578047
今日推荐