-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsealed.html
153 lines (121 loc) · 3.41 KB
/
sealed.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://javaalmanac.io/almanac.min.css">
</head>
<body>
<div class="content" id="content">
<sandbox version="java17" mainclass="Main" preview="true" v-cloak>
<sandbox-source name="Main.java">
record Point(int x, int y) {}
abstract sealed class Shape permits Circle, Rectangle, Triangle {
protected final Point[] points;
Shape(int edges) {
this.points = new Point[edges];
}
Shape(Point... points) {
this.points = points;
}
public int getEdges() {
return this.points.length;
}
protected abstract double area();
protected abstract double perimeter();
}
final class Circle extends Shape {
private final int radius;
Circle() {
super(1);
this.radius = 1;
}
Circle(Point center, int radius) {
super(center);
this.radius = radius;
}
public int getRadius() {
return radius;
}
public Point getCenter() {
return points[0];
}
@Override
public double area() {
return Math.PI * (radius * radius);
}
@Override
public double perimeter() {
return Math.PI * 2*radius;
}
}
final class Rectangle extends Shape {
private int width, height;
Rectangle(int width, int height) {
super(1);
this.width = width;
this.height = height;
}
Rectangle(Point upperLeft, int width, int height) {
super(upperLeft);
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
@Override
public double area() {
return width * height;
}
@Override
public double perimeter() {
return 2*width + 2*height;
}
}
final class Triangle extends Shape {
Triangle(Point... points) {
super(points);
}
public Point[] getPoints() {
return points;
}
// see https://tinyurl.com/msfdtjme
private double length(Point p1, Point p2) {
return Math.sqrt((p2.x() - p1.x()) * (p2.x() - p1.x()) + (p2.y() - p1.y()) * (p2.y() - p1.y()));
}
// see https://www.ypologismos.gr/emvadon-trigonou-xerontas-3-pleyres-typos-hrona/
@Override
public double area() {
double s = perimeter()/2;
double a = length(points[0], points[1]);
double b = length(points[1], points[2]);
double c = length(points[2], points[0]);
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
// https://www.ypologismos.gr/emvadon-trigonou-xerontas-3-pleyres-typos-hrona/
@Override
public double perimeter() {
return length(points[0], points[1]) + length(points[1], points[2]) + length(points[2], points[0]);
}
}
public class Main {
public static void main(String[] args) {
Shape c = new Circle(new Point(10, 10), 10);
System.out.println(c.perimeter());
System.out.println(c.area());
Shape r = new Rectangle(new Point(0, 0), 10, 10);
System.out.println(r.perimeter());
System.out.println(r.area());
Shape t = new Triangle(new Point(0, 0), new Point(10, 10), new Point(-10, -10));
System.out.println(t.perimeter());
System.out.println(t.area());
}
}</sandbox-source>
</sandbox>
</div>
<script src="https://javaalmanac.io/app/sandbox-bundle.js"></script>
<script>new Vue({ el: '#content' })</script>
</body>
</html>