-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle.java
More file actions
82 lines (65 loc) · 1.47 KB
/
Rectangle.java
File metadata and controls
82 lines (65 loc) · 1.47 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
public class Rectangle implements Shapes
{
private double length;
private double width;
// initializes properties for shape if applicacle
public Rectangle(double l, double w)throws Exception{
if( l > 0 && w > 0)
{
length = l;
width = w;
}else
{
throw new Exception("Data is not valid - tried to create Rectangle");
}
}
public Rectangle()
{
length = 0;
width = 0;
}
//Calculates perimeter using length and width
public double getPerimeter()
{
Shapes Rectangle = () ->{return length*2 + width*2;};
return Rectangle.getPerimeter();
}
// returns objects fields and its perimeter
public String toString()
{
return "Rectangle{l=" + length + ", w="+ width + "} " + getPerimeter();
}
//Generates a unique value based up on objects parameters
public int hashCode()
{
int l = (int)Math.round(length)*3410;
int w = (int)Math.round(width)*24140;
return l + w + 12;
}
// Getter methods
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
// 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 Rectangle))
{
return false;
}
return length == ((Rectangle)shape).getLength() && width == ((Rectangle)shape).getWidth();
}
}