-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.java
More file actions
60 lines (46 loc) · 1.14 KB
/
test.java
File metadata and controls
60 lines (46 loc) · 1.14 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
59
60
// To use the sqrt function
import java.lang.Math;
interface Polygon {
void getArea();
// calculate the perimeter of a Polygon
default void getPerimeter(int... sides) {
int perimeter = 0;
for (int side : sides) {
perimeter += side;
}
System.out.println("Perimeter: " + perimeter);
}
}
class Triangle implements Polygon {
private int a, b, c;
private double s, area;
// initializing sides of a triangle
Triangle(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
s = 0;
}
// calculate the area of a triangle
public void getArea() {
s = (double) (a + b + c) / 2;
area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.println("Area: " + area);
}
}
class Main {
public static void main(String[] args) {
Triangle t1 = new Triangle(2, 3, 4);
// calls the method of the Triangle class
t1.getArea();
// calls the method of Polygon
t1.getPerimeter(2, 3, 4);
}
}
interface Shape {
// implicitly public, static and final
public String LABLE = "Shape";
// interface methods are implicitly abstract and public
void draw();
double getArea();
}