0
Follow
0
View

java finds the quadrant of a point and the distance between points

ding_ni_na 注册会员
2023-02-25 18:17

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Point p1 = new Point(scanner.nextDouble(), scanner.nextDouble());
        Point p2 = new Point(scanner.nextDouble(), scanner.nextDouble());
        int quadrant1 = p1.getQuadrant();
        int quadrant2 = p2.getQuadrant();
        double distance = p1.calDistance(p2);
        System.out.println(quadrant1 + " " + quadrant2 + " " + Math.round(distance));
    }
}

class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public int getQuadrant() {
        if (x > 0 && y > 0) {
            return 1;
        } else if (x < 0 && y > 0) {
            return 2;
        } else if (x < 0 && y < 0) {
            return 3;
        } else if (x > 0 && y < 0) {
            return 4;
        } else {
            return 0;
        }
    }

    public double calDistance(Point p) {
        double dx = x - p.getX();
        double dy = y - p.getY();
        return Math.sqrt(dx * dx + dy * dy);
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}

1. First create two Point objects p1 and p2 in the main function and read the coordinates of the two points respectively from the standard input.
2. Then call the getQuadrant() method of p1 and p2, respectively, to find the quadrants where the two points are located.
3. Next, call p1's calDistance() method to calculate the distance between the two points and round it to an integer.
4. Finally, output the three values in sequence.

Note: The calDistance() method calculates the inherit distance from two points based on their coordinate difference, i.e.
( x 1 x 2 < /span> < 2 + ( y 1 y < /span> 2


where $x_1$and $y_1$are the coordinates of the first point and $x_2$and $y_2$are the coordinates of the second point. Finally, the Math.round() method is used to round the result to an integer.

liuyun5459 注册会员
2023-02-25 18:17

This answer quotes ChatGPT

In this program, the Point class represents a point with two properties, x and y, representing the abscissa and ordinate, respectively. It has two methods getQuadrant and calDistance, which calculate the quadrant where the point is and the distance between two points respectively. The Main class reads the coordinates of the two points, creates the two Point objects, calculates the quadrants and distances, and outputs the results.

The

getQuadrant method determines the quadrant where the point is based on the symbol of x and y. If x and y are both positive, it's in the first quadrant; If x is negative and y is positive, it's in the second quadrant; If x and y are both negative, it's in the third quadrant; Otherwise it's in the fourth quadrant.

calDistance method is used to calculate the distance between two points, Pythagorean theorem formula is used to calculate the linear distance between two points, and DecimalFormat is used to round the distance to retain one decimal place.

import java.text.DecimalFormat;

class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public int getQuadrant() {
        if (x > 0 && y > 0) {
            return 1;
        } else if (x < 0 && y > 0) {
            return 2;
        } else if (x < 0 && y < 0) {
            return 3;
        } else {
            return 4;
        }
    }

    public double calDistance(Point other) {
        double deltaX = x - other.x;
        double deltaY = y - other.y;
        double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
        return Double.parseDouble(new DecimalFormat("#.0").format(distance));
    }
}

public class Main {
    public static void main(String[] args) {
        // 读入两个点的坐标
        java.util.Scanner in = new java.util.Scanner(System.in);
        double x1 = in.nextDouble();
        double y1 = in.nextDouble();
        double x2 = in.nextDouble();
        double y2 = in.nextDouble();
        in.close();

        // 创建两个点对象
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);

        // 计算象限和距离
        int quadrant = p1.getQuadrant();
        double distance = p1.calDistance(p2);

        // 输出结果
        System.out.println(quadrant + " " + distance);
    }
}


dragenlong 注册会员
2023-02-25 18:17

Refer to gpt and own ideas, here is the Java Point class design and Main class implementation:


import java.util.Scanner;

class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public int getQuadrant() {
        if (x > 0 && y > 0)
            return 1;
        else if (x < 0 && y > 0)
            return 2;
        else if (x < 0 && y < 0)
            return 3;
        else if (x > 0 && y < 0)
            return 4;
        else
            return 0;
    }

    public double calDistance(Point other) {
        double dx = x - other.x;
        double dy = y - other.y;
        return Math.round(Math.sqrt(dx * dx + dy * dy));
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();

        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);

        int quadrant = p1.getQuadrant();
        double distance = p1.calDistance(p2);

        System.out.printf("%d %d\n", quadrant, (int) distance);
    }
}


The input format is two lines of two numbers, representing the horizontal and vertical coordinates of the two points. The program calculates the quadrants of the two points and the distance between them, and outputs them on a line, separated by a space, preserving the integer portion of the distance.

duanyu11111 注册会员
2023-02-25 18:17

