-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
89 lines (73 loc) · 1.61 KB
/
Triangle.java
File metadata and controls
89 lines (73 loc) · 1.61 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public class Triangle implements Shapes
{
private double a;
private double b;
private double c;
public Triangle()
{
a=0;
b=0;
c=0;
}
// initializes properties for shape if applicacle
public Triangle(double e , double f, double g) throws Exception
{
if((e > 0 && f > 0 && g > 0) && (( e + f ) > g ) && ((e + g) > f) && ((f + g) > e))
{
a = e;
b = f;
c = g;
}else{
throw new Exception("Data is not valid - tried to create Triangle");
}
}
//Getter methods
public double getA()
{
return a;
}
public double getB()
{
return b;
}
public double getC()
{
return c;
}
//Calculates perimeter using a,b and c
public double getPerimeter()
{
Shapes Triangle = () ->{return a+b+c;};
return Triangle.getPerimeter();
}
// returns objects fields and its perimeter
public String toString()
{
return "Triangle{a=" + a + ", b="+ b + ", c="+ c + "} " + getPerimeter();
}
//Generates a unique value based up on objects parameters
public int hashCode()
{
int aTri = (int)Math.round(a)*3410;
int bTri = (int)Math.round(b)*24140;
int cTri = (int)Math.round(c)*24140;
return aTri + bTri + cTri + 12;
}
// checks if its equal based on defined conditions and returns appropriate boolean
public boolean equals(Shapes shape)
{
if(this == shape)
{
return true;
}
if(shape == null)
{
return false;
}
if(!(shape instanceof Triangle))
{
return false;
}
return a == ((Triangle)shape).getA() && b == ((Triangle)shape).getB() && c == ((Triangle)shape).getC();
}
}