标签:body ali point ble util color scn ext 第一个
描述
编写坐标系中的点类CPoint。 1.编写相应的构造方法初始化某个点; 2.重载构造方法初始化对角线的点; 3.编写重载方法计算点到原点,点到点,点到另一个坐标的距离。 输入两个点的坐标,若该点的横坐标与纵坐标相同如(5,5),则只需输入5即可。 输出两点与原点的距离、第一个点与固定点(-6,-8)的距离、两点的距离,结果保留两位小数。 |
一般 |
3 4 |
5.00 |
import java.util.Scanner; public class CPoint { public static void main(String[] args){ int x1,x,y1; Scanner scn=new Scanner(System.in); x1=scn.nextInt(); y1=scn.nextInt(); x=scn.nextInt(); Point p1 = new Point(x1,y1); Point p2 = new Point(x); System.out.println(String.format("%.2f", p1.distance())); System.out.println(String.format("%.2f", p2.distance())); System.out.println(String.format("%.2f", p1.distance(-6,-8))); System.out.println(String.format("%.2f", p1.distance(p2))); } } class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public Point(int x) { this(x, x);//重点 } public int getX() { return x; } public int getY() { return y; } public double distance(int x, int y) { return Math.sqrt(Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2)); } public double distance(Point p) { return distance(p.getX(), p.getY()); } public double distance() { return distance(0, 0); } }
标签:body ali point ble util color scn ext 第一个
原文地址:https://www.cnblogs.com/Blogwjl/p/12740180.html