-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode6.java
More file actions
47 lines (40 loc) · 1.33 KB
/
Code6.java
File metadata and controls
47 lines (40 loc) · 1.33 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
abstract class Shape {
public abstract double calculateArea();
public abstract double calculatePerimeter();
}
class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double calculateArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double calculatePerimeter() {
return side1 + side2 + side3;
}
public String getType() {
if (side1 == side2 && side2 == side3) {
return "Equilateral";
} else if (side1 == side2 || side2 == side3 || side1 == side3) {
return "Isosceles";
} else {
return "Scalene";
}
}
}
public class Code6 {
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("Area of the triangle: " + triangle.calculateArea());
System.out.println("Perimeter of the triangle: " + triangle.calculatePerimeter());
System.out.println("Type of the triangle: " + triangle.getType());
}
}