Вот программа на Java, которая принимает на вход координаты точки в системе координат x-y и выполняет различные операции:
import java.util.Scanner;
public class CoordinateSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the x-coordinate: ");
int x = scanner.nextInt();
System.out.print("Enter the y-coordinate: ");
int y = scanner.nextInt();
Coordinate point = new Coordinate(x, y);
// Method 1: Get the x-coordinate of the point
int pointX = point.getX();
System.out.println("X-coordinate: " + pointX);
// Method 2: Get the y-coordinate of the point
int pointY = point.getY();
System.out.println("Y-coordinate: " + pointY);
// Method 3: Check if the point is on the origin (0, 0)
boolean isOrigin = point.isOrigin();
System.out.println("Is the point on the origin? " + isOrigin);
// Method 4: Calculate the distance from the origin to the point
double distanceFromOrigin = point.calculateDistanceFromOrigin();
System.out.println("Distance from the origin: " + distanceFromOrigin);
}
}
class Coordinate {
private int x;
private int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isOrigin() {
return x == 0 && y == 0;
}
public double calculateDistanceFromOrigin() {
return Math.sqrt(x * x + y * y);
}
}
В этой программе у нас есть класс Coordinate
, который представляет точку в системе координат xy. У него есть методы для получения координат X и Y, проверки того, находится ли точка в начале координат, и расчета расстояния от начала координат. Метод main
принимает координаты x и y в качестве входных данных от пользователя, создает объект Coordinate
и демонстрирует использование этих методов.