In the Point class, you can define two properties, x and y, and a constructor to initialize them. Then, two methods, getQuadrant and calDistance, are implemented to calculate the quadrant where the point is located and the distance between two points. In the calDistance method, the Math.round and/operators are used to round the result and keep two decimal places.

In the main method, use the Scanner class to read the coordinates of two points from the standard input and create two objects of the Point class. Next, the getQuadrant and calDistance methods are called to output the results. Because the calDistance method has been rounded to preserve two decimal places, the result can be printed directly without having to format the output using the System.out.printf method. The specific code is as follows:

import java.util.Scanner;

public class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public int getQuadrant() {
        if (x > 0 && y > 0) {
            return 1;
        } else if (x < 0 && y > 0) {
            return 2;
        } else if (x < 0 && y < 0) {
            return 3;
        } else if (x > 0 && y < 0) {
            return 4;
        } else {
            return 0; // 不在任何一个象限,可以抛出异常
        }
    }

    public double calDistance(Point p) {
        double dx = x - p.x;
        double dy = y - p.y;
        return Math.round(Math.sqrt(dx * dx + dy * dy) * 100) / 100.0; // 四舍五入保留两位小数
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        System.out.printf("%d %.2f", p1.getQuadrant(), p1.calDistance(p2));
    }
}

qklwdd1 注册会员
2023-02-25 18:17

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > here are the realization of the Point class:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x1 = scanner.nextInt();
        int y1 = scanner.nextInt();
        int x2 = scanner.nextInt();
        int y2 = scanner.nextInt();
        Point point1 = new Point(x1, y1);
        Point point2 = new Point(x2, y2);
        System.out.println(point1.getQuadrant() + " " + point2.getQuadrant() + " " + point1.calDistance(point2));
    }

    static class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getQuadrant() {
            if (x > 0 && y > 0) {
                return 1;
            } else if (x < 0 && y > 0) {
                return 2;
            } else if (x < 0 && y < 0) {
                return 3;
            } else if (x > 0 && y < 0) {
                return 4;
            } else {
                return -1;
            }
        }

        public double calDistance(Point p) {
            double dx = this.x - p.x;
            double dy = this.y - p.y;
            return Math.round(Math.sqrt(dx * dx + dy * dy) * 100) / 100.0;
        }
    }
}


In the above implementations, the getQuadrant method in the Point class implements the function of calculating the quadrant where the point is located, and the calDistance method implements the function of calculating the distance between the point and another point. In the Main method of the main class, you read the coordinates of two points, create two Point objects, compute their quadrants and distances, respectively, and print the results.

fcbzyy 注册会员
2023-02-25 18:17

Part of the answer references GPT, GPT_Pro better solve the problem

public class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // 计算点所在象限
    public int getQuadrant() {
        if (x > 0 && y > 0) {
            return 1;
        } else if (x < 0 && y > 0) {
            return 2;
        } else if (x < 0 && y < 0) {
            return 3;
        } else if (x > 0 && y < 0) {
            return 4;
        } else {
            return 0;
        }
    }

    // 计算两点之间的距离,小数部分四舍五入 
    public int calDistance(Point p) {
        double distance = Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
        return (int) Math.round(distance);
    }

    public static void main(String[] args) {
        Point p1 = new Point(12, 34);
        Point p2 = new Point(-2, -5);

        System.out.println("p1的象限为:" + p1.getQuadrant());  // 输出 p1的象限为:1 
        System.out.println("p1到p2的距离为:" + p1.calDistance(p2)); // 输出 p1到p2的距离为:113 
    }
}

The code above implements a Point class that has two properties, the abscissa and the ordinate, and two methods: getQuadrant for calculating the quadrant where the point is, and calDistance for calculating the distance between two points, with the decimal part rounded. In the main method, we create two Point objects. Using the getQuadrant method, we can figure out that the p1 quadrant is 1, and using the calDistance method, we can figure out that the distance from p1 to p2 is 113.
If the answer is helpful, please accept it.

About the Author

Question Info

Publish Time
2023-02-25 18:17
Update Time
2023-02-25 18:17

Related Question

Java的throw 和finally相关问题

java,No line found

如何在Java微配置文件中正确地编写异步POST/GET

为新的谷歌登录使用自定义按钮(javascript)

javaee“<%!”和“%>”标记的用法?

Java中的对集合便利的方式有区别吗

java.lang.UnsatisfiedLinkError:无法找到DSO来加载:libjscexecutor.so

Aws javascript SDK v3签名不匹配错误

我如何创建水平饼图,因为我附加的图片在java

如何调试'-cp'选项在Java似乎不工作?(复制)