R7-27 点

点是最简单的形,是几何图形最基本的组成部分。要求设计一个点类Point类,Point类包括:

  1. 坐标成员变量 int x,int y
  2. 成员变量x,y的setter gette方法,以及ToString方法
  3. 带参无参构造方法
  4. 重写hashcode和equals方法,判断两个点是否相同
  5. 实现接口Comparable,可以比较两个点的大小,比较规则是若第一个点x大,则第一个点大,第一个点x小则第一个点小,若相同,比较y,根据y值判断大小。
  6. 实现方法 int distance(Point p2),求两个点的距离(开平方根使用方法Math.sqrt())

测试类中main方法中,要求如下:

  1. 通过键盘输入2个点坐标,创建2个点对象
  2. 输出第一个点的信息(调用ToString方法),
  3. 比较他们的大小,如果第一点大于等于第二个点,输出true否则输出false
  4. 求这两个点的距离并输出。

### 输入格式:

请在这里写输入格式。例如:输入在一行中给出2个绝对值不超过1000的整数A和B。

输出格式:

输入两行,每行为一个点的坐标

输入样例:

在这里给出一组输入。例如:

4 6
6 7
4 5
3 2

输出样例:

在这里给出相应的输出。例如:

Point [x=4, y=6]
false
2
Point [x=4, y=5]
true
3

 代码如下:

共两种做法,其实差不多,只是改了接口的属性

第一种:


import java.util.Objects;
import java.util.Scanner;
class Point implements Comparable{
    private int x;
    private int y;
    public Point(int x,int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point [" +
                "x=" + x +
                ", y=" + y +
                ']';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Point point = (Point) o;
        return x == point.x && y == point.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }


    @Override
    public int compareTo(Object o) {
        Point p1 = (Point)o;
//先比较x
        if(this.x>p1.x){
           return 1;
        }else if(this.x<p1.x) {
            return -1;
        }else if(this.y>p1.y){
//x相同,则比较y
            return 1;
        }else if(this.y<p1.y){
            return -1;
        }else{
            return 0;         
        }
    }
    public int distance(Point p2){
        double result = (this.x-p2.x)*(this.x-p2.x)+(this.y-p2.y)*(this.y-p2.y);

        return (int) Math.sqrt(result);
    }

}
public class Main {
    public static void main(String[] args) {
        Scanner sc=  new Scanner(System.in);
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
       Point p1 = new Point(x1,y1);
       Point p2 = new Point(x2,y2);
        System.out.println(p1.toString());
//p1.compareTo(p2)如果等于0,则说明p1的点与p2的点相同
      if(p1.compareTo(p2)>=0){
           System.out.println("true");
       }else if(p1.compareTo(p2)<=0) {
           System.out.println("false");
       }
        System.out.println(p1.distance(p2));
    }
}

第二种:

import java.util.Objects;
import java.util.Scanner;
class Point implements Comparable<Point>{
    private int x;
    private int y;
    public Point(int x,int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point [" +
                "x=" + x +
                ", y=" + y +
                ']';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Point point = (Point) o;
        return x == point.x && y == point.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }


    @Override
    public int compareTo(Point o) {
        //Point p1 = (Point)o;
        if(this.x>o.x){
           return 1;
        }else if(this.x<o.x) {
            return -1;
        }else if(this.y>o.y){
            return 1;
        }else if(this.y<o.y){
            return -1;
        }else{
            return 0;
        }

    }
    public int distance(Point p2){
        double result = (this.x-p2.x)*(this.x-p2.x)+(this.y-p2.y)*(this.y-p2.y);

        return (int) Math.sqrt(result);
    }

}
public class Main {
    public static void main(String[] args) {
        Scanner sc=  new Scanner(System.in);
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
       Point p1 = new Point(x1,y1);
       Point p2 = new Point(x2,y2);
        System.out.println(p1.toString());
       if(p1.compareTo(p2)>=0){
           System.out.println("true");
       }else if(p1.compareTo(p2)<=0) {
           System.out.println("false");
       }
        System.out.println(p1.distance(p2));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hu_66666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值