-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
68 lines (57 loc) · 1.78 KB
/
Copy pathSquare.java
File metadata and controls
68 lines (57 loc) · 1.78 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
61
62
63
64
65
66
67
68
import java.util.Random;
public class Square {
public static class Dot {
double X;
double Y;
public Dot(double x, double y) {
this.X = x;
this.Y = y;
}
public Dot() {
Random rand = new Random();
X = rand.nextDouble() * 10;
Y = rand.nextDouble() * 10;
}
}
private Dot A, B, C, D;
public double P;
public Square() {
createSquare();
if (!isValidSquare()){
createSquare();
}
P = getPerimeter();
}
public void createSquare() {
A = new Dot();
B = new Dot();
double sideLength = sideLength(A, B);
C = new Dot(B.X + sideLength, B.Y);
D = new Dot(A.X + sideLength, A.Y);
}
public boolean isValidSquare() {
double AB = sideLength(A, B);
double BC = sideLength(B, C);
double CD = sideLength(C, D);
double AD = sideLength(A, D);
double d1 = sideLength(A, C);
double d2 = sideLength(B, D);
return (AB == BC && BC == CD && CD == AD && d1 == d2);
}
private double sideLength(Dot start, Dot end) {
return Math.sqrt(Math.pow((start.X - end.X), 2) + Math.pow((start.Y - end.Y), 2));
}
public double getPerimeter() {
double sideLength = sideLength(A, B);
return 4 * sideLength;
}
public double getArea() {
double sideLength = sideLength(A, B);
return sideLength * sideLength;
}
@Override
public String toString() {
return String.format("Координати вершин: (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f), (%.2f, %.2f)\nПериметр: %.2f\nПлоща: %.2f",
A.X, A.Y, B.X, B.Y, C.X, C.Y, D.X, D.Y, getPerimeter(), getArea());
}
}