Bài tập Java OOP: điểm và hình tam giác | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create a Point class and a Triangle class like the following class diagram:

 

Explanation:

Point class

  • This is the class used to store information about a point in a two-dimensional coordinate system.
  • x and y attributes are ordinate and abscissa respectively.
  • Point(x: int, y: int) is the parameter constructor which is used for initializing values to x and y.
  • setX(), getX(), setY(), getY() are setters and getters.

Triangle class

  • This is the class that stores information about triangles.
  • vertice1, vertice2, vertice3 attributes describe 3 vertices of the triangle.
  • Triangle(vertice1: Point, vertice2: Point, vertice3: Point) is the parameter constructor which is used for initializing values to 3 vertices of the triangle.
  • Triangle(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int) is the parameter constructor which is used for initializing values to 3 vertices of the triangle.
  • getPerimeter() method returns the circumference of a triangle. For example, if vertice1 = (1, 3), vertice2 = (2, 3), vertice3 = (4, 2), getPerimeter() method returns the following result 8.48528137423857.

A program to test Triangle class:

public class Entry {
	public static void main(String[] args) {
		Point vertice1 = new Point(1, 3);
		Point vertice2 = new Point(2, 3);
		Point vertice3 = new Point(4, 2);
		Triangle triangle1 = new Triangle(vertice1, vertice2, vertice3);
		Triangle triangle2 = new Triangle(4, 5, 2, 6, 3, 7);
		System.out.println(triangle1.getPerimeter());
		System.out.println(triangle2.getPerimeter());
	}
}

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

6.39834563766817
5.8863495173726745

Note: Codelearn compiler accepts the numerical error less than 0.000001

Instruction

Code sample:

File Point.java

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

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

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

	public int getX() {
		return x;
	}

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

	public int getY() {
		return y;
	}
}

File Triangle.java

public class Triangle {
	private Point vertice1;
	private Point vertice2;
	private Point vertice3;

	public Triangle(Point vertice1, Point vertice2, Point vertice3) {
		this.vertice1 = vertice1;
		this.vertice2 = vertice2;
		this.vertice3 = vertice3;
	}

	public Triangle(int x1, int y1, int x2, int y2, int x3, int y3) {
		vertice1 = new Point(x1, y1);
		vertice2 = new Point(x2, y2);
		vertice3 = new Point(x3, y3);
	}

	public double getPerimeter() {
		double d1 = Math.sqrt(Math.pow(vertice1.getX() - vertice2.getX(), 2) 
				+ Math.pow(vertice1.getY() - vertice2.getY(), 2));
		double d2 = Math.sqrt(Math.pow(vertice1.getX() - vertice3.getX(), 2)
				+ Math.pow(vertice1.getY() - vertice3.getY(), 2));
		double d3 = Math.sqrt(Math.pow(vertice2.getX() - vertice3.getX(), 2)
						+ Math.pow(vertice2.getY() - vertice3.getY(), 2));
		return d1 + d2 + d3;
	}
}