Bài tập Java OOP: tạo lớp Point theo yêu cầu | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points


Create a Point class in the Point.java file with the following information:


  • Point class stores the information of coordinates of a point in a two-dimensional Cartesian coordinate system.
  • x, y are attributes that describe the abscissa and ordinate of the objective Point class.
  • Point() is the no-arg constructor.
  • Point(x: double, y: double) is the constructor parameter which is used to initialize the values to x (abscissa) và y (ordinate).
  • setX(x: double), getX(), setY(y: double), getY() are the setter and getter.
  • setXY(x: double, y:double) is the atribute to change the values of x and y.
  • distance(x: double, y:double) method returns the distance between the current objective and the point (x, y)
  • distance(another: Point) method returns the distance between the current objective and another objective (another is also an objective of Point class).

A program to test Point class:

public class Entry {
	public static void main(String[] args) {
		Point p1 = new Point(1.5, 6.7);
		Point p2 = new Point(2.8, 3.2);
		System.out.println(p1.distance(2.34, 7.8));

When the above code is compiled and executed, it produces the following result:



You need to create a Point class as below:

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

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

	public double getX() {
		return x;

	public void setX(double x) {
		this.x = x;

	public double getY() {
		return y;

	public void setY(double y) {
		this.y = y;

	public void setXY(double x, double y) {
		this.x = x;
		this.y = y;

	public double distance(double x, double y) {
		return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));

	public double distance(Point another) {
		return distance(another.getX(), another.getY());