InterfaceDemo.java
· 605 B · Java
Bruto
interface Shape {
double area();
double perimeter();
}
class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
public double perimeter() {
return 2 * Math.PI * radius;
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Circle circle = new Circle(5.0);
System.out.println("Circle Area: " + circle.area());
System.out.println("Circle Perimeter: " + circle.perimeter());
}
}
1 | interface Shape { |
2 | double area(); |
3 | double perimeter(); |
4 | } |
5 | |
6 | class Circle implements Shape { |
7 | private double radius; |
8 | |
9 | public Circle(double radius) { |
10 | this.radius = radius; |
11 | } |
12 | |
13 | public double area() { |
14 | return Math.PI * radius * radius; |
15 | } |
16 | |
17 | public double perimeter() { |
18 | return 2 * Math.PI * radius; |
19 | } |
20 | } |
21 | |
22 | public class InterfaceDemo { |
23 | public static void main(String[] args) { |
24 | Circle circle = new Circle(5.0); |
25 | System.out.println("Circle Area: " + circle.area()); |
26 | System.out.println("Circle Perimeter: " + circle.perimeter()); |
27 | } |
28 | } |
29 |