//For TestCircle.java
public class TestCircle {
public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.printf("Radius of the circle: %.2f%n", circle.getRadius());
System.out.printf("Area of the circle: %.2f%n", circle.calculateArea());
System.out.printf("Perimeter of the circle: %.2f%n%n", circle.calculatePerimeter());
circle.setRadius(7);
System.out.printf("Radius of the circle: %.2f%n", circle.getRadius());
System.out.printf("Area of the circle: %.2f%n", circle.calculateArea());
System.out.printf("Perimeter of the circle: %.2f%n%n", circle.calculatePerimeter());
circle.setRadius(-3);
}
}
///////////////////////////////////////////////////////////////////////////////////////////
//For Circle.java
///////////////////////////////////////////////////////////////////////////////////////////
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
if (radius > 0) {
this.radius = radius;
} else {
System.out.println("Radius must be greater than 0");
}
}
public double calculateArea() {
return Math.PI * radius * radius;
}
public double calculatePerimeter() {
return 2 * (Math.PI * radius);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter