forked from FIT-DNU/Object-Oriented-Programming-with-Java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterfaceShape.java
More file actions
58 lines (53 loc) · 1.62 KB
/
InterfaceShape.java
File metadata and controls
58 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package dahinh;
import java.util.Scanner;
interface Shape2D {
double area();
double perimeter();
}
class Circle implements Shape2D {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
@Override
public double perimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle implements Shape2D {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double area() {
return width * height;
}
@Override
public double perimeter() {
return 2 * (width + height);
}
}
public class Shape {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Nhập bán kính hình tròn: ");
double r = sc.nextDouble();
System.out.print("Nhập chiều rộng hình chữ nhật: ");
double w = sc.nextDouble();
System.out.print("Nhập chiều cao hình chữ nhật: ");
double h = sc.nextDouble();
Shape2D circle = new Circle(r);
Shape2D rectangle = new Rectangle(w, h);
System.out.printf("Circle: area = %.2f, perimeter = %.2f%n",
circle.area(), circle.perimeter());
System.out.printf("Rectangle: area = %.2f, perimeter = %.2f%n",
rectangle.area(), rectangle.perimeter());
}
}