-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRectangle.java
51 lines (43 loc) · 1.24 KB
/
Rectangle.java
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
package lab5;
public class Rectangle {
Point bottomLeft;
double base;
double height;
public Rectangle(Point point, double base, double height) {
if (base <= 0)
throw new IllegalArgumentException("Base must be positive.");
if (height <= 0)
throw new IllegalArgumentException("Height must be positive.");
this.bottomLeft = point;
this.base = base;
this.height = height;
}
public double calculatePerimeter() {
return (2 * base) + (2 * height);
}
public double calculateArea() {
return height * base;
}
public boolean pointInRectangle(Point A) {
return A.x <= (bottomLeft.x + base) && A.x >= bottomLeft.x && A.y <= (bottomLeft.y + height) && A.y >= bottomLeft.y;
}
@Override
public int hashCode() {
if (bottomLeft == null)
return 0;
return bottomLeft.hashCode() + (int)(3*base + 5*height);
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (!(obj instanceof Rectangle))
return false;
Rectangle other = (Rectangle) obj;
if (this.bottomLeft == null || bottomLeft == null)
return false;
return bottomLeft.equals(other.bottomLeft) && (height == other.height) && (base == other.base);
}
}