Bài tập Java OOP: cài đặt lớp Circle theo sơ đồ lớp cho trước | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task 

Declare Circle class with the following information:

Explanation:

  • radius is the attribute that represents the radius of the circle.
  • Circle(radius) is the method that initializes parameters which are used to assign values to radius attribute.
  • getArea() and getCircumference() is two method that returns the area and perimeter of a circle with pi (π) = 3.14.

The program to test Circle class:

public class Entry {
	public static void main(String[] args) {
		Circle c = new Circle(7);
		System.out.println(c.getArea());
		System.out.println(c.getCircumference());
	}
}

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

153.86
43.96

Instruction

You need to create Circle class as below:

class Circle {
	private double radius;

	public Circle(double radius) {
		this.radius = radius;
	}

	public double getArea() {
		return 3.14 * radius * radius;
	}

	public double getCircumference() {
		return 3.14 * 2 * radius;
	}
}