CPoint-Java

CPoint-Java,第1张

编写坐标系的zhong 的点类Cpoint。1、编写相应的构造方法初始化某个点;2、重载构造方法初始化对角线的点;3、编写重载方法计算点到原点,点到点,点到另一个坐标的距离。
主函数如下:
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)));   
	}
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;
        this.y=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);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/874427.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存